Prefer AI-assisted setup? Use our AI prompts for FastAPI to automatically integrate Civic Auth using Claude, ChatGPT, or other AI assistants. Includes a step-by-step video tutorial!
Quick Start
Important: The SDK Handles EverythingThe Civic Auth Python SDK abstracts away all token validation complexity. You do NOT need to:
- Implement custom middleware for token validation
- Parse or validate JWT tokens manually
- Handle token refresh logic yourself
1. Install Dependencies
uv
package manager:
2. Create Your App with Authentication
Create your FastAPI app with Civic Auth integration:3. Add Basic Routes
4. Add Protected Routes
5. Run Your App
http://localhost:8000
and click the login link to test authentication.
How It Works
Authentication Flow
- User visits
/auth/login
- starts the login process - User authenticates with Civic
- User gets redirected to
/auth/callback
- completes authentication - User can now access protected routes
Available Routes
/auth/login
- Start authentication/auth/callback
- Handle OAuth callback (auto-created)/auth/logout
- Sign out user
Working with User Data
Theget_current_user
dependency returns a dictionary with user information:
Protecting Routes
Two ways to protect routes: Method 1: Usingrequire_auth
dependency
get_current_user
directly
Complete Example
Here’s a complete working FastAPI app:Configuration Options
Field | Required | Description |
---|---|---|
client_id | Yes | Your Civic Auth Client ID from auth.civic.com |
redirect_url | Yes | Where Civic redirects after authentication (must be absolute URL) |
post_logout_redirect_url | Yes | Where users go after logout (must be absolute URL) |
Next Steps
- Get your Client ID: Sign up at auth.civic.com
- Replace
YOUR_CLIENT_ID
with your actual client ID - Update URLs when deploying to production
- Add more protected routes as needed
Authentication Flows
Civic Auth supports multiple OAuth 2.0 authentication methods to provide maximum security for different application architectures.Need client secret authentication? Civic Auth supports PKCE-only, client secrets, and hybrid PKCE + client secret approaches. See our Authentication Flows guide for detailed comparison.