Routing, middleware, rate limiting, and request transformation. Zero external dependencies beyond pytest.
- Router — pattern-based URL routing with parameter extraction
- Middleware chain — composable request/response processing pipeline
- Rate limiting — token bucket, sliding window, and fixed window algorithms
- Request transformation — modify requests and responses with configurable rules
- Pure Python — built with dataclasses and type hints, no framework dependency
pip install api-gatewayfrom api_gateway import Gateway, Router, Route, Middleware, RateLimiter
# Set up routing
router = Router()
router.add(Route(path="/v1/chat", handler=chat_handler))
router.add(Route(path="/v1/models", handler=models_handler))
# Add middleware
gateway = Gateway(router=router)
gateway.use(Middleware(rate_limit=RateLimiter(max_requests=100, window_seconds=60)))
gateway.use(Middleware(transform=RequestTransformer(add_header={"X-Fleet": "cocapn"})))
# Process a request
response = gateway.handle(request)The gateway backing cocapn-sdk and cocapn-py — the "one API key, any model" endpoint runs through this.
- cocapn-sdk — Node.js SDK (talks to this gateway)
- Claude-PRISM-CF — Edge routing optimization
- cache-layer-optimizer — Response caching
pip install pytest
pytest tests/pip install api-gatewayPython 3.10+. MIT license.