framework

Browser fingerprinting in Django

Add browser fingerprinting and bot, disposable email and IP checks to a Django signup view: the SDK as a static file, one check call with Requests, and the right client IP.

This guide adds OpenFingerprint to a Django signup view. The browser runs the SDK, which posts its evidence to the API's public identify listener, and puts the identification ID it gets back in a hidden field; the view sends the identification ID, the email address and the visitor's IP to the check API with your secret key, and decides what to do with the answer. The listener sees the browser connect, and your key never reaches the browser.

1. Get the SDK script and add your key

The SDK is a JavaScript package with a standalone build that defines OpenFingerprint.identify. Copy it into your static files once, and again when you upgrade:

sh
npm pack @openfingerprint/sdk
tar -xzf openfingerprint-sdk-*.tgz package/dist/index.iife.js
mv package/dist/index.iife.js yourapp/static/openfingerprint.js

Create a secret key under API keys in the dashboard and set it in the server's environment as OPENFINGERPRINT_KEY. The view below uses Requests (pip install requests).

2. Identify in the browser

Start watching and collecting when the page loads, and fill the hidden fields before the form is sent. Collect only once your disclosure or consent requirements are met.

html
{% load static %}
<form id="signup" method="post" action="{% url 'signup' %}">
	{% csrf_token %}
	<input type="email" name="email" required />
	<input type="hidden" name="of_identification" />
	<button>Sign up</button>
</form>
<script src="{% static 'openfingerprint.js' %}"></script>
<script>
	const form = document.getElementById('signup');
	OpenFingerprint.watch();
	const collecting = OpenFingerprint.identify({
		endpoint: 'https://events.openfingerprint.com/v1/identify'
	});
	let ready = false;
	form.addEventListener('submit', async (event) => {
		if (ready) return;
		event.preventDefault();
		try {
			const { identificationId } = await collecting;
			form.elements.of_identification.value = identificationId ?? '';
		} catch {}
		ready = true;
		form.requestSubmit();
	});
</script>

If posting fails, identificationId is null and the form still submits. The server omits that ID; email and IP checks continue, and browser evidence stays unknown, not suspicious. There is no local-evidence fallback. If your site sets a Content-Security-Policy, allow https://events.openfingerprint.com in connect-src.

3. Check on the server

The view calls POST /v1/check. The API never reads an IP from the connection: your server passes the address it saw. Django doesn't read X-Forwarded-For for you. With exactly one reverse proxy in front of the app that appends the visitor's address (nginx's proxy_add_x_forwarded_for does), use the last entry; without a proxy, use REMOTE_ADDR. Never trust the first entry, which the browser can set.

python
# views.py
import os

import requests
from django.http import HttpResponseBadRequest
from django.shortcuts import redirect

CHECK_URL = "https://api.openfingerprint.com/v1/check"


def client_ip(request):
    forwarded = request.META.get("HTTP_X_FORWARDED_FOR")
    if forwarded:
        return forwarded.split(",")[-1].strip()  # added by our one proxy
    return request.META.get("REMOTE_ADDR")


def check(browser, email, ip):
    try:
        response = requests.post(
            CHECK_URL,
            json={**browser, "email": email, "ip": ip},
            headers={"x-api-key": os.environ["OPENFINGERPRINT_KEY"]},
            timeout=5,
        )
        response.raise_for_status()
        return response.json()
    except requests.RequestException:
        return {}


def signup(request):
    email = request.POST.get("email", "")
    browser = {"identificationId": request.POST.get("of_identification") or None}
    result = check(browser, email, client_ip(request))

    if result.get("disposable"):
        return HttpResponseBadRequest("Please use a permanent email address.")
    verify_first = result.get("automation") or result.get("spoofing") or result.get("replayed") or result.get("tor")
    # create the account, store result.get("visitorId") on it only when present, and ask for
    # email verification before any free credits when verify_first is true
    return redirect("welcome")

A failed or slow check returns {}, so signups keep working when the API can't be reached.

4. Decide

The response is flat: a key is present only when it is known, so a missing disposable means unknown, never "clean".

  • disposable is strong evidence: ask for another address.
  • automation, spoofing (the browser's report contradicts its headers or TLS handshake, as when a script poses as a browser), replayed (the same identification looked up twice) and tor are reasons to ask for more proof, such as email verification or a challenge, not to block.
  • behavior lists input patterns since watch(), such as clicks_without_movement. Keyboard users, autofill and password managers match some of them, so treat them as context for your own rules.
  • visitorId is a random server-issued browser grouping, not a fingerprint hash. Store it only when present and count recent accounts with the same ID. It can split repeat visits or merge indistinguishable browsers; missing IDs stay unknown. identificationId references one hour-long observation and is not the value to store on accounts.

The free trial abuse guide has an example policy, and the check API reference lists every key.

Self-hosting

The SDK and the check API are MIT licensed. To run the API yourself, follow Deploy with Helm, replace the URL with your own service, drop the x-api-key header, and point endpoint at your own mandatory public identify listener. Configure identify.origins and identify.certificates, and use TCP passthrough so it sees the browser's TLS handshake. The internal check API has no authentication, so keep it on a private network. To receive visitor IDs, configure PostgreSQL, apply the visitor migrations and keep the deployment namespace stable. A single-customer install can set postgres.visitorScope; shared hosts should require backend-derived customer scopes. See visitor IDs and scopes.

Related

Read the code before you trust it.

The SDK, the server checks, the rules and the dataset tooling are MIT. Self-host them for free, or call the hosted API.