AI Prompt
AI Prompt
Using AI to integrate Auth0? Add this prompt to Cursor, Windsurf, Copilot, Claude Code or your favourite AI-powered IDE to speed up development.
This quickstart requires:
- Python 3.9 or higher
- pip package manager
- jq - Required for Auth0 CLI setup
- Familiarity with FastAPI
Create a new FastAPI project
Create a new directory for your FastAPI project and set up a virtual environment.
Install dependencies
Create a Install the dependencies:
requirements.txt file with the following dependencies:requirements.txt
Setup your Auth0 API
You’ll need to create an Auth0 API to represent your FastAPI application.Make note of the Domain and Identifier (Audience) values. You’ll need these in the next step.
- Dashboard
- CLI
- Navigate to Applications > APIs in the Auth0 Dashboard
- Click Create API
- Provide a Name for your API (e.g., “My FastAPI API”)
- Set the Identifier to your API identifier (e.g.,
https://my-fastapi-api) - Leave the Signing Algorithm as RS256
- Click Create
The Identifier is a unique identifier for your API. It’s recommended to use a URL, but it doesn’t have to be a publicly accessible URL—Auth0 won’t call it. This value cannot be modified afterwards.
Define API permissions
Permissions (also known as scopes) allow you to define how your API can be accessed. You can create permissions for your API in the Auth0 Dashboard.
- In the Auth0 Dashboard, navigate to your API’s Permissions tab
- Add the following permissions:
read:messageswith description “Read messages”write:messageswith description “Write messages”
Configure the Auth0 client
Create a Replace
.env file in your project root to store your Auth0 configuration:.env
YOUR_AUTH0_DOMAIN with your Auth0 domain (e.g., dev-abc123.us.auth0.com) and YOUR_API_IDENTIFIER with the identifier you set when creating your API.Now create an app.py file and initialize your FastAPI application with Auth0:app.py
Create protected routes
Add the following routes to your The
app.py file. These routes demonstrate different levels of access control:app.py
require_auth() method validates the access token sent in the Authorization header. When called with a scopes parameter, it also verifies that the token contains the specified permission.Test your API
To test the protected endpoints, you’ll need to obtain an access token from Auth0.Get an access token
The easiest way to get an access token for testing is through the Auth0 Dashboard:- Navigate to Applications > APIs in the Auth0 Dashboard
- Select your API
- Click the Test tab
- Click Copy Token in the Asking Auth0 for tokens from my application section
Call your API
Use the access token to call your protected endpoint:read:messages scope:
Advanced Usage
Validate custom claims
Validate custom claims
You can access custom claims that have been added to the access token through Auth0 Actions.Access custom claims in your route handler:To add custom claims to your access tokens, create an Auth0 Action:
- Navigate to Actions > Library in the Auth0 Dashboard
- Click Create Action
- Select Build from scratch
- Name your action and select the Login / Post Login trigger
- Add your custom claims:
- Click Deploy and add the action to your Login flow
Custom claims must use a namespaced format (e.g.,
https://myapp.example.com/claim_name) to avoid conflicts with standard claims.Protect endpoints without using claims
Protect endpoints without using claims
If you need to protect an endpoint but don’t need to access the claims, you can use the This validates the access token but doesn’t inject the claims into your function.
dependencies parameter:DPoP support
DPoP support
DPoP (Demonstrating Proof-of-Possession) is currently in Early Access. Contact Auth0 support to enable it for your tenant.
Authorization: DPoP <token> and DPoP: <proof> headers. The SDK automatically validates the DPoP proof and binds it to the access token.Configure for reverse proxy
Configure for reverse proxy
If your application runs behind a reverse proxy (nginx, AWS ALB, etc.), you need to enable proxy trust for DPoP validation to work correctly:Configure your reverse proxy to forward the necessary headers:This is essential for DPoP validation because the SDK needs to match the exact URL the client used. Without proxy trust, your application sees internal URLs while DPoP proofs reference external URLs, causing validation failures.
Error handling
Error handling
The SDK raises Authentication errors include:
HTTPException for authentication errors. FastAPI handles these automatically, returning appropriate HTTP responses to the client.You can implement custom error handling if needed:- 401 Unauthorized: Missing, invalid, or expired access token
- 403 Forbidden: Valid token but insufficient permissions (scopes)
Common Issues
401 Unauthorized - Invalid audience
401 Unauthorized - Invalid audience
401 Unauthorized - Invalid issuer
401 Unauthorized - Invalid issuer
403 Forbidden - Insufficient scope
403 Forbidden - Insufficient scope
Problem: Protected endpoint returns 403 even with a valid access token.Solution: The access token doesn’t include the required scope.
- Check what scopes your endpoint requires
- When requesting a token, ensure you include the required scopes
- Verify the scope exists in your API’s Permissions tab in the Auth0 Dashboard
- Decode your token at jwt.io to verify it contains the
scopeclaim with the required values
ModuleNotFoundError: No module named 'fastapi_plugin'
ModuleNotFoundError: No module named 'fastapi_plugin'
Problem: Python cannot find the Auth0 FastAPI SDK.Solution: Ensure the SDK is installed in your active virtual environment.
Cannot connect to Auth0 (JWKS fetch failed)
Cannot connect to Auth0 (JWKS fetch failed)
Problem: Application cannot fetch signing keys from Auth0.Solution: Check your network connectivity and domain configuration.
-
Verify your domain is accessible:
-
Check that your firewall allows outbound HTTPS (port 443) connections to
*.auth0.com -
If behind a corporate proxy, configure the
HTTP_PROXYandHTTPS_PROXYenvironment variables
DPoP validation fails
DPoP validation fails
Problem: DPoP authentication returns errors about URL or proof validation.Solution:
-
If behind a reverse proxy, enable proxy trust:
-
Verify your proxy forwards these headers:
X-Forwarded-ProtoX-Forwarded-HostX-Forwarded-Prefix
- Ensure DPoP is enabled for your tenant (contact Auth0 support)
-
Verify the DPoP proof
htuclaim matches your request URL exactly
Next Steps
SDK Documentation
Explore the Auth0 FastAPI SDK on GitHub for advanced configuration and examples
Scopes and Permissions
Learn how to define and use scopes for fine-grained access control
Auth0 Actions
Customize your authentication flow and add custom claims to tokens
FastAPI Documentation
Learn more about FastAPI features, async patterns, and best practices
API Authorization
Implement role-based access control (RBAC) for your API
Deploy to Production
Best practices for deploying FastAPI applications with Auth0