Skip to content

Repository files navigation

Welcome to the FastAPI APIKey Authentication Documentation!

License PyPI Release Pylint Score Supported Python Versions Pre-commit Open Issues Last Commit Languages Coverage

fastapi-apikey-auth is a FastAPI package developed by Lazarus that provides robust API key authentication and management for FastAPI applications.

Designed for simplicity and flexibility, it enables developers to secure API endpoints with API keys, enforce rate limiting, and manage key expiration. The package integrates seamlessly with FastAPI's dependency injection system and SQLAlchemy's async ORM, while offering optional caching for performance optimization.

Key features include a customizable header-based authentication mechanism, user-associated API keys, and a modern route for displaying keys in a sleek, animated template.

Whether you're building a public API or an internal service, fastapi-apikey-auth offers a lightweight yet powerful solution for API security and access control.

It is a direct port of dj-apikey-auth, so projects moving from Django to FastAPI keep the same settings, the same endpoints and the same response shapes.

Project Detail

  • Language: Python >= 3.9
  • Framework: FastAPI >= 0.110
  • SQLAlchemy: >= 2.0 (async)
  • Pydantic: >= 2.5

Documentation Overview

The documentation is organized into the following sections:

  • Quick Start: Get up and running quickly with basic setup instructions.
  • Authentication and Permissions: Learn how to secure your API using API key authentication and manage access control with the APIKeyAuthentication dependency and the HasAPIKey permission (optional use case).
  • API Guide: Detailed information on available APIs and endpoints.
  • Usage: How to effectively use the package in your projects.
  • Settings: Configuration options and settings you can customize.
  • Coming from dj-apikey-auth: A mapping between the Django package and this one.

Quick Start

This section provides a fast and easy guide to getting the fastapi-apikey-auth package up and running in your FastAPI project. Follow the steps below to quickly set up the package and start using the package.

1. Install the Package

Option 1: Using pip (Recommended)

Install the package via pip:

$ pip install fastapi-apikey-auth

Option 2: Using Poetry

If you're using Poetry, add the package with:

$ poetry add fastapi-apikey-auth

Option 3: Using pipenv

If you're using pipenv, install the package with:

$ pipenv install fastapi-apikey-auth

2. Install an async database driver

The package talks to your database through SQLAlchemy's async engine, so you need a driver that supports it. For PostgreSQL:

$ pip install asyncpg

For SQLite:

$ pip install aiosqlite

3. Create the table

The package ships a declarative Base carrying the apikey_auth_api_key table. Create it either through your migration tool of choice (Alembic) or directly at startup:

from fastapi_apikey_auth import Base

async with engine.begin() as connection:
    await connection.run_sync(Base.metadata.create_all)

If you use Alembic, point your target_metadata at fastapi_apikey_auth.Base.metadata (alongside your own metadata) and autogenerate a migration.

Note: APIKey.user_id is a plain nullable integer column rather than a foreign key. FastAPI projects own their user table, so the package stays agnostic about it. Add a foreign key in your own migration if you want referential integrity.

4. Wire the package into your app

setup() registers your session dependency, applies your settings, installs the rate limit middleware and mounts every router in one call:

from fastapi import FastAPI
from fastapi_apikey_auth import setup

app = FastAPI()


async def get_db():
    async with SessionLocal() as session:
        yield session


setup(app, get_db)

That mounts:

  • POST|GET|PATCH|PUT|DELETE /apikey_auth/apikey/ — the admin API
  • GET /apikey_auth/my-apikey/ — the user API
  • GET /apikey_auth/api_keys/ — the HTML dashboard

Pass prefix= to mount them somewhere else.

5. Teach the package about your users

An API key can be linked to a user through its user_id. To let the package resolve that id into a real user — for the user object in responses, for the IsAdminUser permission and for the staff throttle rate — register a user loader:

from fastapi_apikey_auth import setup


async def load_user(session, user_id):
    return await session.get(User, user_id)


setup(app, get_db, user_loader=load_user)

The loader may be sync or async, and may return:

  • a Principal instance,
  • a mapping such as {"id": 1, "username": "bob", "is_staff": True},
  • or any object with id, is_active and is_staff (or is_superuser) attributes.

Without a loader the package still works: keys keep their user_id, but nobody is ever staff, so the admin API stays closed.

6. (Optional) Configure API filters

To filter the list endpoints beyond the built-in ordering and search, write a filter class and point the settings at it:

from fastapi_apikey_auth.filtering import BaseAPIKeyFilter
from fastapi_apikey_auth.models import APIKey


class ActiveOnlyFilter(BaseAPIKeyFilter):
    def filter_query(self, statement, params):
        if params.get("active_only") == "1":
            return statement.where(APIKey.is_active.is_(True))
        return statement
APIKEY_AUTH_API_FILTER_CLASS = "path.to.ActiveOnlyFilter"

For more detailed info, refer to the Settings section.


Authentication and Permissions

This section explains how to leverage the APIKeyAuthentication dependency and the HasAPIKey permission in fastapi-apikey-auth to secure your APIs. These components provide flexible and powerful mechanisms for API key-based authentication and access control.

Using APIKeyAuthentication

The APIKeyAuthentication dependency enables API key-based authentication for your FastAPI application. It validates API keys passed in HTTP headers, checks their status (e.g., active, not expired), enforces rate limits, and optionally associates them with a user.

Applying to Specific Routes

FastAPI has no global authentication setting the way DRF does; you attach the dependency where you want it. This works on a single route, on a router, or on the whole application:

from fastapi import Depends, FastAPI
from fastapi_apikey_auth import authentication

app = FastAPI()


@app.get("/secure/")
async def secure(auth=Depends(authentication)):
    return {"message": "This route requires an API key"}

Applying Everywhere

To apply it across every route in your project, attach it at the application or router level:

from fastapi import Depends, FastAPI
from fastapi_apikey_auth import authentication

app = FastAPI(dependencies=[Depends(authentication)])

Rejecting Missing Keys Outright

By default the dependency mirrors DRF: a request without an API key is allowed through unauthenticated, and the permission decides what to do about it. When you want the dependency itself to reject those requests, use the strict variant:

from fastapi import Depends
from fastapi_apikey_auth import required_authentication


@app.get("/secure/")
async def secure(auth=Depends(required_authentication)):
    ...

How It Works

  • Header Extraction: Extracts the API key from the header specified by APIKEY_AUTH_HEADER_NAME (default: Authorization) with an optional prefix from APIKEY_AUTH_HEADER_TYPE (default: None), e.g., header_type <key>.
  • Validation: Checks the key against the APIKey table, ensuring it is active (is_active=True) and not expired (expires_at).
  • Rate Limiting: Enforces APIKEY_AUTH_MAX_REQUESTS (if set), incrementing requests_count and raising a 429 if exceeded.
  • Caching: Optionally caches key lookups using APIKEY_AUTH_USE_CACHING and APIKEY_AUTH_CACHE_TIMEOUT_SECONDS for performance.
  • User Association: Returns an AuthResult carrying api_key and principal, and attaches both to request.state.api_key and request.state.principal.

Important Note:

The user_id field on the APIKey model is nullable. If a valid API key is provided but not linked to a user:

  • request.state.principal will be None.
  • request.state.api_key will still contain the APIKey instance.

This allows authentication of "anonymous" API keys, which is useful for public or shared access scenarios.

Example Usage

Send a request with an API key in the header:

curl -X GET http://your-api.com/endpoint/ -H "Authorization: header_type test-key-123"
  • If test-key-123 is valid and linked to a user, request.state.principal is that user, and request.state.api_key is the APIKey instance.
  • If test-key-123 is valid but not linked to a user, request.state.principal is None, and request.state.api_key is the APIKey instance.
  • If invalid or expired, a 401 Unauthorized is returned.

Using HasAPIKey

The HasAPIKey class is a permission designed to ensure that an API request is authenticated with a valid API key. It complements APIKeyAuthentication by explicitly checking whether request.state.api_key is an APIKey instance.

Implementation

This is how it is implemented:

from fastapi_apikey_auth.models import APIKey
from fastapi_apikey_auth.security import BasePermission, get_api_key


class HasAPIKey(BasePermission):
    def has_permission(self, request, view=None) -> bool:
        return isinstance(get_api_key(request), APIKey)
  • Check: Verifies that the request carries an APIKey instance.
  • Purpose: Grants access only if the request was authenticated with a valid API key via APIKeyAuthentication.

Applying to Routes

Permissions run authentication themselves, so attaching one is enough:

from fastapi import Depends, FastAPI
from fastapi_apikey_auth import HasAPIKey

app = FastAPI()


@app.get("/keys-only/", dependencies=[Depends(HasAPIKey())])
async def keys_only():
    return {"message": "Access granted with API key"}

Critical Insight

HasAPIKey ensures that an API key is present, regardless of whether it's linked to a user. This is distinct from IsAuthenticated, which requires an actual principal. Use both together for APIs requiring a user-linked API key:

from fastapi import Depends, FastAPI, Request
from fastapi_apikey_auth import HasAPIKey, IsAuthenticated
from fastapi_apikey_auth.security import get_principal

app = FastAPI()


@app.get(
    "/user-keys-only/",
    dependencies=[Depends(IsAuthenticated()), Depends(HasAPIKey())],
)
async def user_keys_only(request: Request):
    return {"message": f"Welcome, {get_principal(request).extra['username']}"}
  • IsAuthenticated: Ensures the key resolved to an active principal.
  • HasAPIKey: Ensures the request carries an APIKey.

Key Considerations

  • Nullable User: Since APIKey.user_id is nullable, APIKeyAuthentication authenticates requests even without a user. HasAPIKey allows these "anonymous" API keys, while IsAuthenticated does not.
  • Flexibility: Use HasAPIKey for APIs where only an API key is needed (e.g., public APIs), and combine with IsAuthenticated for user-specific APIs if needed.
  • Rate Limit Headers: When max_requests is set, the RateLimitHeadersMiddleware adds X-RateLimit-Limit and X-RateLimit-Remaining headers to responses, which can be checked regardless of user association.

Example Scenarios

  • API Key Only:

    • Route requires any valid API key.
    • Use HasAPIKey alone.
    • request.state.principal may be None.
  • User + API Key:

    • Route requires a valid user authenticated via an API key.
    • Use IsAuthenticated and HasAPIKey.
    • request.state.principal is a Principal, and request.state.api_key is an APIKey.

This dual approach provides maximum flexibility for securing your APIs.

Writing Your Own Permission

Permissions keep DRF's shape, so a custom one is a class with a has_permission(request, view) method:

from fastapi_apikey_auth.security import BasePermission, get_principal


class IsInternalService(BasePermission):
    message = "Internal services only."

    def has_permission(self, request, view=None) -> bool:
        principal = get_principal(request)
        return principal is not None and principal.extra.get("internal") is True

Attach it to your own routes with Depends(IsInternalService()), or apply it to every route of the package through APIKEY_AUTH_API_EXTRA_PERMISSION_CLASS.


API Guide

This section provides a detailed overview of the fastapi-apikey-auth API, enabling administrators and users to manage API keys securely within FastAPI applications. The API exposes two primary endpoints:

  • /apikey/ - Admin API for managing all API keys (requires staff principals).
  • /my-apikey/ - User API for viewing their own API keys (authenticated principals only).

Admin API Key Management (/apikey/)

The apikey/ endpoint allows administrators (staff principals) to fully manage API keys. The available operations include:

  • List API keys:

    Fetches all API keys in the system. Controlled by the APIKEY_AUTH_API_ALLOW_LIST setting.

  • Retrieve an API key:

    Retrieves a specific API key by its ID. Controlled by the APIKEY_AUTH_API_ALLOW_RETRIEVE setting.

  • Create an API key:

    Creates a new API key with an associated user (optional) and an auto-generated key. Controlled by the APIKEY_AUTH_API_ALLOW_CREATE setting.

  • Update an API key:

    Updates an existing API key (e.g., toggling is_active or modifying expires_at). Controlled by the APIKEY_AUTH_API_ALLOW_UPDATE setting.

  • Delete an API key:

    Deletes an existing API key. Controlled by the APIKEY_AUTH_API_ALLOW_DELETE setting.

A disabled endpoint stays registered and answers 405 Method Not Allowed with a message pointing at the setting, so the change takes effect immediately without rebuilding the router.

Example Responses

List API keys:

GET /apikey/

Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "user": {
                "id": 2,
                "username": "user",
                "email": "example@domain.com"
            },
            "key": "test-key-123",
            "created_at": "2026-03-19T12:00:00Z",
            "expires_at": "2027-03-19T12:00:00Z",
            "is_active": true,
            "requests_count": 5,
            "max_requests": 100,
            "reset_at": "2026-03-20T12:00:00Z"
        }
    ]
}

Create an API Key:

POST /apikey/
Content-Type: application/json

{
    "user_id": 2,
    "expires_at": "2026-04-19T13:00:00Z"
}

Response:
HTTP/1.1 201 Created
Content-Type: application/json

{
    "id": 3,
    "user": {
        "id": 2,
        "username": "user",
        "email": "example@domain.com"
    },
    "key": "auto-generated-key-789",
    "created_at": "2026-03-19T13:00:00Z",
    "expires_at": "2026-04-19T13:00:00Z",
    "is_active": true,
    "requests_count": 0,
    "max_requests": null,
    "reset_at": null
}

Update an API Key:

PATCH /apikey/1/
Content-Type: application/json

{
    "is_active": false,
    "expires_at": "2026-06-01T00:00:00Z"
}

Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "id": 1,
    "user": {
        "id": 2,
        "username": "user",
        "email": "example@domain.com"
    },
    "key": "test-key-123",
    "created_at": "2026-03-19T12:00:00Z",
    "expires_at": "2026-06-01T00:00:00Z",
    "is_active": false,
    "requests_count": 5,
    "max_requests": 100,
    "reset_at": "2026-03-20T12:00:00Z"
}

PATCH writes only the fields present in the body. PUT writes every editable field, clearing the ones you omit.

Delete an API Key:

DELETE /apikey/1/

Response:
HTTP/1.1 204 No Content

User API Key Management (/my-apikey/)

The my-apikey/ endpoint allows authenticated principals to view their own API keys. Users can list and retrieve API keys, but cannot create, update, or delete them.

Example Responses

List User API Keys:

GET /my-apikey/

Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "user": {
                "id": 2,
                "username": "user",
                "email": "example@domain.com"
            },
            "key": "test-key-123",
            "created_at": "2026-03-19T12:00:00Z",
            "expires_at": "2027-03-19T12:00:00Z",
            "is_active": true,
            "requests_count": 5,
            "max_requests": 100,
            "reset_at": "2026-03-20T12:00:00Z"
        }
    ]
}

Retrieve a User's API Key:

GET /my-apikey/1/

Response:
HTTP/1.1 200 OK
Content-Type: application/json

{
    "id": 1,
    "user": {
        "id": 2,
        "username": "user",
        "email": "example@domain.com"
    },
    "key": "test-key-123",
    "created_at": "2026-03-19T12:00:00Z",
    "expires_at": "2027-03-19T12:00:00Z",
    "is_active": true,
    "requests_count": 5,
    "max_requests": 100,
    "reset_at": "2026-03-20T12:00:00Z"
}

A key belonging to somebody else answers 404, not 403, so the endpoint never confirms that an id exists.


Response Fields

  • id: Unique identifier of the API key.
  • user: The owner of the API key, rendered through the configured user fields (nullable).
  • key: The API key string (auto-generated on creation).
  • created_at: Timestamp when the key was created.
  • expires_at: Timestamp when the key expires (nullable).
  • is_active: Boolean indicating if the key is active.
  • requests_count: Number of requests made with the key.
  • max_requests: Maximum allowed requests (nullable).
  • reset_at: Timestamp when the request count resets (nullable).

Throttling

The API includes a built-in throttling mechanism that limits the number of requests a client can make based on their role. You can customize these throttle limits in the settings.

To specify the throttle rates for regular users and staff members:

APIKEY_AUTH_BASE_USER_THROTTLE_RATE = "100/day"
APIKEY_AUTH_STAFF_USER_THROTTLE_RATE = "1000/day"

Requests are bucketed by principal when one is known, then by API key, then by client IP. The valid time units are second, minute, hour and day.

The default throttle keeps its history in process memory. For a multi-worker deployment, subclass RoleBasedUserRateThrottle with a shared backend and point APIKEY_AUTH_API_THROTTLE_CLASS at it.


Filtering, Ordering, and Search

The API supports advanced query options:

  • Ordering: ?ordering=-created_at,requests_count — comma separated, - for descending. Restricted to the fields in APIKEY_AUTH_API_ORDERING_FIELDS.
  • Search: ?search=abc — matched case-insensitively against the fields in APIKEY_AUTH_API_SEARCH_FIELDS.
  • Filtering: whatever your APIKEY_AUTH_API_FILTER_CLASS implements.

These configurations can be customized in the settings.


Pagination

The API uses limit-offset pagination, allowing customization of minimum, maximum, and default page size limits: ?limit=25&offset=50. Limits below the minimum fall back to the default; limits above the maximum are clamped.

Setting APIKEY_AUTH_API_PAGINATION_CLASS = None returns every matching row in the same envelope.


Permissions

  • Admin API (/apikey/): Restricted to active staff principals (IsAuthenticated + IsAdminUser).
  • User API (/my-apikey/): Available to active principals (IsAuthenticated).

Both additionally apply APIKEY_AUTH_API_EXTRA_PERMISSION_CLASS when one is configured.


Each feature can be configured through the settings. For further details, refer to the Settings section.


Usage

This section provides a comprehensive guide on how to utilize the package's key features.

Configuring the package

There is no settings.py in FastAPI, so the package reads its settings from two places, in order of precedence:

  1. Explicit configuration through configure() or the keyword arguments of setup().
  2. Environment variables, using the same APIKEY_AUTH_ names.
from fastapi_apikey_auth import configure

configure(
    max_requests=1000,
    reset_request_interval="daily",
    header_type="Bearer",
)
export APIKEY_AUTH_MAX_REQUESTS=1000
export APIKEY_AUTH_RESET_REQUEST_INTERVAL=daily
export APIKEY_AUTH_HEADER_TYPE=Bearer

Environment values are converted using the type of the packaged default: booleans accept true/false, 1/0, yes/no and on/off; lists accept either a JSON array or a comma-separated string.

Settings are read at request time, so a change made through configure() after startup takes effect immediately. The configuration is validated when the routers are built, and an invalid value raises ImproperlyConfigured with the same error ids dj-apikey-auth reports through Django's system checks:

from fastapi_apikey_auth.settings.checks import check_apikey_settings

for error in check_apikey_settings():
    print(error)

Creating keys programmatically

Outside the API, use the repository:

from fastapi_apikey_auth.repository import APIKeyRepository

async with SessionLocal() as session:
    api_key = await APIKeyRepository(session).create(user_id=42)
    print(api_key.key)

Caching

Enable caching to skip resolving the 64 character key string on every request:

configure(use_caching=True, cache_timeout_seconds=300)

The cache maps the key string to the row's primary key; the row itself is always loaded fresh, which keeps request counters accurate. The default backend is process-local. For a shared backend, implement BaseCache and assign it:

from fastapi_apikey_auth import cache as cache_module

cache_module.cache = MyRedisCache()

Rate limit headers

RateLimitHeadersMiddleware copies the quota of the authenticating key onto the response as X-RateLimit-Limit and X-RateLimit-Remaining. setup() installs it by default; pass rate_limit_headers=False to leave it out, or install it yourself:

from fastapi_apikey_auth import RateLimitHeadersMiddleware

app.add_middleware(RateLimitHeadersMiddleware)

Mounting the routers by hand

If you would rather not use setup(), build the routers yourself and override the session dependency:

from fastapi_apikey_auth import create_admin_router, create_user_router, get_session

app.dependency_overrides[get_session] = get_db
app.include_router(create_admin_router(prefix="/admin/apikey"))
app.include_router(create_user_router(prefix="/me/apikey"))

API Keys List View

Overview

The APIKeyListView provides a user-friendly interface for displaying a list of all API keys in the application. It renders API keys in a modern, animated template, offering a sleek and interactive experience for administrators or users with appropriate permissions.

Access Control

  • Access is restricted based on the permission configured in the APIKEY_AUTH_VIEW_PERMISSION_CLASS setting. The default is IsAdminUser.
  • The view uses the same permission classes as the API, requiring each class to implement a has_permission(request, view) method that returns a boolean indicating whether access is granted.
  • If any permission check fails (e.g., has_permission is missing or returns False), a 403 Forbidden is returned.

Features

  • Comprehensive API Key List: Displays all API keys in the system, regardless of user association.
  • Customizable Ordering: Keys are ordered according to the APIKEY_AUTH_VIEW_ORDERING_FIELDS setting, allowing flexible sorting.
  • Modern UI: Rendered in the api_keys.html Jinja2 template with smooth animations and a responsive design.
  • Permission Flexibility: Supports a dynamic permission class configured via settings.
  • Masked Keys: Only the first and last four characters of each key are shown, with a copy button for the full value.

Usage

  1. Navigate to the API keys list URL in your application (e.g., /apikey_auth/api_keys/).
  2. Ensure you meet the permission requirements specified in APIKEY_AUTH_VIEW_PERMISSION_CLASS.
  3. View the complete list of API keys, styled in a modern table with status indicators and copy functionality.

Settings

This section outlines the available settings for configuring the fastapi-apikey-auth package. You can customize these settings through configure(), through the keyword arguments of setup(), or through environment variables of the same name.

Example Settings

Below is an example configuration with default values:

# APIKey Settings
APIKEY_AUTH_RESET_REQUEST_INTERVAL = None
APIKEY_AUTH_MAX_REQUESTS = None

# Authentication Settings
APIKEY_AUTH_HEADER_NAME = "Authorization"
APIKEY_AUTH_HEADER_TYPE = None
APIKEY_AUTH_USE_CACHING = False
APIKEY_AUTH_CACHE_TIMEOUT_SECONDS = 300
APIKEY_AUTH_USER_LOADER = None

# Global API Settings
APIKEY_AUTH_API_ALLOW_LIST = True
APIKEY_AUTH_API_ALLOW_RETRIEVE = True
APIKEY_AUTH_API_ALLOW_CREATE = True
APIKEY_AUTH_API_ALLOW_UPDATE = True
APIKEY_AUTH_API_ALLOW_DELETE = False
APIKEY_AUTH_BASE_USER_THROTTLE_RATE = "30/minute"
APIKEY_AUTH_STAFF_USER_THROTTLE_RATE = "100/minute"
APIKEY_AUTH_API_THROTTLE_CLASS = (
    "fastapi_apikey_auth.throttling.RoleBasedUserRateThrottle"
)
APIKEY_AUTH_API_PAGINATION_CLASS = (
    "fastapi_apikey_auth.pagination.DefaultLimitOffSetPagination"
)
APIKEY_AUTH_API_EXTRA_PERMISSION_CLASS = None
APIKEY_AUTH_API_APIKEY_SCHEMA_CLASS = None
APIKEY_AUTH_API_USER_SCHEMA_CLASS = None
APIKEY_AUTH_API_USER_SCHEMA_FIELDS = ["id", "username", "email"]
APIKEY_AUTH_API_ORDERING_FIELDS = [
    "id",
    "max_requests",
    "requests_count",
    "created_at",
    "expires_at",
    "reset_at",
]
APIKEY_AUTH_API_SEARCH_FIELDS = ["id"]
APIKEY_AUTH_API_FILTER_CLASS = None

# Template View Settings
APIKEY_AUTH_VIEW_PERMISSION_CLASS = "fastapi_apikey_auth.security.IsAdminUser"
APIKEY_AUTH_VIEW_ORDERING_FIELDS = ["expires_at", "-created_at"]

Settings Overview

Below is a detailed description of each setting in fastapi-apikey-auth, so you can better understand and tweak them to fit your project's needs.

APIKEY_AUTH_RESET_REQUEST_INTERVAL

Type: Optional[str]

Default: None

Description: Defines the interval after which the request count for API keys resets. Must be one of minutely, hourly, daily, or monthly. Set to None to disable automatic reset.


APIKEY_AUTH_MAX_REQUESTS

Type: Optional[int]

Default: None

Description: Sets the maximum number of requests allowed per API key before throttling is enforced. Set to None for unlimited requests. New keys inherit this value as their max_requests.


APIKEY_AUTH_HEADER_NAME

Type: str

Default: "Authorization"

Description: Specifies the HTTP header name used to pass the API key (e.g., "Authorization" or "X-API-Key"). Customize this to match your authentication setup.


APIKEY_AUTH_HEADER_TYPE

Type: Optional[str]

Default: None

Description: Defines the prefix expected in the API key header (e.g., "Bearer" in "Bearer <key>"). Customize this to align with your authentication format.


APIKEY_AUTH_USE_CACHING

Type: bool

Default: False

Description: Enables caching of API key lookups to improve performance. Set to True to resolve the key string from the cache instead of the database on every request.


APIKEY_AUTH_CACHE_TIMEOUT_SECONDS

Type: int

Default: 300

Description: Sets the duration (in seconds) that API key data is cached when caching is enabled. Adjust this to balance performance and freshness of data.


APIKEY_AUTH_USER_LOADER

Type: Optional[str | Callable]

Default: None

Description: A callable, or the dotted path to one, that resolves the user_id of an API key into a user. It receives (session, user_id), may be sync or async, and may return a Principal, a mapping, or any user object. This is what makes IsAdminUser, the staff throttle rate and the nested user object in responses work.


APIKEY_AUTH_API_ALLOW_LIST

Type: bool

Default: True

Description: Allows the API to list API keys. Set to False to disable this feature.


APIKEY_AUTH_API_ALLOW_RETRIEVE

Type: bool

Default: True

Description: Allows retrieving individual API keys by ID via the API. Set to False to disable this feature.


APIKEY_AUTH_API_ALLOW_CREATE

Type: bool

Default: True

Description: Allows creating new API keys via the API. Set to False to disable this feature.


APIKEY_AUTH_API_ALLOW_UPDATE

Type: bool

Default: True

Description: Allows updating existing API keys via the API (e.g., toggling is_active). Set to False to disable this feature.


APIKEY_AUTH_API_ALLOW_DELETE

Type: bool

Default: False

Description: Allows deleting API keys via the API. Set to True to enable this feature.


APIKEY_AUTH_BASE_USER_THROTTLE_RATE

Type: str

Default: "30/minute"

Description: Sets the throttle rate (e.g., "100/day") for regular users in the API. Adjust this to limit request frequency. An empty value disables throttling for them.


APIKEY_AUTH_STAFF_USER_THROTTLE_RATE

Type: str

Default: "100/minute"

Description: Sets the throttle rate (e.g., "1000/day") for staff principals in the API. Adjust this to provide higher limits for privileged users.


APIKEY_AUTH_API_THROTTLE_CLASS

Type: Optional[str]

Default: "fastapi_apikey_auth.throttling.RoleBasedUserRateThrottle"

Description: Specifies the throttle class used to limit API requests. Set to None to disable throttling on the package's routes.


APIKEY_AUTH_API_PAGINATION_CLASS

Type: Optional[str]

Default: "fastapi_apikey_auth.pagination.DefaultLimitOffSetPagination"

Description: Defines the pagination class used in API responses. Set to None to return every matching row.


APIKEY_AUTH_API_EXTRA_PERMISSION_CLASS

Type: Optional[str]

Default: None

Description: Optionally specifies an additional permission class applied to every route of the package (e.g., "path.to.IsInternalService"). This allows for fine-grained access control beyond authentication.


APIKEY_AUTH_API_USER_SCHEMA_FIELDS

Type: List[str]

Default: ["id", "username", "email"]

Description: Defines the fields to be included in the nested user object in API responses. These are also the attributes read off a user object returned by the user loader.


APIKEY_AUTH_API_APIKEY_SCHEMA_CLASS

Type: Optional[str]

Default: None (falls back to fastapi_apikey_auth.schemas.APIKeyRead)

Description: Specifies the Pydantic schema used for APIKey objects in the API. Customize this if you need a different representation.


APIKEY_AUTH_API_USER_SCHEMA_CLASS

Type: Optional[str]

Default: None (a schema is generated from APIKEY_AUTH_API_USER_SCHEMA_FIELDS)

Description: Specifies the Pydantic schema used for user objects in the API. Customize this if you need a different user representation.


APIKEY_AUTH_API_ORDERING_FIELDS

Type: List[str]

Default: ["id", "max_requests", "requests_count", "created_at", "expires_at", "reset_at"]

Description: Specifies the fields available for ordering in API queries, allowing responses to be sorted by these fields. See all available fields below.


APIKEY_AUTH_API_SEARCH_FIELDS

Type: List[str]

Default: ["id"]

Description: Specifies the fields that are searchable in the API, allowing users to filter results based on these fields. See all available fields below.


APIKEY_AUTH_API_FILTER_CLASS

Type: Optional[str]

Default: None

Description: Specifies a custom filter class for API filtering. It must implement filter_query(statement, params) and is easiest to write by subclassing fastapi_apikey_auth.filtering.BaseAPIKeyFilter. Set to None to disable custom filtering.


APIKEY_AUTH_VIEW_PERMISSION_CLASS

Type: Optional[str]

Default: "fastapi_apikey_auth.security.IsAdminUser"

Description: Specifies the permission class for the APIKeyListView. Customize this to change access requirements for the view.


APIKEY_AUTH_VIEW_ORDERING_FIELDS

Type: List[str]

Default: ["expires_at", "-created_at"]

Description: Specifies the fields that the APIKeyListView is ordered by. Adjust this to sort the displayed API key list differently.


All Available Fields

These are all fields available for searching and ordering in API key records:

  • id: Unique identifier of the API key (orderable, searchable).
  • user_id: ID of the associated user (orderable, searchable).
  • key: The API key string (orderable, searchable).
  • created_at: Timestamp when the key was created (orderable).
  • expires_at: Timestamp when the key expires (orderable).
  • is_active: Boolean indicating if the key is active (orderable).
  • requests_count: Number of requests made with the key (orderable).
  • max_requests: Maximum allowed requests (orderable).
  • reset_at: Timestamp when the request count resets (orderable).

Coming from dj-apikey-auth

The two packages share their settings names, endpoints, response fields and behaviour. What differs is what has no FastAPI equivalent:

dj-apikey-auth fastapi-apikey-auth
apikey_auth Django app in INSTALLED_APPS setup(app, get_db)
Django ORM APIKey model + migration SQLAlchemy APIKey model on Base.metadata
settings.py constants configure(), setup(**settings) or APIKEY_AUTH_* environment variables
Django system checks run_checks(), raising ImproperlyConfigured at router build time
AUTH_USER_MODEL APIKEY_AUTH_USER_LOADER returning a Principal
DRF authentication class APIKeyAuthentication dependency
DRF permission classes The same classes, still exposing has_permission(request, view)
request.user / request.auth request.state.principal / request.state.api_key
DRF serializers Pydantic schemas
request.META["X-RateLimit-*"] RateLimitHeadersMiddleware
Django cache framework fastapi_apikey_auth.cache, swappable
Django admin (APIKeyAdmin) No equivalent — use the admin API and the HTML dashboard
django-filter FilterSet BaseAPIKeyFilter
Disabled methods raise MethodNotAllowed in the viewset Routes stay registered and answer 405 per request

Response bodies are unchanged apart from the nested user object, which is now driven by APIKEY_AUTH_API_USER_SCHEMA_FIELDS instead of the Django user model, and list responses, which always use the count/next/previous/results envelope even when pagination is disabled.


Conclusion

We hope this documentation has provided a comprehensive guide to using and understanding fastapi-apikey-auth.

Final Notes:

  • Version Compatibility: Ensure your project meets the compatibility requirements for both FastAPI and Python versions.
  • API Integration: The package is designed for flexibility, allowing you to customize many features based on your application's needs.
  • Contributions: Contributions are welcome! Feel free to check out the Contributing guide for more details.

If you encounter any issues or have feedback, please reach out via our GitHub Issues page.

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages