What Are OAuth2 Scopes?
Scopes are permissions that define what a token is allowed to do. For example:me:read- Read user profile informationme:write- Update user profileitems:read- Read itemsitems:write- Create or update items
The format like
items:read is a common convention, but OAuth2 treats scopes as opaque strings. You can use any naming scheme: read_items, items.read, etc.Defining Scopes
First, define your scopes when creating the OAuth2 scheme:/docs, allowing users to see what permissions they can request.
SecurityScopes
FastAPI provides a specialSecurityScopes class to access required scopes in your dependencies:
Token with Scopes
Update your token model to include scopes:Creating Tokens with Scopes
Modifycreate_access_token to include scopes:
Validating Scopes
Updateget_current_user to validate scopes:
1
Build authentication header
Include required scopes in the
WWW-Authenticate header2
Decode token
Extract username and scopes from the JWT
3
Get user
Retrieve user from database
4
Check scopes
Verify the token has all required scopes for this operation
Using Security() with Scopes
Now you can require specific scopes in your endpoints usingSecurity() instead of Depends():
Security() is just like Depends(), but it allows you to specify required scopes. When you use Security() with scopes, FastAPI automatically passes them to your dependency through SecurityScopes.Scope Hierarchy
You can also have dependencies that require different scopes:read_own_items requires both me and items scopes because:
read_own_itemsrequiresitemsscopeget_current_active_userrequiresmescope- FastAPI combines all required scopes:
["me", "items"]
Complete Example with Scopes
Click to see full code
Click to see full code
Testing with Scopes
1
Login with specific scopes
In the
/docs interface, click “Authorize” and you’ll see checkboxes for each scope. Select the scopes you want.Or with cURL:2
Test with full permissions
With both
me and items scopes, you can access all endpoints:3
Test with limited permissions
Get a token with only Now you can access
me scope:/users/me/ but not /users/me/items/:OAuth2 Scopes in OpenAPI
When you define scopes, they automatically appear in your OpenAPI documentation:- The “Authorize” dialog shows checkboxes for each scope
- Each endpoint shows which scopes it requires
- The OpenAPI JSON includes the security requirements
Advanced Scope Patterns
Resource-Specific Scopes
Role-Based Scopes
Combining Scopes
You can require multiple scopes for a single endpoint:Best Practices
Principle of Least Privilege
Request only the scopes you need. Don’t request all scopes by default.
Clear Scope Names
Use descriptive scope names that clearly indicate what permissions they grant.
Document Scopes
Provide clear descriptions for each scope in your OAuth2 scheme.
Validate on Server
Always validate scopes on the server. Never trust client-side validation.
Summary
You now have a complete, production-ready authentication system with: ✅ OAuth2 password flow ✅ JWT tokens with expiration ✅ Secure password hashing with Argon2 ✅ Fine-grained permissions with scopes ✅ Automatic OpenAPI documentation ✅ Type-safe dependenciesFurther Reading
HTTP Basic Auth
Learn about simpler HTTP Basic authentication
API Keys
Alternative authentication with API keys