Skip to main content

[WIP] FastLimits is a rate limiting extension/middleware for FastAPI applications powered by limits.


Documentation: https://astronuttt.github.io/fastlimits

Source Code: https://github.com/astronuttt/fastlimits

Features

  • FastAPI depencies support: FastAPI dependencies can be used in limiters to apply limits based on the injected dependency.

  • Automatic documentation for additional responses: It will automatically add additional 429_TOO_MANY_REQUESTS response to the schema, so it can be used in the generated documentation.

  • Middlewares support: Can act as a middleware to apply limits to all the routes and etc.

  • Partial and full limits: limits can be applied to an APIRouter the whole FastAPI app or a single route operation.

  • Exclude certain responses: certain responses can be excluded from the limitation. for example, you can exclude 400 status codes and if the route returns a 400 it won't be counted for limitations.

  • Limit groups: You can apply a group of limits, for example a certain limit for admin users, and a certain limit for normal users.

  • Multiple limit strategies: It uses the limits library on the back-end so it supports multiple strategies. see Supported Strategies.

  • Multiple Storage backends: As another perk of the limits library, multiple storage backends are supported. see Storage backends

How it works

It uses the limits library in the backend for all the limitation handlings.

As for the FastAPI side it Injects a dependency into the APIRoute object and that dependency acts as the main component for limitation on that route. one of it's perks are that you can apply some limitations based on a Depends and everything will be handled automatically by FastAPI.

Other limit libraries

Some of the ideas for this library comes from two great libraries that were created before this:

SlowApi: https://github.com/laurents/slowapi

fastapi-limiter: https://github.com/long2ice/fastapi-limiter

These are two great libraries, but each of them lack some functionality, so I decided to extend them and create FastLimits.

Quick Start

This library works by a combination of a dependency and a middleware, but using it is very simple.

First step is to setup the Limiter and then add the limiting middleware to your FastAPI application.

from fastapi import FastAPI

# The simplest setup is to use 'MemoryStorage' and 'FixedWindowRateLimiter', you don't even have to specify these because it's the default
from limits.aio.storage import MemoryStorage
from limits.aio.strategies import FixedWindowRateLimiter

from fastlimits import RateLimitingMiddleware, limit


app = FastAPI()


limiter = FixedWindowRateLimiter(storage=MemoryStorage())
app.add_middleware(RateLimitingMiddleware, strategy=limiter)

# and thats for the setup! you can now use the 'limit' decorator to apply limits on any route

@limit(app, "5/minute")
@app.get("/")
async def items(q: int | None = None):
    return {"q": q}

And thats it! now users can call the GET 'items' route only 5 times per minute.

For more usage please refer to the documentation at https://astronuttt.github.io/fastlimits


This library inherits most of it's functionality from limits. special thanks to the everyone who helped make that happen.

License

This project is licensed under the terms of the MIT license.

Release files for fastlimits 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastlimits 0.0.1
File Size Uploaded
fastlimits-0.0.1.tar.gz 298.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastlimits 0.0.1
File Interpreter ABI Platform
fastlimits-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 310.8 kB

Release files / fastlimits-0.0.1.tar.gz

Download URL fastlimits-0.0.1.tar.gz
Size 298.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6cd6a8ffc9e179ee30de08c1786e14be42b422d3704cf6f040c5251148d59c52
BLAKE2b-256 checksum
How to use checksums
ac5d9e835c19db7760efb765548e3fef3678472a34ab7e5c549bb119f00879f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.19.1 CPython/3.12.5 Linux/6.9.3-76060903-generic

Release files / fastlimits-0.0.1-py3-none-any.whl

Download URL fastlimits-0.0.1-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ed37bfa1db50685b770b341d61c2bfad8fb7fc91d9f560940dd7dc05ccd1f51
BLAKE2b-256 checksum
How to use checksums
9b96e9b77de5672c06bf792b54d7de98c4b1e02de06cc94225dc84e0fbfcc9f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.19.1 CPython/3.12.5 Linux/6.9.3-76060903-generic

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page