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

# Python

> Read and evaluate a stored identification, then verify raw-body webhook signatures in your backend.

Read and evaluate a stored identification, then verify raw-body webhook signatures in your backend. This guide uses the supported ShieldLabs package API, not a standalone generated client.

## Before you start

Python 3.9+ and a Private API Key. Use a virtual environment for the installation.

Register and verify the website domain in your [analytics dashboard](https://app.shieldlabs.ai/). Use credentials from the same domain and environment as the browser check. For a fresh account, start with [Quick Start](/quickstart).

## Get your credentials

Copy the domain's **Private API Key** from **Integration > API keys** and store it as `SHIELDLABS_API_KEY` in your server environment. For webhook verification, also set `SHIELDLABS_WEBHOOK_SECRET` from **Integration > Webhooks**. Never expose either secret to the browser.

## Install the SDK

```bash theme={null}
python -m venv .venv
source .venv/bin/activate
pip install shieldlabs
```

On Windows, activate the virtual environment with `.venv\\Scripts\\activate` instead.

## Add the integration

```python theme={null}
import os
from typing import Optional

from shieldlabs import ShieldLabs, evaluate_identification, webhooks

client = ShieldLabs(api_key=os.environ["SHIELDLABS_API_KEY"])  # Private API Key, sec_...
used_request_ids: set[str] = set()  # use your database or cache in production


def allow_signup(request_id: str) -> bool:
    # 1. Read the identification for the request ID the browser sent with the form.
    #    Scoring is asynchronous, so this waits (up to 10 s by default) for the verdict.
    identification = client.identifications.get(request_id)

    # 2. Evaluate it: missing, reused, stale, rate-limited, automated or dangerous is refused.
    verdict = evaluate_identification(identification, is_replay=lambda rid: rid in used_request_ids)
    if identification is not None:
        used_request_ids.add(identification.request_id)
    return verdict.ok


def on_webhook(raw_body: bytes, signature_header: Optional[str]) -> None:
    # 3. Verify and parse a delivery: the raw body bytes and the X-Shield-Signature header.
    event = webhooks.construct_event(
        raw_body,
        signature_header,
        os.environ["SHIELDLABS_WEBHOOK_SECRET"],  # whsec_...
    )
    print(event.event_type)
```

A runnable FastAPI app that does all of this is in [`examples/`](https://github.com/ShieldLabs-ai/shieldlabs-python/tree/5e07aae906b4adc9514b3c9bbc30f4095f64e417/examples/).

## Test the complete flow

1. Use the Request ID from a real browser check on the same domain as the Private API Key.
2. Confirm the server retrieves that identification and its Risk Score.
3. Test an invalid or missing ID and an unavailable API: none should be treated as a clean identification.
4. For webhooks, test the original raw body with its signature, then change one byte and confirm rejection.

In-memory replay stores in examples are demonstrations, not shared production storage. Claim accepted IDs atomically in a durable database or cache, enforce freshness, authenticate the action, and check the expected domain and user association. Reading a valid identification alone does not authorize a business action.

## Troubleshooting

* No History row: confirm the registered domain, credential/environment match and that the browser remained open while collectors posted.
* Missing or pending verdict: scoring is asynchronous. The server helper waits within a bounded budget; handle a missing result and API errors explicitly.
* Authentication error: use the Private API Key for History, not a Public Key, Management Secret Key or MCP OAuth token.
* Invalid webhook signature: verify the original raw bytes with the endpoint's full signing secret, before trusting parsed JSON.

## Next steps

* [Runnable example](https://github.com/ShieldLabs-ai/shieldlabs-python/tree/5e07aae906b4adc9514b3c9bbc30f4095f64e417/examples)
* [SDK reference and changelog](https://github.com/ShieldLabs-ai/shieldlabs-python)
* [Identification flow](/api/identification-flow)
* [Server API](/api/server-api)
* [Webhook setup](/setup/webhooks)
* [Content Security Policy](/setup/csp)


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