Prerequisites: Before you begin, ensure you have the following installed:
- Python and pip
- Your preferred code editor
Get Started
This guide demonstrates how to integrate Auth0 with any new or existing Python API built with Flask.1
Create a new Flask project
Create a new directory for your Flask API:Create a virtual environment and activate it:
2
Install dependencies
Create a Install the dependencies:
requirements.txt file with the following dependencies:requirements.txt
3
Setup your Auth0 API
Next up, you need to create a new API on your Auth0 tenant and configure your application.Alternatively, you can read our getting started guide that helps you set up your first API through the Auth0 dashboard.You can do this manually via the Dashboard or use the Auth0 CLI:
- Dashboard
- CLI
- Go to the Auth0 Dashboard → Applications → APIs
- Click Create API
- Enter your API details:
- Name:
My Flask API - Identifier:
https://my-flask-api(this will be your audience) - Signing Algorithm: RS256
- Name:
- Click Create
- Copy your Domain from the Dashboard (found under Applications → Applications → [Your App] → Settings)
- Copy the Identifier you just created (this is your audience)
Your Domain should not include
https:// - use only the domain name (e.g., your-tenant.auth0.com).The Audience (API Identifier) is a unique identifier for your API and can be any valid URI.4
Define API permissions
Configure permissions (scopes) for your API to control access to specific resources:
- In the Auth0 Dashboard, navigate to Applications → APIs
- Select your API (
My Flask API) - Go to the Permissions tab
- Click Add Permission
- Add the following permission:
- Permission (Scope):
read:messages - Description:
Read messages
- Permission (Scope):
- Click Add
Permissions define what actions can be performed on your API. You can add multiple permissions like
write:messages, delete:messages, etc. The /api/private-scoped endpoint in this quickstart requires the read:messages permission.5
Configure the Auth0 client
If you used the CLI method in Step 3, your
.env file was automatically created. Skip to creating the app.py file below..env file in your project root to store your Auth0 configuration:.env
server.py file and import the required dependencies:server.py
6
Create authentication functions
Add functions to extract and validate the access token:
server.py
7
Create API endpoints
Add the public and protected endpoints:
server.py
8
Run your API
Start your Flask application:Your API is now running (check your console output for the exact URL, typically
http://localhost:3010).CheckpointYou should now have a fully functional Auth0-protected Flask API running on your localhost with three endpoints:
GET /api/public- Accessible without authenticationGET /api/private- Requires a valid Auth0 access tokenGET /api/private-scoped- Requires authentication and theread:messagesscope
Test Your API
To test your protected endpoints, you need an access token.Get a test token
- Go to the Auth0 Dashboard
- Navigate to Applications → APIs
- Select your API
- Go to the Test tab
- Copy the access token
For testing the scoped endpoint (
/api/private-scoped), you’ll need to define the read:messages permission in your API settings under the Permissions tab, and ensure it’s granted in your access token.Make a request
Test the public endpoint (no token required):YOUR_ACCESS_TOKEN with the token you copied from the Auth0 Dashboard.
Advanced Usage
Custom Error Handling
Custom Error Handling
Customize the AuthError handler to provide more detailed error responses:
Using Environment-Based Configuration
Using Environment-Based Configuration
Manage different configurations for development and production:
Accessing User Claims
Accessing User Claims
After successful authentication, access user information from the token:
Docker Deployment
Docker Deployment
Deploy your API using Docker (based on the sample repository):Create a Build and run:
Dockerfile:Multiple Scope Validation
Multiple Scope Validation
Check for multiple required scopes:
CORS Configuration
CORS Configuration
Configure CORS for specific origins:
Error Handling Best Practices
Error Handling Best Practices
Implement comprehensive error handling with specific error types:
Common Issues
401 Unauthorized - Invalid audience
401 Unauthorized - Invalid audience
401 Unauthorized - Invalid issuer
401 Unauthorized - Invalid issuer
Configuration values not found
Configuration values not found
Symptom:
None values or environment variable errorsCause: Environment variables not loaded or .env file not foundSolution:- Ensure
.envfile exists in your project root - Verify
load_dotenv()is called before accessing environment variables - Check that variable names match exactly:
AUTH0_DOMAINandAPI_IDENTIFIER
Token expired errors
Token expired errors
Symptom:
jwt.ExpiredSignatureError: Token is expiredCause: The access token has passed its expiration timeSolution:- Request a new token from the Auth0 Dashboard Test tab
- Implement token refresh in your client application
- Tokens from the Dashboard are typically valid for 24 hours
Missing Authorization header
Missing Authorization header
Invalid header algorithm
Invalid header algorithm
Symptom:
Invalid header. Use an RS256 signed JWT Access TokenCause: Token is signed with HS256 instead of RS256Solution:- Verify your API is configured to use RS256 signing algorithm in the Auth0 Dashboard
- Ensure you’re using an access token, not an ID token
- Check that the token was issued for your API audience
Additional Resources
Sample Repository
Complete working examples with Flask
Flask Documentation
Official Flask framework documentation
Auth0 Dashboard
Manage your Auth0 tenant and APIs
API Authentication Guide
Learn about access tokens and API security
PyJWT Documentation
JWT library documentation for Python
Community Forum
Get help from the Auth0 community