Demo project
Check out the code folder under demo_project for a complete example.
The demo project will show:
- Authentication with Zitadel OpenID Connect
- Role-based access control for protected endpoints
- Scope-based authorization for API endpoints
- Service user authentication via JWT
- Swagger UI with OAuth2 integration
Starting the FastAPI server
- Make sure to have
dev
dependencies installed:uv sync --group dev
(see Contributing). - Run the demo server using
uv
:
- The server should start at http://localhost:8001.
Login
User types in Zitadel
Zitadel differentiates two types of users:
- Users ("human users", i.e. people with a login)
- Service users ("machine users", i.e. integration bots).
User login
- Navigate to http://localhost:8001/docs.
- Click on the Authorize button in the top right corner.
- Click on the Authorize button in the modal.
- You should be redirected to the Zitadel login page.
- Log in with your Zitadel credentials.
- You should be redirected back to the FastAPI docs page.
- You can now try out the endpoints in the docs page.
- If you encounter issues, try again in a private browsing window.
Service user login
- Set up a service user as described in the setup guide.
- Download the private key from Zitadel.
- Change the config in
demo_project/service_user.py
. - Run the service user script:
- You should get a response similar to this:
{
"message": "Hello world!",
"user": {
"claims": {
"aud": [
"..."
],
"client_id": "...",
"exp": 1739406574,
"iat": 1739363374,
"iss": "https://myinstance.zitadel.cloud",
"sub": "...",
"nbf": 1739363374,
"jti": "...",
"project_roles": {
"admin": {
"1234567": "hello.xyz.zitadel.cloud"
}
}
},
"access_token": "eyJhbGciO... (truncated)"
}
}