slowapi

Rate limiting for Starlette and FastAPI endpoints, adapted from Flask-Limiter.

Library
PyPI
v0.1.10
2,068 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
55 /100 Fair
Development Activity 48
Maintenance 20
Community 52
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
69 /100 Good
Architecture 72
Code Quality 68
Innovation 62
Learning Curve 75

slowapi is a rate-limiting library for Starlette and FastAPI, adapted directly from flask-limiter. It wraps the limits package to enforce per-route, per-method, shared-group, or application-wide rate limits, and plugs into Starlette’s exception-handling system to return 429 responses with standard rate-limit headers.

The library is used in production setups handling millions of requests per month. It supports both sync and async endpoints, multiple storage backends (in-memory, Redis, Memcached) with automatic in-memory fallback, and custom key functions for deriving rate-limit identity from IP address, auth tokens, or any request attribute.

What You Get

  • A Limiter class you instantiate once with a key_func and attach to app.state.limiter.
  • A @limiter.limit(...) decorator for applying per-route or per-method limits to sync and async endpoints.
  • Pluggable storage backends (in-memory, Redis, Memcached) via the underlying limits library, with in-memory fallback if the backend goes down.
  • A ready-made RateLimitExceeded exception and Starlette exception handler that returns 429 JSON responses with X-RateLimit-* headers.

Common Use Cases

  • Protecting public FastAPI APIs from scraping and abuse
  • Throttling login and password-reset endpoints against credential stuffing
  • Applying different limits to different route groups via shared limit scopes
  • Setting application-wide default limits while overriding specific hot endpoints

Under The Hood

Architecture The library layers a small set of focused modules: extension.py defines the Limiter class that owns configuration, storage-backend selection, and per-request limit checking; wrappers.py defines Limit and LimitGroup, the abstraction that turns a limit string or callable into concrete Limit objects at request time; middleware.py implements an ASGI/Starlette middleware path for auto-checking limits outside the decorator flow; and errors.py defines RateLimitExceeded, a starlette.exceptions.HTTPException subclass that Starlette’s own exception-handling machinery can route to a 429 JSON response. A decorated endpoint call flows through LimitGroup.__iter__ to produce Limit instances, which Limiter._check_request_limit evaluates against the configured storage strategy from the limits package, raising RateLimitExceeded on breach. Because both the decorator path and the middleware path funnel through the same Limit/LimitGroup abstraction, changing that core representation would ripple through request checking, header injection, and exemption logic alike.

Tech Stack Written for Python 3.7+ and managed with Poetry, the package’s only required runtime dependency is limits (>=2.3), which supplies the actual rate-limiting strategies and storage backends; redis is an optional extra for the Redis-backed storage option. It builds directly on Starlette’s ASGI primitives (Request, Response, BaseHTTPMiddleware) and works transparently with FastAPI since FastAPI is itself built on Starlette. Development tooling includes black, mypy, isort, and flake8 for style and typing, pytest with coverage for testing, and mkdocs with mkautodoc for the docs site published on Read the Docs; CI runs this full chain across Python 3.9-3.11 via GitHub Actions.

Code Quality Test coverage is meaningful rather than superficial: test_fastapi_extension.py and test_starlette_extension.py each exercise the decorator across single/multiple limits, headers, exemptions, and both sync and async handlers, backed by shared fixtures in tests/__init__.py. Type hints are used extensively and checked with mypy in CI, though several # type: ignore markers on the limits import point to gaps in the upstream library’s own typing. CI only enforces the F401 (unused-import) flake8 rule rather than a full lint pass, and there’s no visible coverage-percentage gate, so quality leans on the type checker and the decorator-level test suite more than exhaustive static analysis.

API Design The public surface is deliberately small and mirrors flask-limiter’s API: construct a Limiter with a key_func, decorate endpoints with @limiter.limit(...), and register the provided exception handler. This keeps onboarding low for anyone coming from Flask, but it carries one documented rough edge — the request argument must be explicitly present in the endpoint signature or the decorator cannot hook into it, a constraint the README calls out directly rather than handling transparently. Documentation is present but thin: an api.md and examples.md on Read the Docs cover the essentials without deep guides for advanced backend or key-function customization.

Used by 6 apps in this directory

Python
64%
Other

Keep

Automation · Devops · Monitoring

12,381

The open-source AIOps and alert management platform that unifies 130+ monitoring tools into a single pane of glass with AI-powered correlation, deduplication, and workflow automation.

View details
87
Repo Health
79
Technical
66
Dependency
Built with
Python 64%
TypeScript 36%
Updated 1 weeks ago
Python
69%
MIT

Langflow

AI Agents · AI Development

155,451

Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.

View details
90
Repo Health
85
Technical
64
Dependency
Built with
Python 69%
TypeScript 22%
Updated today
Python
59%
Apache 2.0

MLflow

AI Development · Monitoring

28,331

The open source AI engineering platform for debugging, evaluating, monitoring, and optimizing production LLMs and agents at scale.

View details
96
Repo Health
86
Technical
66
Dependency
Built with
Python 59%
TypeScript 33%
Updated today
JavaScript
40%
MIT

NoteDiscovery

Knowledge Management · Note Taking · Productivity

2,831

A self-hosted markdown knowledge base with MCP-powered AI integration, graph views, LaTeX math, and built-in drawing — your notes stay on your server, forever.

View details
80
Repo Health
59
Technical
74
Dependency
Built with
JavaScript 40%
Python 32%
HTML 25%
Updated yesterday
Python
64%
AGPL 3.0

Shadowbroker

Analytics · Monitoring · Security

11,296

Self-hosted OSINT dashboard that fuses 60+ live intelligence feeds — flight tracking, ship AIS, satellites, CCTV, seismic and radio networks — into one real-time map, with an agent-ready command channel for AI co-analysts.

View details
85
Repo Health
83
Technical
71
Dependency
Built with
Python 64%
TypeScript 32%
Updated today
Python
68%
Apache 2.0

SurfSense

AI Assistants · Search

16,342

The open-source, unlimited NotebookLM alternative with real-time collaboration, a desktop app, and no vendor lock-in.

View details
88
Repo Health
71
Technical
66
Dependency
Built with
Python 68%
TypeScript 29%
Updated yesterday

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers