Skip to main content

FastAPI/Starlette-native wrapper for python-ipware to get client IP addresses

Project description

fastapi-ipware

A FastAPI/Starlette-native wrapper for python-ipware that eliminates the need for WSGI-style header conversion.

Features

  • Zero conversion overhead - Headers converted once at initialization, not on every request
  • FastAPI-native API - Works directly with FastAPI/Starlette Request objects
  • Customizable precedence - Easy to configure header priority for your infrastructure
  • Proxy validation - Supports trusted proxy lists and proxy count validation
  • Thin wrapper - Leverages all the battle-tested logic from python-ipware

Installation

pip install fastapi-ipware

Or with uv:

uv add fastapi-ipware

Quick Start

from fastapi import FastAPI, Request
from fastapi_ipware import FastAPIIpWare

app = FastAPI()
ipware = FastAPIIpWare()

@app.get("/")
async def get_ip(request: Request):
    ip, trusted = ipware.get_client_ip_from_request(request)
    
    if ip:
        return {
            "ip": str(ip),
            "trusted": trusted,
            "is_public": ip.is_global,
            "is_private": ip.is_private,
        }
    
    return {"error": "Could not determine IP"}

Usage

Basic Usage

from fastapi_ipware import FastAPIIpWare

# Use default configuration (optimized for FastAPI/cloud deployments)
ipware = FastAPIIpWare()

ip, trusted = ipware.get_client_ip_from_request(request)

Custom Header Precedence

Customize which headers are checked and in what order:

# Prioritize Cloudflare headers
ipware = FastAPIIpWare(
    precedence=(
        "CF-Connecting-IP",
        "X-Forwarded-For",
        "X-Real-IP",
    )
)

# NGINX configuration
ipware = FastAPIIpWare(
    precedence=(
        "X-Real-IP",
        "X-Forwarded-For",
    )
)

Proxy Count Validation

Validate that requests pass through the expected number of proxies:

# Expect exactly 1 proxy (e.g., AWS ALB)
ipware = FastAPIIpWare(proxy_count=1)

# In strict mode, must be exactly 1 proxy
ip, trusted = ipware.get_client_ip_from_request(request, strict=True)

# In non-strict mode, allow 1 or more proxies
ip, trusted = ipware.get_client_ip_from_request(request, strict=False)

Trusted Proxy List

Validate that requests pass through specific trusted proxies:

# Trust specific proxy IP prefixes
ipware = FastAPIIpWare(
    proxy_list=["10.0.", "10.1."]  # AWS internal IPs
)

ip, trusted = ipware.get_client_ip_from_request(request)

# trusted=True only if request came through specified proxies

Combined Validation

Use both proxy count and trusted proxy list:

# Expect 1 proxy from a specific IP range
ipware = FastAPIIpWare(
    proxy_count=1,
    proxy_list=["10.0."]
)

Real-World Examples

AWS Application Load Balancer

ipware = FastAPIIpWare(
    proxy_count=1,
    proxy_list=["10.0."]  # Your VPC CIDR
)

Cloudflare

ipware = FastAPIIpWare(
    precedence=("CF-Connecting-IP",)
)

Multiple Proxies (CDN + Load Balancer)

ipware = FastAPIIpWare(
    proxy_count=2,
    proxy_list=["10.1.", "10.2."]  # CDN and LB IPs
)

NGINX Reverse Proxy

ipware = FastAPIIpWare(
    precedence=("X-Real-IP", "X-Forwarded-For"),
    proxy_count=1
)

IP Address Types

The returned IP address object has useful properties:

ip, _ = ipware.get_client_ip_from_request(request)

if ip:
    print(f"Is public: {ip.is_global}")
    print(f"Is private: {ip.is_private}")
    print(f"Is loopback: {ip.is_loopback}")
    print(f"Is multicast: {ip.is_multicast}")

python-ipware automatically prefers:

  1. Public (global) IPs first
  2. Private IPs second
  3. Loopback IPs last

Default Header Precedence

The default precedence order is optimized for modern cloud deployments. See the default precedence configuration in the source code.

Why fastapi-ipware?

python-ipware expects WSGI-style headers (HTTP_X_FORWARDED_FOR), but FastAPI uses natural header names (X-Forwarded-For). This wrapper handles the conversion automatically so you don't have to.

Contributing

Contributions welcome! This is a thin wrapper around python-ipware, so most IP detection logic lives there.

License

MIT License

Credits

Built on top of python-ipware by un33k.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_ipware-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_ipware-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_ipware-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_ipware-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for fastapi_ipware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63ffae371fb63522eb3d682aeefbcf998bb11e8646b5c3fc3365da228838212f
MD5 5ee738cf30f876f964d015719f504b8b
BLAKE2b-256 464456ad397367db534a34a1a8218b91199260d7bdddc2921635c2a3343558b7

See more details on using hashes here.

File details

Details for the file fastapi_ipware-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_ipware-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b71430d9316637f1e1bfb0947a60ae0dfa5878137c5f3bfeb07fd5c7ad29f5f7
MD5 b1ad1a9c8de4e9a65d323c9f3c9f6721
BLAKE2b-256 663eae2464f66a2e41ff7d8ce87a5ce8329945ffc2027b507fb3d92c8b2e9da8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page