Skip to main content

VPNDetection VPNDetection FastAPI Middleware

PyPI license

The official FastAPI middleware for the VPNDetection API: it checks each visitor's IP for VPN, Tor and proxy use, and can block them with a 403.

It classifies the visitor behind each request: VPN, residential proxy, Tor, hosting, CDN or relay. Then it hands the answer to your code. Blocking is opt-in.

Getting Started

pip install vpndetection-fastapi

Requires Python 3.11 or newer.

No API key needed to start. Without one, a lookup is on Unlimited, the free plan, which answers VPN, Tor and relay detection with no daily or monthly cap. A paid plan's key adds the hosting, CDN and proxy flags and the provider behind each flag: create one in the console. A free account tries every field.

from fastapi import FastAPI
from vpndetection_fastapi import VPNDetectionMiddleware

app = FastAPI()
app.add_middleware(VPNDetectionMiddleware, api_key=os.environ["VPNDETECTION_API_KEY"])

Pure ASGI, so this works on Starlette and Litestar too, not only FastAPI.

from fastapi import Request
from vpndetection_fastapi import lookup


@app.get("/")
async def index(request: Request):
    found = lookup(request)
    return {"vpn": found.result.is_vpn}

A request your skip claims gets no answer, and lookup(request) is None for it.

By default nothing is blocked. Every request gets an answer and your own code decides what to do with it, since whether a VPN visitor is a problem depends on what they're doing.

Blocking

Pass a block_condition and a matching request is answered with 403 and never reaches your code.

block_condition = {"is_vpn": True}

A condition is written in the shape of a result, keyed by the same names the API uses, and only the members you name are considered. That lets it reach the evidence, not just the flags:

{"is_vpn": True, "vpn": {"provider": "nordvpn"}}  # one provider
{"is_resproxy": True, "resproxy": {"hits": {"gte": 5}}}  # a numeric threshold
{"vpn": {"confidence": ["high", "medium"]}}  # any of these
[{"is_tor": True}, {"is_resproxy": True}]  # a list is OR

Values are matched by equality, strings without regard to case. A list means any-of. A dict of gte/gt/lte/lt compares numbers, and every bound you give must hold, so two of them are a range. A member the answer holds as a list (web, pubproxy.protocols, bittorrent.clients, bot.agents, bot.categories) is never matched, so block on its flag alone. Members set to False or None are ignored, so a condition states the signals you act on; one that constrains nothing would match every request, and is refused when the middleware is built rather than silently blocking all your traffic.

Replace the refusal with on_blocked.

Blocking one endpoint

block_condition applies to every endpoint. In FastAPI, refuse a visitor on some endpoints only with the block_if dependency, on a path operation, a router or the app:

from fastapi import Depends
from vpndetection_fastapi import block_if


@app.get("/checkout", dependencies=[Depends(block_if({"is_vpn": True}))])
async def checkout(): ...

A refused request gets an HTTPException, 403 with {"detail": "access denied"} unless you pass status_code and detail. Declared as a parameter, the dependency hands the endpoint the answer. block_if takes the same condition, fail_closed and on_missing_field. It judges the answer the middleware already attached, so a visitor is looked up once however many endpoints check them, and it warns once for a member your plan doesn't include. A request skip claimed reaches the endpoint. The middleware still has to be added: without it, the dependency raises RuntimeError instead of letting everyone through.

Where the client address comes from

Get this setting right, or none of the above works.

By default the middleware uses request.client.host. uvicorn, and fastapi run with it, reads proxy headers by default, but only from a peer that --forwarded-allow-ips trusts, and that defaults to 127.0.0.1,::1 (or $FORWARDED_ALLOW_IPS). So behind a proxy on the same machine, the visitor's address arrives with no setup. Behind one anywhere else, such as nginx in its own container or a load balancer in a VPC, every visitor looks like the proxy. That address is usually a private one, which the middleware answers locally: nobody is flagged, nothing is blocked, and the private-address warning below is the only sign. Name your proxy, by its address or its subnet: --forwarded-allow-ips=172.28.5.0/28. gunicorn's own --forwarded-allow-ips works the same way. Never pass *. It trusts every peer, so the left-most X-Forwarded-For entry wins, and that is whatever the visitor sent: someone on a VPN who sends X-Forwarded-For: 1.1.1.1 is looked up as 1.1.1.1.

For an edge that writes the address into its own header, name the header:

from vpndetection_fastapi import header_ip_selector

ip_selector = header_ip_selector("CF-Connecting-IP")  # or True-Client-IP, or your own

xff_ip_selector() reads X-Forwarded-For directly. The left-most entry is whatever the caller sent, since proxies append to that header, so trust it only when an edge you control overwrites it. If you know how many proxies sit in front, count from the right instead: xff_ip_selector(1) is the address your nearest proxy saw.

Anything else, pass your own callable. It receives the request and returns an address.

If the address resolves to a private one, the middleware says so once through the vpndetection logger. That is expected on localhost and is the signal to fix your configuration anywhere else.

When a lookup fails

The request is let through, and the reason is recorded on the answer's error. Our outage should not become yours, so a network failure, an exhausted quota or a rejected key all fail open. Pass fail_closed=True to block instead. Private addresses are answered locally and never fail, so this will not lock you out in development.

Cost and latency

Answers are cached for an hour, so a returning visitor costs nothing, and private addresses never leave the process. A cache miss is one request to our API, bounded at 2.5 seconds by default and not retried: on a request path, failing open fast beats holding a visitor while we retry. Both are adjustable, as is the cache, through a vpndetection client you build yourself and pass as client.

Skip what you do not care about:

skip = lambda request: request.url.path.startswith("/static")

Beyond a few million distinct visitors a day, stop calling the API per request: download the dataset and look addresses up locally instead.

Absent is not false

Unlimited, with a key or without one, leaves out is_hosting, is_cdn, the four proxy flags, the carrier, satellite and service flags, bot, company and abuse, and of the detail behind each flag it answers only Tor's and relay's. A field your plan does not include is None, which means "not in your plan" rather than "checked, and no".

lookup(request).result.is_hosting  # None when your plan does not include it

A block_condition naming a member your plan does not serve can never match, so the middleware warns once instead of failing silently. Pass on_missing_field="raise" to make it an error.

Other Libraries

There are official VPNDetection client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. See our GitHub at https://github.com/vpndetection-io for more.

About VPNDetection

VPN Detection API: Accurate anonymity detection identifying VPNs, residential proxies, hosting servers, Tor nodes, CDNs, relays and more.

VPNDetection

License

This project is licensed under the MIT License.

Metadata

Release files for vpndetection-fastapi 2.1.29

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vpndetection-fastapi 2.1.29
File Size Uploaded Provenance Actions
vpndetection_fastapi-2.1.29.tar.gz 46.6 kB With provenance Details for vpndetection_fastapi-2.1.29.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vpndetection-fastapi 2.1.29
File Interpreter ABI Platform Actions
vpndetection_fastapi-2.1.29-py3-none-any.whl Python 3 none any Details for vpndetection_fastapi-2.1.29-py3-none-any.whl

Total release size: 55.5 kB

Release files / vpndetection_fastapi-2.1.29.tar.gz

Download URL vpndetection_fastapi-2.1.29.tar.gz
Size 46.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6160c9d1bb118f2b2c4683e3132d21f1c9f6f4a5a24ec763b676c3e45d0a4b4e
BLAKE2b-256 checksum
How to use checksums
1db4a781a8fe6193230aa5a918f487d071a3a6ece5d8b0df77faecc86281b6a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / vpndetection_fastapi-2.1.29-py3-none-any.whl

Download URL vpndetection_fastapi-2.1.29-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a6884f89e4a40b0107f253b4c67bef383bfae2492ace7a72fdfb4758650ea04
BLAKE2b-256 checksum
How to use checksums
ee8ae4c52eee303f671f1c7c29194a7610810c0c0f08139e4c5b8a68740f0d8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for vpndetection-fastapi 2.1.29

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vpndetection_fastapi-2.1.29.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.1.29 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page