Skip to content

Installation

Install fastapi-guard using pip:

pip install fastapi-guard

Note: Ensure you have Python 3.10 or higher installed.

Prerequisites

Before using fastapi-guard, obtain an IPInfo token:

  1. Visit IPInfo's website to create a free account.
  2. After signing up, you'll receive an API token.
  3. The free tier includes:
  4. Up to 50,000 requests per month.
  5. Access to IP to Country database.
  6. Daily database updates.
  7. IPv4 & IPv6 support.

Usage Example:

from fastapi import FastAPI
from fastapi_guard.middleware import SecurityMiddleware
from fastapi_guard.models import SecurityConfig

app = FastAPI()
config = SecurityConfig(
    ipinfo_token="your_ipinfo_token_here", # Required for IP geolocation
    whitelist=["192.168.1.1"],
    blacklist=["10.0.0.1"],
    blocked_countries=["AR", "IT"],
    blocked_user_agents=["curl", "wget"],
    auto_ban_threshold=5,
    auto_ban_duration=86400,
    custom_log_file="security.log",
)

app.add_middleware(SecurityMiddleware, config=config)