Drop-in API key authentication for FastAPI
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
apikeyauth-0.1.1.tar.gz
(4.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apikeyauth-0.1.1.tar.gz.
File metadata
- Download URL: apikeyauth-0.1.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cd596e0416ea69de249bef2591c95a4da9eb7b436e6732cea72e6da28508506
|
|
| MD5 |
23a24c2a0f2cba904ea4e0af90186c18
|
|
| BLAKE2b-256 |
209b4cd8bb83c5d0d4b9ff266b8631dbca3a00e751ec1e8f634128a7cc3ebd97
|
File details
Details for the file apikeyauth-0.1.1-py3-none-any.whl.
File metadata
- Download URL: apikeyauth-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
578168a770f5922a1a64ae32e0d60ea93c1aac03b6ffe636a6d3b7934cd42b4e
|
|
| MD5 |
03281ea8b89412d629093bf9a770c8ad
|
|
| BLAKE2b-256 |
48cdfde658d87e3c765ca55ff7d4b641a30c841bbd609d97343f30db97e74b13
|