litestar-middleware

Configure built-in and custom ASGI middleware for Litestar applications.

13|Updated Apr 15, 2026
One-click install
npx skills add https://github.com/litestar-org/litestar-skills --skill litestar-middleware-litestar-org
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: litestar-middleware
Source: https://github.com/litestar-org/litestar-skills/tree/main/plugins/litestar/skills/litestar-middleware
Command: npx skills add https://github.com/litestar-org/litestar-skills --skill litestar-middleware-litestar-org

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill eliminates the guesswork and common errors when configuring Litestar's built-in and custom ASGI middleware, ensuring correct scope filtering, ordering, and separation of cross-cutting concerns from business logic.

Core Features & Use Cases

  • Built-in Middleware Configuration: Guidance for using Litestar's pre-built middleware for CORS, CSRF, compression, request IDs, and allowed hosts out of the box.
  • Custom ASGI Middleware Patterns: Reusable templates for cross-cutting concerns like request timing, structured logging, and JWT authentication that auto-loads users onto the connection object.
  • Scope and Ordering Best Practices: Rules for filtering middleware to specific ASGI scopes (HTTP/WebSocket) and setting intentional middleware order to avoid unexpected behavior.
  • Use Case: For example, if you need to add request timing that only applies to HTTP endpoints and skips health check paths, this Skill provides the exact implementation pattern to avoid affecting WebSocket traffic or adding overhead to noise endpoints.

Quick Start

Use the litestar-middleware skill to implement a custom timing middleware that only runs for HTTP requests, excludes /health and /metrics paths, and logs request duration in milliseconds.

Frequently Asked Questions about litestar-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I configure custom ASGI middleware in Litestar for cross-cutting concerns like logging and authentication?

Configuring custom ASGI middleware in Litestar involves creating reusable templates for cross-cutting concerns like structured logging and JWT authentication, ensuring separation of cross-cutting logic from business rules while auto-loading users onto the connection object.

How does Litestar handle middleware ordering and scope filtering for HTTP and WebSocket traffic?

Litestar middleware ordering requires intentional sequence configuration to avoid unexpected behavior, while scope filtering restricts middleware execution to specific ASGI scopes like HTTP or WebSocket, preventing cross-protocol interference and unnecessary overhead.

Can I apply Litestar's built-in CORS and CSRF middleware while excluding specific paths like health checks?

Litestar's built-in CORS and CSRF middleware supports path exclusion, allowing you to skip noise endpoints like health checks and metrics while maintaining cross-cutting security policies across the remaining application routes.

What is the best way to implement request timing middleware in Litestar without affecting WebSocket traffic?

The best way to implement request timing middleware in Litestar is to apply ASGI scope filtering to restrict execution to HTTP endpoints only, while simultaneously excluding health and metrics paths to avoid adding overhead to noise endpoints.

Does Litestar middleware integrate with the framework's authentication and guard systems?

Litestar middleware integrates seamlessly with the framework's authentication and guard systems, enabling custom middleware to auto-load authenticated users onto the connection object before subsequent guard checks evaluate route authorization.

Why does my Litestar middleware run unexpectedly for WebSocket connections or skip intended HTTP routes?

Unexpected middleware execution in Litestar typically occurs when ASGI scope filtering is not applied correctly or middleware ordering lacks intentional configuration, causing cross-cutting logic to run for WebSocket connections or bypass intended HTTP routes.