Installation¶
Install fastapi-guard
using pip:
Note: Ensure you have Python 3.10 or higher installed.
Prerequisites¶
Before using fastapi-guard
, obtain an IPInfo token:
- Visit IPInfo's website to create a free account.
- After signing up, you'll receive an API token.
- The free tier includes:
- Up to 50,000 requests per month.
- Access to IP to Country database.
- Daily database updates.
- 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)