---
title: "Python SDK: the client, errors, stream and helpers"
description: "What doorman_inbox has besides the generated methods: the client and its arguments, the exceptions, the event stream, webhook verification and the OAuth helpers."
url: https://doorman.fyi/docs/python/reference/client
markdown: https://doorman.fyi/docs/python/reference/client.md
area: "SDKs and tools"
section: "Python SDK"
---

# Python SDK: the client, errors, stream and helpers

What doorman_inbox has besides the generated methods: the client and its arguments, the exceptions, the event stream, webhook verification and the OAuth helpers.

Source: https://doorman.fyi/docs/python/reference/client

## The client

`DoormanInbox` is synchronous and is a context manager: leaving the `with` block closes its connections. Every argument is given by keyword.

Python:
```python
DoormanInbox(*, base_url: str, api_key: str | None = None, timeout: float = 30, transport: httpx.BaseTransport | None = None)
```

- `base_url`: The API origin. It is always given: the client never assumes a deployment. Use HTTPS outside isolated local development.
- `api_key`: An API key or an app's access token, sent as a bearer header. Leave it out for the operations that take no credential.
- `timeout`: Seconds for one request.
- `transport`: An `httpx` transport in place of the network, for a test.

Python:
```python
import os
from doorman_inbox import DoormanInbox

with DoormanInbox(
    base_url="https://api.doorman.fyi",
    api_key=os.environ["DOORMAN_API_KEY"],
) as client:
    me = client.me()
```

- `client.base_url` is the origin as the client keeps it, and `client.close()` closes it outside a `with` block.
- One attempt is made for each call. Nothing is retried, no redirect is followed, and no ambient cookie or proxy credential is sent.
- A path parameter is a positional argument in snake_case. A request body is one dictionary named `body`, with the API's own JSON keys. Query parameters are keyword arguments in snake_case; one set to `None` is left out.
- An answer is a dictionary with the API's own JSON keys. `doorman_inbox.models` holds a `TypedDict` for each schema of the contract, and the package ships `py.typed`.

## Errors

Every error is importable from `doorman_inbox`. A failure of the network itself stays the `httpx` exception it was.

| Exception | Raised when | Attributes |
| --- | --- | --- |
| `ApiError` | The API answered `400` or more. | `operation`, `status`, `code`, `message`, `details`, `retry_after_seconds` |
| `WebhookVerificationError` | A delivery failed verification. | `reason` |
| `StreamFrameError` | The server refused a subscription, or sent a frame that is not valid. | the message |
| `StreamClosedError` | The stream closed and will not reconnect. | `code`, `reason` |
| `OAuthProtocolError` | An OAuth protocol request was refused. | `status`, `code`, `retry_after_seconds` |

`ApiError.code` is a code of the [error catalogue](https://doorman.fyi/docs/errors.md), and `details` holds the fields some codes add. No request header, credential or raw body is kept on an error. `OAuthProtocolError` carries the protocol's error code only, never a description the server sent back.

## The stream

Python:
```python
client.stream(*, inbox_ids: Sequence[str] = (), event_types: Sequence[str] = (), last_event_id: str | None = None, reconnect: bool = True, max_reconnects: int = 5, ping_interval: float = 45, open_timeout: float = 10) -> InboxStream
```

An `InboxStream` is a context manager and an iterator of events. Entering the context opens the socket and waits for the server to acknowledge the subscription, without consuming replayed events.

- `inbox_ids`, `event_types`: What to subscribe to. Empty means every inbox and every event type the credential may see.
- `last_event_id`: Replay what came after this event before the live feed.
- `reconnect`, `max_reconnects`: Whether an unexpected close is followed by a new connection, and how many in a row are tried.
- `ping_interval`, `open_timeout`: Seconds between keepalive pings while idle, and seconds to wait for the connection to open.
- `stream.last_event_id`: The last event the iterator yielded. It says the event was handed to the loop, not that the loop finished its work.
- `stream.closed`, `stream.close()`: Whether the stream has ended, and the call that ends it.

The credential goes in a bearer header and never in the address. A close for the credential or for the account's connection limit is final and raises `StreamClosedError`. See [The WebSocket stream](https://doorman.fyi/docs/stream.md).

## Verifying webhooks

Python:
```python
verify_webhook(headers: Mapping[str, str | Sequence[str]], body: bytes | str, secret: str, *, tolerance_seconds: float = 300, now: float | None = None) -> WebhookEvent
```

Checks the signature over the body exactly as it was received, refuses a timestamp further than `tolerance_seconds` from now, then reads the JSON and checks it against the schema of its event type. `now` is seconds since the epoch, for a test.

Python:
```python
verify_webhook_signature(headers: Mapping[str, str | Sequence[str]], body: bytes | str, secret: str, *, tolerance_seconds: float = 300, now: float | None = None) -> str
```

The same check without reading the payload: it returns the body as text, for a receiver that parses it itself. Both raise `WebhookVerificationError`. See [Verifying a delivery](https://doorman.fyi/docs/webhooks.md#verify).

## OAuth

Python:
```python
create_pkce() -> Pkce
```

A fresh verifier, its S256 challenge and a state, as `pkce.verifier`, `pkce.challenge`, `pkce.state` and `pkce.method`. Keep the verifier and the state in the session of the person who started the flow until the callback, and never log them.

Python:
```python
parse_oauth_callback(value: str, *, redirect_uri: str, state: str, issuer: str) -> str
```

Checks the callback address against the redirect address, the state and the issuer, and returns the code. It raises `OAuthProtocolError` for a denial and for a callback that does not match.

The protocol's own requests are methods of `client.oauth`, listed under [OAuth](https://doorman.fyi/docs/python/reference/oauth.md). They leave out the client's credential and cookies. See [Apps and OAuth](https://doorman.fyi/docs/oauth.md).
