FASTAPI INTEGRATION

Build typed screenshot jobs with FastAPI and async Python

Use Pydantic for application input, resolve approved destinations on the server, and call Snapshot Site with an async HTTP client. Move large batches and retries into a worker rather than holding a request open.

Pydantic
Validated input
Async HTTP
Non-blocking request
Workers
Durable batches
Install:pip install fastapi httpx
Auth:SNAPSHOT_SITE_API_KEY
Get started for free
FastAPI screenshot workflow
Snapshot Site themed FastAPI screenshot API illustration
Good fits
Python APIs generating visual evidence
Analysis services pairing screenshots with data pipelines
Background workers capturing URL inventories
Internal tools exposing typed capture jobs

Let FastAPI validate business input before network work

The request model should describe an authorized application action. Destination policy and provider credentials stay inside the service.

1

Typed request

Accept a page identifier or constrained options rather than a private provider payload.

2

Async transport

Reuse an HTTP client with explicit timeouts and response validation.

3

Failure mapping

Separate caller errors, provider errors, target readiness, and artifact storage.

4

Background execution

Use a durable queue for work that needs retries, batching, or status history.

Implementation workflow

Add a typed FastAPI capture route

1

Inject the key through deployment secret management

2

Define a Pydantic model for application-owned input

3

Resolve the page ID and make the async provider request

4

Return a job or stored asset record with normalized status

FastAPI screenshot API example

Python

Call Snapshot Site with HTTPX

The model accepts a known page key and the server constructs the provider request.

import os
import httpx
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

app = FastAPI()
PAGES = {
    "pricing": "https://example.com/pricing",
    "docs": "https://example.com/docs",
}

class CaptureRequest(BaseModel):
    page_id: str

@app.post("/captures")
async def create_capture(payload: CaptureRequest):
    url = PAGES.get(payload.page_id)
    if not url:
        raise HTTPException(status_code=400, detail="Unknown page")

    async with httpx.AsyncClient(timeout=45) as client:
        response = await client.post(
            "https://api.prod.ss.snapshot-site.com/api/v1/screenshot",
            headers={"x-snapshotsiteapi-key": os.environ["SNAPSHOT_SITE_API_KEY"]},
            json={
                "url": url,
                "format": "webp",
                "width": 1440,
                "height": 900,
                "fullSize": True,
            },
        )

    response.raise_for_status()
    return response.json()

Type the product request, not the provider payload

A public FastAPI model should express what the user is allowed to do. A report ID, template name, or approved route is safer and clearer than forwarding an unrestricted URL and every provider option. The service resolves that input and applies its own capture policy.

Pydantic validation catches shape errors before outbound work, but business authorization and destination safety still require application logic.

Use async I/O with explicit limits

An async HTTP client prevents the worker thread from blocking while waiting on network I/O. Configure connection and response timeouts intentionally, reuse clients where the application lifecycle permits, and check status before parsing success fields.

Do not automatically retry every timeout. A target that never reaches readiness needs investigation or a changed specification, while a temporary transport failure may justify bounded retry.

Choose a durable execution model

FastAPI request handlers can suit immediate, low-volume capture. A queue such as the one already used by your application is better for URL inventories, scheduling, attempts, and recovery after deployment. The queue owns logical job identity; the API exposes status.

Store source identity, specification, capture time, result location, and normalized failure together. Protect screenshots according to their source, especially when they feed analysis or support workflows.

Use the Python SDK for a package-oriented client and API timeout guidance to distinguish request, target, and job budgets.

Screenshot API for FastAPI FAQ

Can FastAPI call the screenshot API asynchronously?

Yes. Use an async HTTP client with an explicit timeout and validate the response before returning application data.

Where should the API key be stored?

Inject it through a protected environment or secret manager available only to the server or worker.

Should I use FastAPI BackgroundTasks?

They can suit small noncritical follow-up work, but a durable queue is safer for retries, batches, restarts, and jobs that need status history.

How should URLs be validated?

Prefer server-resolved entity IDs. For raw input, enforce allowed protocols and destinations and apply network safety controls.

Can Pydantic expose all provider fields to clients?

Keep the public model limited to product needs. The server should own defaults and prevent callers from bypassing policy.

How should timeouts be handled?

Use separate, explicit transport and job budgets. Classify target slowness and provider failure rather than retrying every timeout.

Can a screenshot enter an AI pipeline?

Yes, after validating and storing the artifact under appropriate access and data policy. Keep generated interpretation reviewable.

What should an API response return?

Return an application job or asset identifier and normalized status rather than leaking secrets or unnecessary provider internals.

Create one typed FastAPI capture job

Resolve a known page, apply a fixed specification, and verify timeout and storage behavior before enabling batches.