Skip to main content

apikeyauth

Drop-in API key authentication for FastAPI


Install

pip install apikeyauth

Usage

from fastapi import FastAPI, Depends
from apikeyauth import APIKeyAuth, MemoryKeyStore, KeyInfo

store = MemoryKeyStore({
    "sk_live_abc123": KeyInfo(name="Production App", scopes=["read", "write"]),
    "sk_test_xyz789": KeyInfo(name="Test App", scopes=["read"]),
})
auth = APIKeyAuth(store)

app = FastAPI()

@app.get("/data")
async def get_data(key: KeyInfo = Depends(auth)):
    return {"hello": key.name, "scopes": key.scopes}

Clients authenticate via header or query param:

# Header
curl -H "X-API-Key: sk_live_abc123" http://localhost:8000/data

# Query param
curl http://localhost:8000/data?api_key=sk_live_abc123

Features

Feature Description
Header auth X-API-Key header (configurable name)
Query auth ?api_key= query param (configurable name)
Flexible auth Checks header first, falls back to query
Key metadata Name, scopes, rate limit, owner, custom metadata
Pluggable store Implement KeyStore for database/Redis backends
Memory store Built-in dict-based store with add/revoke

Custom Backend

from apikeyauth import KeyStore, KeyInfo

class DatabaseKeyStore(KeyStore):
    async def validate(self, key: str) -> KeyInfo | None:
        row = await db.fetch_one("SELECT * FROM api_keys WHERE key_hash = $1", hash(key))
        if row:
            return KeyInfo(name=row["name"], scopes=row["scopes"].split(","))
        return None

License

MIT

Release files for apikeyauth 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for apikeyauth 0.1.1
File Size Uploaded
apikeyauth-0.1.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for apikeyauth 0.1.1
File Interpreter ABI Platform
apikeyauth-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / apikeyauth-0.1.1.tar.gz

Download URL apikeyauth-0.1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7cd596e0416ea69de249bef2591c95a4da9eb7b436e6732cea72e6da28508506
BLAKE2b-256 checksum
How to use checksums
209b4cd8bb83c5d0d4b9ff266b8631dbca3a00e751ec1e8f634128a7cc3ebd97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / apikeyauth-0.1.1-py3-none-any.whl

Download URL apikeyauth-0.1.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
578168a770f5922a1a64ae32e0d60ea93c1aac03b6ffe636a6d3b7934cd42b4e
BLAKE2b-256 checksum
How to use checksums
48cdfde658d87e3c765ca55ff7d4b641a30c841bbd609d97343f30db97e74b13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page