Supercharge Your Nexios Apps with Community Extensions
The nexios-contrib package is where the Nexios community comes together to share powerful extensions, middleware, and utilities. Instead of reinventing the wheel, you can leverage battle-tested components that solve common web development challenges.
Think of nexios-contrib as the Swiss Army knife for Nexios applications. It’s a collection of community-contributed extensions that provide:
- Performance optimizations (ETag, compression, caching)
- Security enhancements (trusted hosts, rate limiting)
- Integration utilities (Redis, databases, external APIs)
- Development tools (request ID tracking, debugging helpers)
Getting started with community extensions is simple:
pip install nexios-contribEach extension is designed to work seamlessly with your existing Nexios application:
from nexios import NexiosAppfrom nexios_contrib.etag import ETagMiddlewarefrom nexios_contrib.trusted import TrustedHostMiddlewarefrom nexios_contrib.redis import RedisMiddleware
app = NexiosApp(title="Enhanced API")
# Add community middlewareapp.add_middleware(ETagMiddleware())app.add_middleware(TrustedHostMiddleware(allowed_hosts=["api.example.com"]))app.add_middleware(RedisMiddleware(redis_url="redis://localhost:6379"))The ETag middleware automatically generates ETags for your responses, enabling efficient client-side caching:
from nexios_contrib.etag import ETagMiddleware
app.add_middleware(ETagMiddleware( # Only apply to GET requests methods=["GET"],
))
@app.get("/users/{user_id}")async def get_user(request, response): user = await fetch_user(request.path_params.user_id) # ETag automatically generated based on response content # Client will receive 304 Not Modified if content unchanged return response.json(user)Protect against Host header attacks:
from nexios_contrib.trusted import TrustedHostMiddleware
app.add_middleware(TrustedHostMiddleware( allowed_hosts=[ "api.yourapp.com", "*.yourapp.com", # Wildcard support "localhost:8000" # Development ],))Seamless Redis integration for caching, sessions, and more:
from nexios_contrib.redis import get_redis,init_redis
init_redis(app)
@app.get("/users/{user_id}")async def get_user_cached(request, response): user_id = request.path_params.user_id redis = get_redis(request)
# Try cache first cached_user = await redis.get(f"user:{user_id}") if cached_user: return response.json(json.loads(cached_user))
# Fetch from database user = await fetch_user_from_db(user_id)
# Cache for future requests await redis.setex( f"user:{user_id}", 3600, # 1 hour TTL json.dumps(user) )
return response.json(user)Track requests across your application with automatic request ID generation:
from nexios_contrib.request_id import RequestIDMiddleware
app.add_middleware(RequestIDMiddleware( header_name="X-Request-ID", force_generate=True # Auto-generate if not provided))
@app.get("/debug")async def debug_endpoint(request, response): request_id = request.headers.get("X-Request-ID") logger.info(f"Processing request {request_id}")
# Request ID automatically added to response headers return response.json({"request_id": request_id})Prevent hanging requests with configurable timeouts:
from nexios_contrib.timeout import TimeoutMiddleware
app.add_middleware(TimeoutMiddleware( timeout=30.0, # 30 second timeout exclude_paths=["/upload", "/long-running-task"]))The contrib package also provides utilities for building your own extensions:
from nexios_contrib.base import BaseMiddleware
class CustomAnalyticsMiddleware(BaseMiddleware): def __init__(self, analytics_endpoint: str): self.analytics_endpoint = analytics_endpoint
async def __call__(self, request, response, call_next): start_time = time.time()
# Process request result = await call_next()
# Send analytics data duration = time.time() - start_time await self.send_analytics({ "path": str(request.url), "method": request.method, "duration": duration, "status": response.status_code })
return result
async def send_analytics(self, data): # Your analytics implementation pass
# Use your custom middlewareapp.add_middleware(CustomAnalyticsMiddleware( analytics_endpoint="https://analytics.yourapp.com/events"))Here’s how you might combine multiple contrib extensions for a production e-commerce API:
from nexios import NexiosAppfrom nexios_contrib.etag import ETagMiddlewarefrom nexios_contrib.trusted import TrustedHostMiddlewarefrom nexios_contrib.redis import RedisMiddleware
app = NexiosApp(title="E-commerce API")
# Security layerapp.add_middleware(TrustedHostMiddleware( allowed_hosts=["api.shop.com", "*.shop.com"]))
# Performance layerapp.add_middleware(ETagMiddleware())
@app.get("/products/{product_id}")async def get_product(request, response): # All middleware automatically applied: # - Host validation # - Rate limiting # - Redis caching # - ETag generation # - Response compression product = await fetch_product(request.path_params.product_id) return response.json(product)The community extensions are open source and welcome contributions:
- Fork the repository:
https://github.com/nexios-labs/contrib - Create your extension: Follow the established patterns
- Add tests: Ensure your extension is well-tested
- Submit a PR: Share your utility with the community
- Documentation: Each extension includes comprehensive docs
- GitHub Issues: Report bugs or request features
- Community Discord: Get help from other developers
- Stack Overflow: Tag questions with
nexios-contrib
The nexios-contrib package transforms Nexios from a great framework into an incredibly powerful development platform. With community-tested extensions, you can focus on building your unique business logic while leveraging proven solutions for common challenges.
Ready to supercharge your Nexios app?
pip install nexios-contribJoin the community and start building with the collective wisdom of Nexios developers worldwide!