> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apitally.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Configuration

> Configure the Apitally SDK for Python.

<CodeGroup>
  ```python FastAPI {6-11} theme={null}
  import apitally
  from fastapi import FastAPI

  app = FastAPI()

  apitally.init(
      app,
      write_token="your-write-token",
      env="dev",
      # other parameters ...
  )
  ```

  ```python Django {4-8} theme={null}
  import apitally

  # At the end of settings.py
  apitally.init(
      write_token="your-write-token",
      env="dev",
      # other parameters ...
  )
  ```

  ```python Flask {6-11} theme={null}
  import apitally
  from flask import Flask

  app = Flask(__name__)

  apitally.init(
      app,
      write_token="your-write-token",
      env="dev",
      # other parameters ...
  )
  ```
</CodeGroup>

## Parameters

### General

#### `write_token`

Write token for your application in the format `apt_...`. Find it on the *Setup instructions* page for your app. Required unless the `APITALLY_WRITE_TOKEN` environment variable is set.

#### `env`

Name of the environment, for example `prod` or `dev`. Can also be set via the `APITALLY_ENV` environment variable. The environment is created automatically if it does not exist. Defaults to `dev`.

#### `app_version`

The current version of your application, for example `1.0.0`.

#### `disabled`

Boolean. Whether to disable the SDK. Truthy `APITALLY_DISABLED` or `OTEL_SDK_DISABLED` environment variables also disable it. Defaults to `False`.

### Data capture

#### `capture_logs`

Boolean. Whether to capture application logs from the standard `logging` module and other third-party logging integrations. Only logs associated with captured requests are exported. Defaults to `True`.

#### `capture_request_headers`

Boolean. Whether to capture request headers. Default masking applies to common sensitive headers such as `Authorization`. Defaults to `False`.

#### `capture_request_body`

Boolean. Whether to capture the request body. Only JSON and text bodies up to 50 KB are supported. Defaults to `False`.

#### `capture_response_headers`

Boolean. Whether to capture response headers. Defaults to `True`.

#### `capture_response_body`

Boolean. Whether to capture the response body. Only JSON and text bodies up to 50 KB are supported. Defaults to `False`.

### Masking

#### `mask_query_params`

List of regular expressions for matching query parameters to mask. These are applied in addition to the default masking patterns. Defaults to `[]`.

```python theme={null}
mask_query_params=[r"^api_key$", r"^token$"],
```

#### `mask_headers`

List of regular expressions for matching headers to mask. These are applied in addition to the default masking patterns. Defaults to `[]`.

```python theme={null}
mask_headers=[r"^X-Api-Key$", r"^X-Internal-"],
```

#### `mask_body_fields`

List of regular expressions for matching request and response body fields to mask. These are applied in addition to the default masking patterns. Defaults to `[]`.

```python theme={null}
mask_body_fields=[r"^credit_card$", r"^account_id$"],
```

#### `mask_request_body`

Callback for masking the request body. It receives the request [`ReadableSpan`](https://opentelemetry-python.readthedocs.io/en/stable/sdk/trace.html#opentelemetry.sdk.trace.ReadableSpan) and body as `bytes`, and returns the masked body as `bytes` or `None`.

See example [here](/sdk-reference/python/v1/masking#body-masking-callbacks).

#### `mask_response_body`

Callback for masking the response body. It receives the request [`ReadableSpan`](https://opentelemetry-python.readthedocs.io/en/stable/sdk/trace.html#opentelemetry.sdk.trace.ReadableSpan) and body as `bytes`, and returns the masked body as `bytes` or `None`.

See example [here](/sdk-reference/python/v1/masking#body-masking-callbacks).

#### `mask_log_record`

Callback for masking application logs. It receives an OpenTelemetry [`ReadWriteLogRecord`](https://opentelemetry-python.readthedocs.io/en/stable/sdk/_logs.html#opentelemetry.sdk._logs.ReadWriteLogRecord) and returns the same record after masking, or `None` to drop it.

See example [here](/sdk-reference/python/v1/masking#log-record-masking-callback).

### Sampling

#### `sample_rate`

Fraction of requests to capture as request logs and traces, from `0.0` to `1.0`. Metrics are not sampled. Defaults to `1.0`.

#### `sample_on_request`

Callback for adjusting sampling when a request starts. It receives the request [`ReadableSpan`](https://opentelemetry-python.readthedocs.io/en/stable/sdk/trace.html#opentelemetry.sdk.trace.ReadableSpan) and returns a probability, a boolean, or `None` to use `sample_rate`.

See example [here](/sdk-reference/python/v1/sampling#custom-sampling).

#### `sample_on_response`

Callback for adjusting sampling when a response is ready. It receives the ended request [`ReadableSpan`](https://opentelemetry-python.readthedocs.io/en/stable/sdk/trace.html#opentelemetry.sdk.trace.ReadableSpan) and returns a probability, a boolean, or `None` to preserve the existing decision.

See example [here](/sdk-reference/python/v1/sampling#custom-sampling).

#### `exclude_paths`

List of regular expressions for matching paths to exclude from request logs and traces. Excluded requests are still included in metrics. Defaults to `[]`.

```python theme={null}
exclude_paths=[r"^/admin/$", r"/internal/"],
```

### Django-specific

#### `django_urlconf`

String or list of strings representing Django URLconf modules used to discover routes and schemas. Set this to `None` to use the root URLconf.

```python theme={null}
django_urlconf=["myproject.urls", "myapp.urls"],
```

#### `django_include_class_based_views`

Boolean. Whether to include class-based Django views in the reported endpoint list in addition to Django REST Framework and Django Ninja routes. Defaults to `False`.
