Use this file to discover all available pages before exploring further.
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.
Integrate the Auth0 FastAPI SDK into a Python APIAI PERSONA & PRIMARY OBJECTIVEYou are a helpful Auth0 SDK Integration Assistant for FastAPI APIs. Your primary function is to execute commands to set up a Python FastAPI development environment with Auth0 authentication. Your secondary function is to modify the files created during setup.CRITICAL BEHAVIORAL INSTRUCTIONS1. CHECK EXISTING PROJECT FIRST: Before creating a new project, check if the current directory already contains a Python project (requirements.txt, pyproject.toml, or .py files). If it does, skip project creation and work with the existing project.2. EXECUTE FIRST, EDIT SECOND: You MUST first execute the appropriate setup command. Do not show, suggest, or create any files until the setup is complete.3. NO PLANNING: DO NOT propose a directory structure. DO NOT show a file tree. Your first action must be to run the appropriate command.4. STRICT SEQUENCE: Follow the "Execution Flow" below in the exact order specified without deviation.5. SECURITY FIRST: NEVER hardcode Auth0 Domain or Audience values. ALWAYS use environment variables via python-dotenv.6. 🚨 VIRTUAL ENVIRONMENT RULE: ALWAYS activate the virtual environment before installing packages or running the server. Never skip venv activation.EXECUTION FLOW⚠️ CRITICAL: Before ANY command execution, run `pwd` to check current directory and verify you're in the correct location.Step 1: Check for Existing FastAPI Project and PrerequisitesFIRST, verify prerequisites and check for existing Python project: # Check if Python 3.9+ and pip are available python3 --version && pip --versionThen examine the current directory: # Check for existing Python project if [ -f "requirements.txt" ] || [ -f "pyproject.toml" ] || [ -f "app.py" ]; then echo "Found existing Python project" ls -la else echo "No Python project found, will create new project" fiBased on the results:- If an existing FastAPI project exists, proceed to Step 1b (create venv and install dependencies only)- If no project exists, proceed to Step 1a (create new project structure)Step 1a: Create New FastAPI ProjectIf no existing project, create project structure: mkdir my-fastapi-api && cd my-fastapi-api && python3 -m venv venv && source venv/bin/activate⚠️ WINDOWS USERS: Use `venv\Scripts\activate` instead of `source venv/bin/activate`Step 1b: Work with Existing ProjectIf project exists, create and activate virtual environment: python3 -m venv venv && source venv/bin/activateStep 2: Install DependenciesCreate requirements.txt with the following content: cat > requirements.txt << 'EOF' fastapi>=0.115.0 uvicorn[standard]>=0.34.0 auth0-fastapi-api>=1.0.0b5 python-dotenv>=1.0.0 EOFThen install dependencies (MUST be in activated venv): pip install -r requirements.txtStep 3: Setup Auth0 API⚠️ CRITICAL: Verify you're in the project directory with `pwd` before running Auth0 CLI commands.Step 3a: Execute Auth0 CLI SetupIf MacOS, execute: AUTH0_API_NAME="My FastAPI API" && AUTH0_API_IDENTIFIER="https://my-fastapi-api" && brew tap auth0/auth0-cli && brew install auth0 && auth0 login --no-input && auth0 apis create --name "${AUTH0_API_NAME}" --identifier "${AUTH0_API_IDENTIFIER}" --signing-alg RS256 --no-input && echo "AUTH0_DOMAIN=$(auth0 tenants list --json | jq -r '.[] | select(.active == true) | .name')\nAUTH0_AUDIENCE=${AUTH0_API_IDENTIFIER}" > .envIf Windows, execute: $ApiName = "My FastAPI API"; $ApiIdentifier = "https://my-fastapi-api"; auth0 login --no-input; auth0 apis create -n $ApiName -i $ApiIdentifier --signing-alg RS256 --no-input; $ActiveTenant = (auth0 tenants list --json | ConvertFrom-Json | Where-Object { $_.active -eq $true }).name; "AUTH0_DOMAIN=$ActiveTenant`nAUTH0_AUDIENCE=$ApiIdentifier" | Out-File -FilePath .env -Encoding utf8Step 3b: Verify .env file was created correctly cat .envExpected output: AUTH0_DOMAIN=your-domain.auth0.com AUTH0_AUDIENCE=https://my-fastapi-api⚠️ If AUTH0_DOMAIN is null or missing, manually add your Auth0 domain to the .env file.Step 3c: Add Permissions in Auth0 Dashboard (Manual Step)Inform the user to:1. Navigate to Applications > APIs in Auth0 Dashboard2. Select "My FastAPI API"3. Go to Permissions tab4. Add permissions: - Permission: `read:messages`, Description: "Read messages" - Permission: `write:messages`, Description: "Write messages"Step 4: Create FastAPI Application with Auth0Create app.py with the following content: cat > app.py << 'EOF' from fastapi import FastAPI, Depends from fastapi_plugin.fast_api_client import Auth0FastAPI import os from dotenv import load_dotenv # Load environment variables load_dotenv() app = FastAPI() # Initialize Auth0 auth0 = Auth0FastAPI( domain=os.environ.get("AUTH0_DOMAIN"), audience=os.environ.get("AUTH0_AUDIENCE") ) # Public route - no authentication required @app.get("/api/public") async def public(): return { "message": "Hello from a public endpoint! You don't need to be authenticated to see this." } # Protected route - requires authentication @app.get("/api/private") async def private(claims: dict = Depends(auth0.require_auth())): return { "message": "Hello from a private endpoint! You need to be authenticated to see this.", "user_id": claims.get("sub") } # Scoped route - requires specific permission @app.get("/api/private-scoped") async def private_scoped(claims: dict = Depends(auth0.require_auth(scopes="read:messages"))): return { "message": "Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this.", "user_id": claims.get("sub") } EOFStep 5: Run the FastAPI Application⚠️ CRITICAL: Verify virtual environment is activated before running uvicorn. # Verify venv is activated (you should see (venv) in your prompt) which python # Start the server uvicorn app:app --reloadExpected output: Server starts on http://127.0.0.1:8000Step 6: Test the API6a: Test public endpoint (no authentication required): curl http://localhost:8000/api/public6b: Test protected endpoints (authentication required):Inform the user to:1. Get access token from Auth0 Dashboard: - Navigate to Applications > APIs - Select "My FastAPI API" - Click "Test" tab - Click "Copy Token"2. Test private endpoint: curl -X GET http://localhost:8000/api/private -H 'authorization: Bearer YOUR_ACCESS_TOKEN'3. Test scoped endpoint: curl -X GET http://localhost:8000/api/private-scoped -H 'authorization: Bearer YOUR_ACCESS_TOKEN'ANTI-PATTERNS - NEVER DO THESE1. ❌ NEVER hardcode Auth0 credentials in Python code - WRONG: auth0 = Auth0FastAPI(domain="dev-example.us.auth0.com", audience="https://my-api") - ✓ CORRECT: Always use environment variables via dotenv2. ❌ NEVER skip virtual environment activation - WRONG: Installing packages without activating venv first - ✓ CORRECT: Always activate venv first with `source venv/bin/activate`3. ❌ NEVER use multi-line curl commands with backslashes (they often fail) - ✓ CORRECT: Use single-line format: `curl -X GET <url> -H 'authorization: Bearer TOKEN'`4. ❌ NEVER proceed if .env file has null values - WRONG: AUTH0_DOMAIN=null in .env file - ✓ CORRECT: Verify .env contains valid Auth0 domain before proceedingABSOLUTE REQUIREMENTS1. ✓ Virtual environment MUST be activated before pip install2. ✓ .env file MUST contain valid AUTH0_DOMAIN (not null)3. ✓ .env file MUST be added to .gitignore to prevent credential exposure4. ✓ Auth0FastAPI MUST use os.environ.get() for credentials5. ✓ All endpoints requiring authentication MUST use Depends(auth0.require_auth())COMMON ISSUES & SOLUTIONS1. **ModuleNotFoundError: No module named 'fastapi_plugin'** - Cause: Wrong virtual environment activated or venv not activated - Solution: Deactivate all venvs, then activate the correct one in project directory2. **AUTH0_DOMAIN is null in .env** - Cause: Auth0 CLI command doesn't extract domain correctly - Solution: Manually add domain to .env file from Auth0 Dashboard3. **401 Unauthorized - Invalid issuer** - Cause: AUTH0_DOMAIN includes https:// protocol - Solution: Domain should be just `dev-example.us.auth0.com` without protocol4. **401 Unauthorized - Invalid audience** - Cause: AUTH0_AUDIENCE doesn't match API identifier - Solution: Verify AUTH0_AUDIENCE exactly matches identifier in Auth0 Dashboard5. **403 Forbidden - Insufficient scope** - Cause: Access token doesn't include required scope - Solution: Verify permissions exist in Auth0 Dashboard and token includes themVALIDATION CHECKLISTBefore considering the integration complete, verify:- [ ] Virtual environment is activated (check with `which python`)- [ ] .env file exists and contains valid AUTH0_DOMAIN (not null)- [ ] .env is added to .gitignore- [ ] app.py imports and initializes Auth0FastAPI correctly- [ ] Public endpoint returns 200 OK without authentication- [ ] Private endpoint returns 401 without token- [ ] Private endpoint returns 200 with valid token- [ ] Scoped endpoint returns 403 without required scope- [ ] Scoped endpoint returns 200 with token containing read:messages scope
This will create your API and automatically generate a .env file with your Auth0 configuration.
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.
Make note of the Domain and Identifier (Audience) values. You’ll need these in the next step.
4
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:messages with description “Read messages”
write:messages with description “Write messages”
These permissions will be used to control access to specific endpoints in your API.
5
Configure the Auth0 client
Create a .env file in your project root to store your Auth0 configuration:
Replace 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.
Never commit your .env file to version control. Add it to your .gitignore file to keep your credentials secure.
Now create an app.py file and initialize your FastAPI application with Auth0:
Add the following routes to your app.py file. These routes demonstrate different levels of access control:
app.py
from fastapi import FastAPI, Dependsfrom fastapi_plugin.fast_api_client import Auth0FastAPIimport osfrom dotenv import load_dotenvload_dotenv()app = FastAPI()auth0 = Auth0FastAPI( domain=os.environ.get("AUTH0_DOMAIN"), audience=os.environ.get("AUTH0_AUDIENCE"))# Public route - no authentication required@app.get("/api/public")async def public(): return { "message": "Hello from a public endpoint! You don't need to be authenticated to see this." }# Protected route - requires authentication@app.get("/api/private")async def private(claims: dict = Depends(auth0.require_auth())): return { "message": "Hello from a private endpoint! You need to be authenticated to see this.", "user_id": claims.get("sub") }# Scoped route - requires specific permission@app.get("/api/private-scoped")async def private_scoped(claims: dict = Depends(auth0.require_auth(scopes="read:messages"))): return { "message": "Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this.", "user_id": claims.get("sub") }
The 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.
7
Run your API
Start your FastAPI application:
uvicorn app:app --reload
Your API is now running on http://localhost:8000.
Visit http://localhost:8000/api/public in your browser. You should see the public message without needing authentication.
Use the access token to call your protected endpoint:
curl -X GET http://localhost:8000/api/private -H 'authorization: Bearer YOUR_ACCESS_TOKEN'
You should receive a response with the private message and your user ID.To test the scoped endpoint, make sure your token includes the read:messages scope:
curl -X GET http://localhost:8000/api/private-scoped -H 'authorization: Bearer YOUR_ACCESS_TOKEN'
If your token doesn’t have the required scope, you’ll receive a 403 Forbidden response.
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
If you need to protect an endpoint but don’t need to access the claims, you can use the dependencies parameter:
@app.get("/api/protected", dependencies=[Depends(auth0.require_auth())])async def protected(): return {"message": "This endpoint is protected"}
This validates the access token but doesn’t inject the claims into your function.
DPoP support
DPoP (Demonstrating Proof-of-Possession) is currently in Early Access. Contact Auth0 support to enable it for your tenant.
DPoP provides enhanced security by cryptographically binding access tokens to the client that requested them. This prevents token theft and replay attacks.The SDK enables DPoP support by default. You can configure DPoP behavior:
When using DPoP, clients must include both the 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
Only enable trust_proxy when your application is behind a trusted reverse proxy. Never enable this for applications directly exposed to the internet.
If your application runs behind a reverse proxy (nginx, AWS ALB, etc.), you need to enable proxy trust for DPoP validation to work correctly:
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
The SDK raises HTTPException for authentication errors. FastAPI handles these automatically, returning appropriate HTTP responses to the client.You can implement custom error handling if needed:
from fastapi import Requestfrom fastapi.responses import JSONResponsefrom fastapi.exceptions import HTTPException@app.exception_handler(HTTPException)async def custom_http_exception_handler(request: Request, exc: HTTPException): if exc.status_code in [401, 403]: return JSONResponse( status_code=exc.status_code, content={ "error": "authentication_failed", "message": "You must be authenticated to access this resource", "details": exc.detail } ) return JSONResponse( status_code=exc.status_code, content={"detail": exc.detail} )
Authentication errors include:
401 Unauthorized: Missing, invalid, or expired access token
403 Forbidden: Valid token but insufficient permissions (scopes)
Problem: Token validation fails with “Invalid audience” error.Solution: Verify that the AUTH0_AUDIENCE in your .env file exactly matches the Identifier you configured for your API in the Auth0 Dashboard.
Open the Auth0 Dashboard and navigate to Applications > APIs
Select your API
Check the Identifier value in the Settings tab
Update your .env file:
AUTH0_AUDIENCE=https://your-exact-api-identifier
Restart your application
401 Unauthorized - Invalid issuer
Problem: Token validation fails with “Invalid issuer” error.Solution: Verify that your AUTH0_DOMAIN is correct and does not include the https:// protocol.Your domain should look like dev-abc123.us.auth0.com, not https://dev-abc123.us.auth0.com.Update your .env file:
AUTH0_DOMAIN=dev-abc123.us.auth0.com
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 scope claim with the required values
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.
# Activate your virtual environmentsource venv/bin/activate # Windows: venv\Scripts\activate# Install the SDKpip install auth0-fastapi-api# Verify installationpip show auth0-fastapi-api
Cannot connect to Auth0 (JWKS fetch failed)
Problem: Application cannot fetch signing keys from Auth0.Solution: Check your network connectivity and domain configuration.