AuthMiddleware to your own FastAPI app, then pass the app to AgentOS as base_app. The middleware covers your routes and the AgentOS routes.
AuthMiddleware was named JWTMiddleware before v2.7. JWTMiddleware still works as an alias.Code
custom_fastapi_jwt.py
Usage
Test Authentication Flow
Step 1: Login to get JWT tokenStep 2: Test protected endpoints with tokenStep 3: Test without token (should get 401)
Test on a browser
- Visit the API docs: http://localhost:7777/docs
- Login via form: Try the
/auth/loginendpoint withusername=demoandpassword=password - Copy the token: From the response, copy the
access_tokenvalue - Authorize in docs: Click the “Authorize” button and paste the token. Swagger adds the
Bearerprefix for you. - Test protected endpoints: Try any AgentOS endpoint - they should now work