IP Geolocation¶
FastAPI Guard uses IPInfo's database for IP geolocation and country-based filtering.
Setup¶
- Get your IPInfo token from ipinfo.io
- Configure geolocation in your app:
config = SecurityConfig(
ipinfo_token="your_ipinfo_token_here",
blocked_countries=["CN", "RU"], # Block specific countries
whitelist_countries=["US", "CA"] # Optional: only allow specific countries
)
Country Blocking¶
Block requests from specific countries using ISO 3166-1 alpha-2 country codes:
config = SecurityConfig(
ipinfo_token="your_ipinfo_token_here",
blocked_countries=[
"CN", # China
"RU", # Russia
"IR", # Iran
"KP" # North Korea
]
)
Country Whitelisting¶
Only allow requests from specific countries:
config = SecurityConfig(
ipinfo_token="your_ipinfo_token_here",
whitelist_countries=[
"US", # United States
"CA", # Canada
"GB", # United Kingdom
"AU" # Australia
]
)
Custom Geolocation Logic¶
You can also use the IPInfoManager
directly for custom geolocation logic: