A simple FastAPI app that shows how to add secure sign-in with Scalekit (OIDC). You can use it as a starting point or as a reference to integrate enterprise-grade authentication.
What this example includes:
- The app signs users in with Scalekit using the OpenID Connect (OIDC) authorization flow.
- The
/dashboardpage is protected and redirects unauthenticated users to the login flow. - The configuration shows how to register an OAuth 2.0 client and wire login, callback, and logout endpoints.
- The templates use Bootstrap classes so pages render well on desktop and mobile.
- After login, the dashboard displays selected ID token claims to demonstrate how to access user information.
- Python 3.8 or later is installed.
- pip is installed.
- You have a Scalekit account with an OIDC application. Sign up
Pick one method below.
Method A — .env file (recommended for local dev):
Create or update .env in the project root:
# Replace placeholders with your values
SCALEKIT_ENV_URL=https://your-env.scalekit.io
SCALEKIT_CLIENT_ID=YOUR_CLIENT_ID
SCALEKIT_CLIENT_SECRET=YOUR_CLIENT_SECRET
SCALEKIT_REDIRECT_URI=http://localhost:8000/auth/callback
# Optional server config
DEBUG=True
SECRET_KEY=your-secret-key-change-me-in-productionMethod B — environment variables:
export SCALEKIT_ENV_URL=https://your-env.scalekit.io
export SCALEKIT_CLIENT_ID=YOUR_CLIENT_ID
export SCALEKIT_CLIENT_SECRET=YOUR_CLIENT_SECRET
export SCALEKIT_REDIRECT_URI=http://localhost:8000/auth/callbackImportant:
- Never commit secrets to source control.
- Ensure the redirect URI exactly matches what is configured in Scalekit.
# Install dependencies
pip install -r requirements.txt
# Run the application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Or using Python directly:
python -m app.mainThe application will start at http://localhost:8000
To find your required values:
-
Visit Scalekit Dashboard and proceed to Settings
-
Copy the API credentials
- Environment URL (e.g.,
https://your-env.scalekit.dev) - Client ID
- Client Secret
- Environment URL (e.g.,
-
Authentication > Redirect URLs > Allowed redirect URIs:
- Add
http://localhost:8000/auth/callback(no trailing slash) - Optionally add
http://localhost:8000as a post-logout redirect
- Add
| Route | Description | Auth required |
|---|---|---|
/ |
Home page with login option | No |
/login |
Custom login page | No |
/auth/callback |
OIDC callback | No |
/dashboard |
Protected dashboard | Yes |
/sessions |
Session management | Yes |
/sessions/validate-token |
Validate token (POST) | Yes |
/sessions/refresh-token |
Refresh token (POST) | Yes |
/organization/settings |
Protected settings page | Yes (permission) |
/logout |
Logout and end session | Yes |
- Start the app (see Quick start)
- Visit
http://localhost:8000 - Click Sign in with Scalekit
- Authenticate with your provider
- Open the dashboard and then try logout
Stuck? Contact us.
The application uses Python's standard logging. To enable debug logging, set the DEBUG environment variable to True in your .env file:
DEBUG=Truefastapi-scalekit-example/
├── app/ # Main application package
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point
│ ├── config.py # Configuration settings
│ ├── routes.py # API routes and endpoints
│ ├── scalekit_client.py # Scalekit OAuth client
│ ├── dependencies.py # FastAPI dependencies (auth, permissions)
│ └── middleware.py # Token refresh middleware
├── templates/ # Jinja2 HTML templates
│ ├── index.html # Home page
│ ├── login.html # Login page
│ ├── dashboard.html # User dashboard
│ ├── sessions.html # Session management
│ ├── organization_settings.html # Protected settings page
│ ├── error.html # Error page
│ └── permission_denied.html # Permission denied page
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── .gitignore
└── README.md # This file
- FastAPI 0.104+
- scalekit-sdk-python (Official Scalekit Python SDK)
- python-dotenv (for environment variable management)
- jinja2 (for templating)
- uvicorn (ASGI server)
- starlette (web framework, included with FastAPI)
See requirements.txt for exact versions.
This application uses the official Scalekit Python SDK for all authentication operations:
ScalekitClient.get_authorization_url()- Generate OAuth authorization URLScalekitClient.authenticate_with_code()- Exchange code for tokensScalekitClient.validate_access_token_and_get_claims()- Validate tokens and extract permissionsScalekitClient.refresh_access_token()- Refresh expired tokensScalekitClient.get_logout_url()- Generate logout URL
- Read the Scalekit docs: Documentation.
- Read the FastAPI docs: Documentation.
This project is for demonstration and learning. Refer to dependency licenses for production use.