Skip to main content
This example demonstrates how to integrate JWT middleware with your custom FastAPI application and then add AgentOS functionality on top.

Code

custom_fastapi_jwt.py

Usage

1

Set up your virtual environment

2

Set Environment Variables

3

Install dependencies

4

Setup PostgreSQL Database

5

Run Example

6

Test Authentication Flow

Step 1: Login to get JWT token
Step 2: Test protected endpoints with token
Step 3: Test without token (should get 401)
7

Test on a browser

  1. Visit the API docs: http://localhost:7777/docs
  2. Login via form: Try the /auth/login endpoint with username=demo and password=password
  3. Copy the token: From the response, copy the access_token value
  4. Authorize in docs: Click the “Authorize” button and paste Bearer <your-token>
  5. Test protected endpoints: Try any AgentOS endpoint - they should now work

Authentication Flow

1

User Login

Client sends credentials to /auth/login:
2

Token Generation

Server validates credentials and returns JWT:
3

Authenticated Requests

Client includes token in Authorization header:
4

Middleware Validation

JWT middleware validates token and allows/denies access

Developer Resources