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

# Python SDK for KYC, AML and Transactions

> Quickstart for the official Didit Python SDK: typed sync and async clients for the server APIs, webhook verification and Authentication token checks.

The official Didit library for Python. It needs Python 3.10 or later and offers a synchronous client, `Didit`, and an asynchronous one, `AsyncDidit`, with the same methods. See the [server SDKs overview](https://docs.didit.me/integration/server-sdks/overview.md) for what every SDK shares.

## Install

```bash theme={null}
pip install didit-sdk
```

The package is [`didit-sdk` on PyPI](https://pypi.org/project/didit-sdk/). Its source and changelog are in [didit-protocol/sdk-python](https://github.com/didit-protocol/sdk-python) on GitHub.

To verify [Authentication](#verify-authentication-tokens) tokens, install the extra that adds the signature check:

```bash theme={null}
pip install "didit-sdk[auth]"
```

## Configure the client

`Didit()` reads `DIDIT_API_KEY` from the environment. Pass options to override it, or to set the organization and application ids that application-scoped calls (cases, networks, regulatory reports) need.

```python theme={null}
import os

from didit import Didit

client = Didit(
    api_key=os.environ["DIDIT_API_KEY"],
    organization_id=os.environ.get("DIDIT_ORGANIZATION_ID"),
    application_id=os.environ.get("DIDIT_APPLICATION_ID"),
    timeout=30,
    max_retries=2,
)

# Options for a single call.
fast = client.with_options(timeout=5, max_retries=0)
```

## Create a session

Create a [verification session](https://docs.didit.me/sessions-api/create-session.md) for a workflow and send your user to its URL.

```python theme={null}
from didit import Didit

client = Didit()

session = client.sessions.create(
    workflow_id="11111111-2222-3333-4444-555555555555",
    vendor_data="user-123",
    callback="https://example.com/verification/done",
)
print(session.url)
```

## Read the decision

When the user finishes (or when the webhook arrives), [read the decision](https://docs.didit.me/sessions-api/retrieve-session.md). Every feature result is a list with one entry per workflow node.

```python theme={null}
from didit import Didit

client = Didit()

decision = client.sessions.retrieve_decision("11111111-2222-3333-4444-555555555555")
print(decision.status)
for verification in decision.id_verifications or []:
    print(verification.first_name, verification.last_name)
```

## Verify a webhook

Pass the exact raw body bytes and the request headers. `verify` raises `WebhookVerificationError` for anything that is not a valid, fresh delivery, so catching that one class is enough.

```python theme={null}
import os

import didit

WEBHOOK_SECRET = os.environ["DIDIT_WEBHOOK_SECRET"]


def handle_webhook(raw_body: bytes, headers: didit.webhooks.HeadersLike) -> int:
    try:
        event = didit.webhooks.verify(raw_body, headers, WEBHOOK_SECRET)
    except didit.WebhookVerificationError as error:
        print("rejected:", error.reason)
        return 400
    if isinstance(event, didit.webhooks.SessionStatusUpdatedEvent):
        print(event.session_id, event.status)
    return 200
```

In Flask that is `handle_webhook(request.get_data(), dict(request.headers))`; in FastAPI, `handle_webhook(await request.body(), request.headers)`. While you rotate the secret, pass both: `[new_secret, old_secret]`. See [Webhooks](https://docs.didit.me/integration/webhooks.md) for the events and the signature scheme.

## Screen a transaction

Submit a payment, withdrawal or crypto transfer to [transaction monitoring](https://docs.didit.me/transaction-monitoring/overview.md); the response carries its status and risk score.

```python theme={null}
from didit import Didit

client = Didit()

transaction = client.transactions.create(
    transaction_id="txn-1001",
    transaction_category="finance",
    transaction_details={"direction": "outbound", "amount": 1500, "currency": "EUR"},
    subject={"entity_type": "individual", "vendor_data": "user-123", "full_name": "Maria Garcia"},
)
print(transaction.status, transaction.score)
```

## Verify Authentication tokens

`didit.authentication` checks the access tokens your [Authentication](https://docs.didit.me/authentication/overview.md) issuer signs, in your backend, without calling Didit for each request. It also exchanges codes, refreshes tokens, checks and revokes sessions, and runs step-ups.

> [!NOTE]
>
> Token verification makes the checks in [Verify a token on your server](https://docs.didit.me/authentication/tokens-and-jwks.md#verify-a-token-on-your-server), against your application's issuer and signing keys.

```python theme={null}
from didit.authentication import Authentication, TokenVerificationError

# The publishable key (pk_live_... or pk_test_...) carries your issuer URL.
auth = Authentication.from_publishable_key("pk_live_...", client_id="your-application-id")


def current_user(access_token: str) -> str | None:
    try:
        claims = auth.verify_access_token(access_token)
    except TokenVerificationError as error:
        print("rejected:", error.reason)
        return None
    return claims.sub
```

## Errors

```python theme={null}
import didit
from didit import Didit

client = Didit()

try:
    client.sessions.retrieve_decision("11111111-2222-3333-4444-555555555555")
except didit.NotFoundError:
    print("no such session")
except didit.RateLimitError as error:
    print("slow down, request", error.request_id)
except didit.APIStatusError as error:
    print(error.status_code, error.body)
```

## Lists, files and async

```python theme={null}
import asyncio
from pathlib import Path

from didit import AsyncDidit, Didit

client = Didit()

# Lists page through every result.
for session in client.sessions.list(status="Approved"):
    print(session.session_id)

# Uploads take a path, bytes or an open file.
result = client.id_verification.verify(front_image=Path("id-front.jpg"), back_image=Path("id-back.jpg"))
print(result.id_verification)


async def main() -> None:
    async with AsyncDidit() as aclient:
        session = await aclient.sessions.create(workflow_id="11111111-2222-3333-4444-555555555555")
        print(session.url)


asyncio.run(main())
```

Server endpoint pages of the [API reference](https://docs.didit.me/api-reference/overview.md) have a Python sample for their operation, except for the endpoints and fields that no 1.0.1 release has yet, named in the [overview](https://docs.didit.me/integration/server-sdks/overview.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.