Async SDK for issuing and validating scoped API keys
Project description
apikeys-platform
Async Python SDK for issuing, scoping, and validating API keys — backed by SQLite or PostgreSQL.
Designed for developers who want to add API key management to their own product without building the infrastructure from scratch.
Installation
# SQLite (development / small deployments)
pip install "apikeys-platform[sqlite]"
# PostgreSQL (production)
pip install "apikeys-platform[postgresql]"
# Both drivers
pip install "apikeys-platform[all]"
Requires Python 3.11+.
Quickstart
create_tables and APIKeyClient both accept plain URLs — no need to specify the async driver suffix:
import asyncio
from apikeys import APIKeyClient, KeyMetadata, create_tables
async def main():
# SQLite for dev: "sqlite:///myapp.db"
# PostgreSQL prod: "postgresql://user:pass@host:5432/dbname"
db_url = "sqlite:///myapp.db"
await create_tables(db_url) # creates tables on first run; safe to call on every restart
client = APIKeyClient(db_url)
# One-time setup
org = await client.create_organization("Acme Inc")
product = await client.create_product(str(org.id), "My API")
project = await client.create_project(str(org.id), "v1")
await client.add_product_to_project(str(product.id), str(project.id))
# Issue a key for one of your users
result = await client.create_key(
str(org.id),
project_id=str(project.id),
product_id=str(product.id),
metadata=KeyMetadata(
name="Alice's key",
scopes=["read", "write"],
rate_limit=1000,
custom={"user_id": "u_alice", "plan": "pro"},
),
)
print(result.plaintext) # return this once to your user
# Validate an incoming request
key = await client.validate_key(
result.plaintext,
product_id=str(product.id),
required_scope="read",
)
print(key.metadata.custom["user_id"]) # → u_alice
asyncio.run(main())
Key concepts
| Concept | Description |
|---|---|
| Organization | Top-level tenant — maps to one of your customers or your own company |
| Project | Groups keys within an org (e.g. v1, v2, mobile) |
| Product | A named API surface linked to projects; keys can be scoped to one product |
KeyMetadata.custom |
Arbitrary JSON attached to a key — store user_id, plan, tenant_id, etc. |
Keys are scoped from broad → narrow: org-wide → project-scoped → product-scoped.
validate_key() checks the key is active, matches the expected product, and holds the required scope.
Exceptions
from apikeys import InvalidKeyError, RevokedKeyError, InsufficientScopeError
| Exception | When raised |
|---|---|
InvalidKeyError |
Key not found |
RevokedKeyError |
Key exists but has been revoked |
InsufficientScopeError |
Key is valid but missing the required scope |
Full example
See examples/acme_integration.py for a complete end-to-end script showing setup, per-user key creation, request validation, and listing keys by user.
License
MIT
api-project
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 apikeys_platform-0.1.0.tar.gz.
File metadata
- Download URL: apikeys_platform-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f18158dd71ed2fe187ae814e4533be34cc9c22763573adf3fcb055bf1ff0804
|
|
| MD5 |
25790d3ef06702221ef3be0593140dc2
|
|
| BLAKE2b-256 |
ef34c1ef65f97c7909a6d56f755caebce89169033c5b018e7ea2bfb47cc642a2
|
File details
Details for the file apikeys_platform-0.1.0-py3-none-any.whl.
File metadata
- Download URL: apikeys_platform-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a2f3ecd6fc40eeb734cd9d3285bc77c9def876db52a3343159fc55e041f2ec0
|
|
| MD5 |
9397a316d17177f15ac17fd411285bbd
|
|
| BLAKE2b-256 |
fdf441dad0d118934f34ebbc56f155f5a3054e3ea9ff7c256353bf32fb7516dc
|