Skip to content

cafeTechne/flask_limiter_firestore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”§ Features

  • βœ… Drop-in replacement for Redis/Memcached backends
  • ☁️ Firestore-compatible (GCP-managed, serverless, global scale)
  • 🧹 Built-in TTL auto-cleanup via expires_at field
  • πŸ” No extra infrastructure needed on Google App Engine/Cloud Run
  • πŸ§ͺ Fully compatible with Flask-Limiter β‰₯3.5+

πŸ“¦ Installation

pip install Flask-Limiter-Firestore

πŸš€ Usage

from flask import Flask, request from flask_limiter import Limiter from flask_limiter_firestore import FirestoreStorage

def get_client_ip():
xff = request.headers.get("X-Forwarded-For", "") return xff.split(",")[0].strip() if xff else request.remote_addr

app = Flask(__name__) limiter = Limiter(

app=app, key_func=get_client_ip, storage=FirestoreStorage(collection_name="rate_limits"), default_limits=["10 per minute"],

)

πŸ” Authentication

If you're running locally, authenticate with:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json

On GAE / Cloud Run / Cloud Functions: ADC is automatic.

βœ… Example Route

@app.route("/api/data") @limiter.limit("5 per minute") def data():

return "Rate limited!"

🧹 Cleanup Policy

Use a TTL index on the expires_at field in Firestore for auto-deletion.

πŸ› Troubleshooting

  • FirestoreStorage.incr() got unexpected keyword 'amount' β†’ Upgrade Flask-Limiter β‰₯ 3.5
  • Invalid document key β†’ Avoid slashes in limiter keys
  • 'wrap_exceptions' attribute missing β†’ ensure base_exceptions property exists

πŸ”— Links

πŸ“„ License

MIT License

About

A Firestore backend for Flask-Limiter that enables serverless, distributed rate limiting without Redis or Memcached.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages