Quick Start
The JWT middleware provides two main features:- Token Validation: Validates JWT tokens and handles authentication
- Parameter Injection: Automatically injects user_id, session_id, and custom claims into endpoint parameters
Configuration Options
Token Sources
The middleware supports three token sources:- Both Sources
Parameter Injection
The middleware automatically injects JWT claims into the request object flowing across your FastAPI state. This is a great way to resolve data from your token into parameters received by your endpoints. These are the parameters automatically injected by our JWT middleware into your endpoints:user_idsession_iddependenciessession_stateFor example, in/agents/{agent_id}/runs, theuser_id,session_id,dependenciesandsession_stateare automatically used if they were extracted from the JWT token.
- Automatically using the
user_idandsession_idfrom your JWT token when running an agent - Automatically filtering sessions retrieved from
/sessionsendpoints byuser_id(where applicable) - Automatically injecting
dependenciesfrom claims in your JWT token into the agent run, which then is available on tools called by your agent
Security Features
Remember to always use strong secret keys, don’t hardcode them anywhere in your code and enable validation in production.
validate=True, the middleware:
- Verifies JWT signature using the secret key
- Checks token expiration (
expclaim) - Returns 401 errors for invalid/expired tokens