# Configuring the Python SDK

```python
from watchtower15 import AsyncClient, Client

client = Client(api_key="wt15ak_…", timeout=10)
from_env = Client.from_environment(max_retries=4)
async_client = AsyncClient.from_environment()
```

`from_environment(**overrides)` reads five variables from `os.environ`, each the first non-empty value after trimming, an override winning. The constructor reads no environment, so a test never picks a credential up by accident. Every credential is optional at construction: an operation that needs one refuses, naming the argument and the variable, when it is used. An unknown override is refused.

| Variable | Argument | Default |
| --- | --- | --- |
| `WT15_API_KEY` | `api_key` | none |
| `WT15_METRICS_TOKEN` | `metrics_token` | none |
| `WT15_LOGS_TOKEN` | `logs_token` | none |
| `WT15_API_URL` | `api_url` | `https://rest.watchtower15.com/v1` |
| `WT15_INGEST_URL` | `ingest_url` | `https://api.watchtower15.com` |

## Arguments

| Argument | Meaning | Bounds |
| --- | --- | --- |
| `api_key`, `metrics_token`, `logs_token` | the credentials; `metrics_token` may be a callable returning a token, called only when a request is about to be sent with it | each checked locally |
| `api_url`, `ingest_url` | the base URLs | `https`, or `http` for `localhost`, `127.0.0.1` and `[::1]` only; no user-info, query or fragment; a trailing `/` is dropped |
| `timeout` | the whole request’s limit, in seconds (15 by default) | above 0, at most 300 |
| `connect_timeout` | the limit for making the connection, in seconds (2 by default) | above 0, not above `timeout` |
| `max_retries` | how many times a failed request is sent again (2 by default) | 0 to 10 |
| `http_client` | an `httpx2.Client` (`httpx2.AsyncClient` for `AsyncClient`) to send through | see below |
| `app_info` | `name/version` of your application, added to the `User-Agent` | letters, digits and `._+-` |

An argument out of its bounds, an unknown argument or a malformed credential raises a `ConfigurationError` (a `ValueError`) naming it, before anything is sent. A credential given as a string is trimmed, and an empty one is absent.

## Credential checks

An API key carries a checksum; the SDK verifies it, the prefix and the length locally, so a truncated or mistyped credential never reaches a host. A credential of another kind in a slot is refused: `This is a metric source token (wt15ms_…abcd): pass it as` metrics\_token`(WT15_METRICS_TOKEN), not`api\_key`.` Nothing the SDK prints holds a credential in full: `repr()` of a client shows masked forms, a client cannot be pickled, and no exception, log record or response object holds a request’s headers or body.

## Timeouts and retries

`timeout` limits the whole exchange and `connect_timeout` the connection. Every call can override `timeout`, `connect_timeout`, `max_retries` and `max_retry_wait` in its [`request_options`](https://developer.watchtower15.com/sdks/python/api/). Reads are retried on a connection failure, a 408, a 429 (waiting for `Retry-After`) and a 502, 503 or 504, with a backoff from 0.5 s to 8 s; a write is sent with an `Idempotency-Key` and retried the same way and on any 5xx; a daily limit is never retried.

## Logging

The SDK logs on the `watchtower15` logger and installs only a `NullHandler`, so it is silent until you configure `logging`. Each retry is logged at `INFO`, and a `Deprecation` or `Sunset` notice at `WARNING`. Nothing it logs holds a credential.

```python
import logging
logging.getLogger("watchtower15").setLevel(logging.INFO)
```

## The User-Agent

The only metadata the SDK sends is the `User-Agent`: `watchtower15-python/<version> python/3.12 [framework/version] [app_info]`. An integration adds its framework once (`django/5.2.1`).

## A custom httpx2 client, a private CA

Pass `http_client` to route through a proxy, to set a certificate bundle (`httpx2.Client(verify="/etc/ssl/corp-ca.pem")`) or to use a test transport. The SDK closes only a client it built; one you pass is yours to close. After a `fork()` (gunicorn, Celery prefork, `multiprocessing`) build the client in the child: see [servers, workers and serverless](https://developer.watchtower15.com/sdks/python/deployment/). Without `http_client`, the SDK uses httpx2’s defaults, so `SSL_CERT_FILE` and `SSL_CERT_DIR` apply. Use a client as a context manager (`with Client(...) as client:`), or call `close()`, or `await aclose()`.

Source: https://developer.watchtower15.com/sdks/python/configuration/
