Automatic Swagger UI authentication for FastAPI development
Project description
FastAPI Swagger Auth
Automatically authenticate Swagger UI in FastAPI during development - solving the pain of manually copying JWT tokens every time you want to test endpoints.
Features
- Automatic Authentication - Swagger UI auto-injects JWT tokens
- Zero Configuration - Works out of the box with sensible defaults
- Multiple Providers - Supabase or custom JWT (extensible for any auth service)
- Security-First - Only activates in development mode
- Auto-Refresh - Tokens refresh automatically before expiry
- Type-Safe - Full type hints for better IDE support
Installation
pip install fastapi-swagger-auth
With Supabase support:
pip install fastapi-swagger-auth[supabase]
Quick Start
Basic Usage (Custom JWT)
from fastapi import FastAPI
from fastapi_swagger_auth import SwaggerAuthDev
app = FastAPI(debug=True)
# One-line setup
SwaggerAuthDev(
app,
auth_provider="custom",
dev_credentials={
"email": "admin@dev.local",
"sub": "user_123",
}
)
Supabase Provider
SwaggerAuthDev(
app,
auth_provider="supabase",
dev_credentials={
"email": "admin@dev.local",
"password": "devpass123",
"supabase_url": "https://your-project.supabase.co",
"supabase_key": "your-anon-key",
}
)
Custom Token Getter
def get_my_token():
# Your custom logic to get a token
return "my-jwt-token"
SwaggerAuthDev(app, token_getter=get_my_token)
Custom Provider
from fastapi_swagger_auth.providers.base import AuthProvider
class MyProvider(AuthProvider):
async def get_token(self, credentials: dict) -> str:
# Your auth logic
return "jwt-token"
async def refresh_token(self, current_token: str) -> str:
# Your refresh logic
return "new-jwt-token"
def get_token_expiry(self, token: str) -> int:
# Parse token and return seconds until expiry
return 3600
SwaggerAuthDev(
app,
provider_instance=MyProvider(),
dev_credentials={"email": "admin@dev.local", "password": "secret"}
)
Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
app |
FastAPI |
Required | FastAPI application instance |
auth_provider |
str |
"custom" |
Provider type: "custom" or "supabase" |
dev_credentials |
dict |
None |
Credentials for authentication |
token_getter |
callable |
None |
Custom function to get token (overrides provider) |
auto_refresh |
bool |
True |
Enable automatic token refresh |
enabled |
bool |
None |
Explicitly enable/disable (None = auto-detect from debug mode) |
provider_instance |
AuthProvider |
None |
Custom provider instance |
How It Works
- Initialization - SwaggerAuthDev validates environment (dev mode only by default)
- Authentication - Selected provider authenticates using
dev_credentials - Token Injection - Custom Swagger UI HTML injects JWT via
requestInterceptor - Auto-Refresh - Token automatically refreshes before expiry (optional)
Security
- Development Only - Only activates when
app.debug=Trueby default - No Production Risk - Explicitly enable with
enabled=Trueif needed - Clear Logging - Debug messages show authentication status
- Graceful Fallback - Shows instructions if authentication fails
Examples
See the examples/ directory for complete working examples:
basic_usage.py- Simple custom JWT providersupabase_example.py- Supabase authenticationcustom_provider.py- Custom provider implementation
Development
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Lint
ruff check .
# Format
ruff format .
FastAPI Swagger Auth eliminates this workflow entirely. Similar to Swashbuckle in .NET, it's a development tool that just works.
License
MIT License - see LICENSE file for details.
Contributing
Contributions welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_swagger_auth-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_swagger_auth-0.1.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdb5f098e1226c01b13597ab5a3dd23a700063ec90d9a8b933538645cfac0326
|
|
| MD5 |
a5a8a47cf4d0661312e35e2b5e35ed1c
|
|
| BLAKE2b-256 |
b60fc0e32ecd6df70b9ffc0cf004be8d097aed5732d7894a8f01befbe5e27acd
|
Provenance
The following attestation bundles were made for fastapi_swagger_auth-0.1.0.tar.gz:
Publisher:
publish.yml on maxfallstrom/fastapi-swagger-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_swagger_auth-0.1.0.tar.gz -
Subject digest:
cdb5f098e1226c01b13597ab5a3dd23a700063ec90d9a8b933538645cfac0326 - Sigstore transparency entry: 582755347
- Sigstore integration time:
-
Permalink:
maxfallstrom/fastapi-swagger-auth@3017d272525831f3cad7a9752df21b0521ff6136 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/maxfallstrom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3017d272525831f3cad7a9752df21b0521ff6136 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_swagger_auth-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_swagger_auth-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7adb7aca5e377bd1aa468ded49390ee751fa2db7761266c7f7d0a19791eb2278
|
|
| MD5 |
1f07dd9b41b8206f41dae947160ace79
|
|
| BLAKE2b-256 |
3c6c676e06631ed647d064726b2ba1d2435bc9697741d0ca2b15fe53e252ec5e
|
Provenance
The following attestation bundles were made for fastapi_swagger_auth-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on maxfallstrom/fastapi-swagger-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_swagger_auth-0.1.0-py3-none-any.whl -
Subject digest:
7adb7aca5e377bd1aa468ded49390ee751fa2db7761266c7f7d0a19791eb2278 - Sigstore transparency entry: 582755349
- Sigstore integration time:
-
Permalink:
maxfallstrom/fastapi-swagger-auth@3017d272525831f3cad7a9752df21b0521ff6136 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/maxfallstrom
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3017d272525831f3cad7a9752df21b0521ff6136 -
Trigger Event:
release
-
Statement type: