You are viewing the local version of the documentation. Click here to go to the latest version.

Litestar Litestar Litestar
  • Home
    • Contributing Learn how to contribute to the Litestar project
    • Code of Conduct Review the etiquette for interacting with the Litestar community
    • Security Overview of Litestar's security protocols
    • Litestar Organization Details about the Litestar organization
    • Releases Explore the release process, versioning, and deprecation policy for Litestar
    • What's new in 3.0 Explore the new features in Litestar 3.0
    • 3.x Changelog All changes in the 3.x series
    • 2.x Changelog All changes in the 2.x series
    • Discord Help Forum Dedicated Discord help forum
    • GitHub Discussions GitHub Discussions
    • Stack Overflow We monitor the litestar tag on Stack Overflow
  • Sponsor
/
  • latest
  • development
  • v3
  • v2
  • v1
  • Discussion

Documentation

  • Getting Started
  • Usage
    • Applications
    • Routing
      • Overview
      • Route handlers
      • Parameters
    • Requests
    • Caching
    • Channels
    • CLI
    • Databases
      • SQLAlchemy
        • SQLAlchemy Models & Repository
        • Plugins
          • SQLAlchemy Plugin
          • SQLAlchemy Init Plugin
          • SQLAlchemy Serialization Plugin
      • Piccolo ORM
    • Debugging
    • Dependency Injection
    • Data Transfer Object (DTO)
      • Basic Use
      • AbstractDTO
      • Implementing Custom DTO Classes
    • Events
    • Exceptions and exception handling
    • File Systems
    • HTMX
    • Life Cycle Hooks
    • Logging
    • Metrics
      • OpenTelemetry
      • Prometheus
    • Middleware
      • Using Middleware
      • Built-in middleware
      • Creating Middleware
    • OpenAPI
      • Configuring schema generation
      • Configuring schema generation on a route handler
      • Accessing the OpenAPI schema in code
      • Customizing Pydantic model schemas
      • Customizing Operation class
      • Generating examples
      • OpenAPI UI Plugins
      • Using OpenAPI UI Plugins
      • Configuring OpenAPI UI Plugins
      • Configuring the OpenAPI Root Path
      • Building your own OpenAPI UI Plugin
      • Interacting with the Router
      • OAuth2 in Swagger UI
      • Customizing the OpenAPI UI
      • CDN and offline file support
    • Plugins
      • Flash Messages
      • Problem Details
    • Responses
    • Security
      • Implementing Custom Authentication
      • Security Backends
      • Guards
      • Excluding and including endpoints
      • JWT Security Backends
      • Handling Secrets
    • Static files
    • Custom types
    • Stores
    • Templating
    • Testing
    • WebSockets
  • API reference
    • app
    • background_tasks
    • channels
      • plugin
      • subscriber
      • backends
        • base
        • memory
        • redis
        • psycopg
        • asyncpg
    • cli
    • config
    • connection
    • contrib
      • HTMX
      • jinja
      • jwt
      • mako
      • opentelemetry
      • piccolo_orm
      • pydantic
      • sqlalchemy
        • plugins
        • repository
        • types
        • base
        • DTO
    • controller
    • concurrency
    • data_extractors
    • datastructures
      • secret_values
    • di
    • dto
      • config
      • data_structures
      • field
      • types
      • base_dto
      • msgspec_dto
      • dataclass_dto
    • enums
    • events
    • exceptions
    • file_system
    • handlers
    • logging
      • config
      • picologging
      • standard
    • middleware
      • allowed_hosts
      • authentication
      • compression
      • cors
      • csrf
      • logging
      • rate_limit
      • session
        • base
        • client_side
        • server_side
    • openapi
      • openapi
      • plugins
      • spec
    • pagination
    • params
    • plugins
      • attrs
      • flash
      • htmx
      • problem details
      • prometheus
      • pydantic
      • structlog
      • sqlalchemy
    • repository
      • abc
      • filters
      • exceptions
      • testing
      • handlers
    • response
      • base
      • file
      • redirect
      • streaming
      • SSE (Server Sent Events)
      • template
    • router
    • routes
    • security
      • jwt
      • session_auth
    • serialization
    • static_files
    • status_code
    • stores
      • base
      • file
      • memory
      • redis
      • registry
      • valkey
    • template
    • testing
    • types
    • typing
  • Benchmarks

Guides

  • Migrating to Litestar
    • From Flask
    • From Starlette / FastAPI
  • Topics
    • Sync vs. Async
    • Deployment
      • NGINX Unit
      • Manually with ASGI server
      • Docker
      • Supervisor (Linux)
  • Tutorials
    • Developing a basic TODO application
      • Application basics
      • Accessing the list
      • Making the list interactive
      • Recap and assembling the final application
    • Improving the TODO app with SQLAlchemy
      • Introduction
      • Providing the session with DI
      • Using the serialization plugin
      • Using the init plugin
      • Final touches and recap
    • Data Transfer Object Tutorial
      • Our first DTO
      • Excluding from nested models
      • Excluding from collections of nested models
      • Max nested depth
      • Renaming fields
      • Receiving data
      • Read only fields
      • Accessing the data
      • Updating instances
      • Declaring DTOs on app layers
    • SQLAlchemy Repository Tutorial
      • Introduction to Database Modelling and Repository Features
      • Modelling
      • Full Code
      • Interacting with repositories
      • Working with Bulk Data Operations
      • Full Code
      • Working with Controllers and Repositories
      • Full Code
      • Adding Additional Features to the Repository
      • Slug Fields
      • Full Code

Contributing

  • Contribution Guide
  • Available Issues
  • Code of Conduct
litestar-org/litestar 0 0
Edit this page
  1. Litestar /
  2. Usage /
  3. Middleware

Middleware¶

Middlewares in Litestar are ASGI apps that are called “in the middle” between the application entrypoint and the route handler function.

Litestar ships with several builtin middlewares that are easy to configure and use. See the documentation regarding these for more details.

See also

If you’re coming from Starlette / FastAPI, take a look at the migration guide:

  • Migration - FastAPI/Starlette - Middlewares

  • Using Middleware
  • Built-in middleware
  • Creating Middleware
Previous
Prometheus
Next
Using Middleware

2025, Litestar Organization

Made with Sphinx and Shibuya theme.