Skip to content
Nexios Blog

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.

What is nexios-contrib?

Think of nexios-contrib as the Swiss Army knife for Nexios applications. It’s a collection of community-contributed extensions that provide:

Installation & Setup

Getting started with community extensions is simple:

Terminal window
pip install nexios-contrib

Each extension is designed to work seamlessly with your existing Nexios application:

from nexios import NexiosApp
from nexios_contrib.etag import ETagMiddleware
from nexios_contrib.trusted import TrustedHostMiddleware
from nexios_contrib.redis import RedisMiddleware
app = NexiosApp(title="Enhanced API")
# Add community middleware
app.add_middleware(ETagMiddleware())
app.add_middleware(TrustedHostMiddleware(allowed_hosts=["api.example.com"]))
app.add_middleware(RedisMiddleware(redis_url="redis://localhost:6379"))

Essential Performance Extensions

ETag Middleware for Smart Caching

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)

Security Extensions

Trusted Host Middleware

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
],
))

Integration Extensions

Redis Integration

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)

Development & Debugging Extensions

Request ID Tracking

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})

Timeout Management

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"]
))

Building Custom Extensions

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 middleware
app.add_middleware(CustomAnalyticsMiddleware(
analytics_endpoint="https://analytics.yourapp.com/events"
))

Real-World Example: E-commerce API

Here’s how you might combine multiple contrib extensions for a production e-commerce API:

from nexios import NexiosApp
from nexios_contrib.etag import ETagMiddleware
from nexios_contrib.trusted import TrustedHostMiddleware
from nexios_contrib.redis import RedisMiddleware
app = NexiosApp(title="E-commerce API")
# Security layer
app.add_middleware(TrustedHostMiddleware(
allowed_hosts=["api.shop.com", "*.shop.com"]
))
# Performance layer
app.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)

Contributing to nexios-contrib

The community extensions are open source and welcome contributions:

  1. Fork the repository: https://github.com/nexios-labs/contrib
  2. Create your extension: Follow the established patterns
  3. Add tests: Ensure your extension is well-tested
  4. Submit a PR: Share your utility with the community

Getting Help

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?

Terminal window
pip install nexios-contrib

Join the community and start building with the collective wisdom of Nexios developers worldwide!