API Key Gateway
A Python library that provides an @apikey_login decorator to validate API keys with service-aware authentication.
Features
- CLI Decorator: Automatically adds
--apikey/-aCLI parameter to decorated functions - FastAPI Middleware: Built-in support for FastAPI applications
- Strong Security: Validates API keys using argon2id hashing algorithm
- Service-Aware: Keys are scoped to specific services
- Remote Key Management: Fetches valid public keys from a remote JSON endpoint
- Caching: Caches valid API key-service pairs for improved performance
- Retry Mechanism: Automatically retries fetching keys on network failures
- Flexible Configuration: Supports custom API key URLs and authentication headers
Installation
uv install apikey-gateway
Usage
The library supports two usage modes: CLI Decorator and FastAPI Middleware.
CLI Decorator
The apikey_login decorator automatically adds --apikey/-a CLI parameter to decorated functions.
from apikey_gateway import apikey_login
@apikey_login(service="media-match")
def media_app():
print("API key validated for media-match service!")
# Your media application logic here
@apikey_login(service="analytics")
def analytics_app():
print("API key validated for analytics service!")
# Your analytics application logic here
if __name__ == "__main__":
media_app() # or analytics_app()
Run with:
python app.py --apikey your-secret-key
FastAPI Middleware
The library provides built-in FastAPI middleware for validating API keys on all requests.
from fastapi import FastAPI
from apikey_gateway import add_apikey_gateway_middleware
# Create your FastAPI app
app = FastAPI(title="My API")
# Add API key validation middleware
add_apikey_gateway_middleware(
app=app,
service="my-service",
verbose=True
)
# All endpoints below will require API key validation
@app.get("/protected/resource")
def protected_resource():
return {"message": "Access granted to protected resource"}
# To run: uvicorn app:app --reload
Test the FastAPI endpoint:
# With X-AKGATEWAY-API-KEY header (default)
curl -H "X-AKGATEWAY-API-KEY: your-secret-key" http://localhost:8000/protected/resource
# With Authorization Bearer header
curl -H "Authorization: Bearer your-secret-key" http://localhost:8000/protected/resource
Middleware Features:
- Automatically checks
X-AKGATEWAY-API-KEYandAuthorization Bearerheaders - Supports custom API key headers
- Can be configured with a custom public keys URL
- Supports
X-JSONBIN-API-KEYheader for JSONBin API key authentication - Allows customization of both API key header and JSONBin API key header names
How It Works
For CLI Applications
- The application specifies the
servicename when using the@apikey_logindecorator - The user provides a secret API key via the
--apikey/-aCLI parameter - Followed by the same validation steps as FastAPI applications...
For FastAPI Applications
- The application adds the middleware with a specific
servicename - The client provides a secret API key via
X-AKGATEWAY-API-KEYorAuthorization Bearerheader - Followed by the same validation steps as CLI applications...
Common Validation Steps
- The library computes an argon2id hash (public key) from the secret key
- It fetches the list of valid public keys from
https://api.jsonbin.io/v3/b/691ec6a543b1c97be9b8ea6d - Valid keys are filtered to only those belonging to the specified service
- If the computed public key matches any valid service-specific public key, access is granted
JSON Format
The remote JSON follows a service-aware structure where keys are organized by service name:
Service-Aware Structure
{
"service1": {
"key_id_1": "argon2id_hash_here",
"key_id_2": "another_hash_here"
},
"service2": "single_key_hash_here"
}
- Top-level keys are service names
- Each service can have either multiple keys (as a dictionary) or a single key (as a string)
- The library automatically handles both formats when fetching keys
License
MIT
Metadata
Release files for apikey-gateway 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| apikey_gateway-1.1.3.tar.gz | 13.4 kB | Without provenance | Details for apikey_gateway-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| apikey_gateway-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for apikey_gateway-1.1.3-py3-none-any.whl |
Total release size: 24.5 kB
Release files / apikey_gateway-1.1.3.tar.gz
| Download URL | apikey_gateway-1.1.3.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3c9f5ef11d9c867d1279a1906a2eeff0a29af68b045d990cfa370c6fecd6a15
|
|
BLAKE2b-256 checksum How to use checksums |
5fbf917797e4853e8c7a3d0754024d7e17c57737024ff18dc3a2cfadf3c0711b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|
Release files / apikey_gateway-1.1.3-py3-none-any.whl
| Download URL | apikey_gateway-1.1.3-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42c17d6287fb680b74e6b0ea5bf805a9cfc467d2929f4f16d2d9b4d623673261
|
|
BLAKE2b-256 checksum How to use checksums |
c61aa9032b845827e940c2f8e7f964d24e1c9a3546693dadfe5b5f744131d00d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|
Security for apikey-gateway 1.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.