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.0.tar.gz
(4.1 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.0.tar.gz.
File metadata
- Download URL: apikeyauth-0.1.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cc2785f5a6e3e18afd3acd3dbb760c272dfebff8651fdfc8397e0934a2ffed2
|
|
| MD5 |
b1cf5ea2555d027abee6781f605e1947
|
|
| BLAKE2b-256 |
be7418d41adb8657c9fd9546f718c1c8de49d80caecc3fc11b06954ab3540e91
|
File details
Details for the file apikeyauth-0.1.0-py3-none-any.whl.
File metadata
- Download URL: apikeyauth-0.1.0-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 |
4da7cad3009618bab7e1951c71ad34de4cfd4b0a78c32810beb384d462252ad2
|
|
| MD5 |
524b63a84fb5decac56e1fb5f37c1f3e
|
|
| BLAKE2b-256 |
613bf17b1f723660510c052073ae8752182908bc13ba254b32a989314252c7b4
|