Skip to main content
The Spidra FastAPI integration (spidra-fastapi) brings the full Spidra API into FastAPI with idiomatic dependency injection, native async/await, a pre-built APIRouter that auto-documents itself in Swagger UI, HMAC-verified webhook dependencies, and a global proxy for cases where DI feels like overhead.

Installation

Requires Python 3.9+ and FastAPI 0.100+.
Get your API key from app.spidra.io under API Keys. Store it as an environment variable. Never hardcode it.

Configuration

Set environment variables before starting your app:

Two integration styles

spidra-fastapi offers two approaches. Both work, and you can mix them freely. get_async_spidra and get_spidra are ready-made FastAPI dependencies. They create (or reuse) a scoped client on each request. This is the idiomatic FastAPI approach and makes unit testing easy — just override the dependency.

Option B — Global proxy

For scripts, background tasks, or simple routes where passing DI is cumbersome, import the global spidra proxy directly:
spidra.async_client returns the async Spidra client. spidra.scrape(...), spidra.search(...), etc. are also available as synchronous shortcuts on the proxy.

Scraping

For structured extraction, add a schema:
to_dict() recursively converts Spidra dataclasses and nested objects to plain dicts, ready to return from FastAPI routes.

Searching

Available sources: "web", "news", "images", "videos", "research", "developer". Defaults to ["web"].

Crawling

For long crawls, submit without waiting:

Mapping

Batch scraping

Parsing documents

Webhooks

Spidra sends signed webhook events to your endpoint. spidra-fastapi ships two webhook dependencies.

verify_spidra_webhook_json — validated JSON dict

The simplest approach. Validates the HMAC-SHA256 signature from X-Spidra-Signature and returns the parsed payload:

verify_spidra_webhook — raw bytes

When you need the raw body (e.g. to re-verify yourself or pass it elsewhere):
Both dependencies return HTTPException(401) automatically if the signature is missing or invalid.

Event listeners

Register handlers that fire when spidra.dispatch_event() is called:

Pydantic webhook model

SpidraWebhookEvent is a Pydantic model for strongly typed webhook payloads:

Advanced dependency injection

Use SpidraDependency to configure a scoped client with specific credentials, useful when handling multiple Spidra accounts in the same app:
dep.get_async() returns an AsyncSpidra client; dep.get_sync() returns a synchronous Spidra client.

Pre-built APIRouter

Mount all Spidra REST endpoints into your app in one line, auto-documented in OpenAPI/Swagger:
Visit http://localhost:8000/docs to see the full interactive Swagger documentation. Or use the default pre-built router:
The router exposes:

Error handling

All Spidra exceptions inherit from SpidraError and are re-exported from spidra_fastapi. Register them as exception handlers:
See the Python SDK for the full exception hierarchy.

Flask

Flask extension with Blueprint routing, flask spidra CLI, and HMAC webhook decorator.

Django

Django integration with management commands, signals, and built-in URL routing.