FastAPI + Grantex

Grantex Middleware for FastAPI

Protect your FastAPI endpoints with Grantex grant tokens. Dependency-injection middleware that verifies JWTs, enforces scopes, and identifies agents.

The Problem

Without Grantex

Your FastAPI endpoints accept bearer tokens with no scope enforcement. Any valid token accesses any route. No way to identify which AI agent made the request.

With Grantex

Grantex verifies JWT signatures with the configured JWKS URI, enforces scopes per route, and provides typed agent identity context.

How Grantex Works with FastAPI

Install the middleware

pip install grantex-fastapi — integrates with FastAPI dependency injection.

Add the dependency

Use GrantexAuth as a dependency and auth.scopes() for required scopes.

JWKS verification

The current Python verifier retrieves the configured JWKS during verification; account for that network dependency.

Access agent identity

The dependency returns the full grant context: agent DID, scopes, principal, grant ID.

Verification support

Configurable JWKS retrieval, scope enforcement, and typed authorization errors.

Quick Start

pip install grantex-fastapi
from fastapi import FastAPI, Depends
from grantex import VerifiedGrant
from grantex_fastapi import GrantexAuth

app = FastAPI()
auth = GrantexAuth(
    jwks_uri="https://api.grantex.dev/.well-known/jwks.json"
)

@app.get("/api/emails")
async def list_emails(
    grant: VerifiedGrant = Depends(auth.scopes("email:read"))
):
    print(grant.agent_did)
    print(grant.scopes)
    return {"emails": []}

Why Grantex for FastAPI

FastAPI Dependency Injection

Works with FastAPI's Depends() pattern. Pythonic and type-safe.

FastAPI Dependency

A callable dependency verifies tokens and returns a typed VerifiedGrant.

Per-Route Scopes

auth.scopes() creates a dependency that enforces required permissions and returns 403 on mismatch.

Typed Grant Context

Full Pydantic model for grant context: agent DID, scopes, principal, grant ID.

JWKS URI

Configure the signing-key endpoint used during token verification.

Error Handling

Typed authorization errors distinguish missing, invalid, expired, and insufficient-scope tokens.

Verification, revocation, and audit boundary

This integration enforces authorization only where its wrapper or middleware is installed. Local JWT verification checks signatures and claims after JWKS retrieval; it does not prove current revocation unless the verifier performs an online state check or synchronizes revocation data.

Token verification does not create a consent record or prove that a tool executed. Use the configured authorization flow for grant decisions and add an audit integration at the execution boundary when action records are required.

Ready to secure your FastAPI agents?

Follow the integration guide and verify the package version before deployment. Apache 2.0.