Code
custom_fastapi_jwt.py
Usage
1
Create a virtual environment
Open the
Terminal
and create a python virtual environment.2
Set Environment Variables
3
Install libraries
4
Setup PostgreSQL Database
5
Run Example
6
Test Authentication Flow
Step 1: Login to get JWT tokenStep 2: Test protected endpoints with tokenStep 3: Test without token (should get 401)
7
Test on a browser
- Visit the API docs: http://localhost:7777/docs
- Login via form: Try the
/auth/login
endpoint withusername=demo
andpassword=password
- Copy the token: From the response, copy the
access_token
value - Authorize in docs: Click the “Authorize” button and paste
Bearer <your-token>
- 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