framework

Browser fingerprinting in SvelteKit

Add browser fingerprinting and bot, disposable email and IP checks to a SvelteKit signup form: the SDK in onMount, one check call in a form action, and getClientAddress behind a proxy.

This guide adds OpenFingerprint to a SvelteKit signup form. The browser runs the SDK, which posts its evidence to the API's public identify listener, and adds the identification ID it gets back to the form data; a form action 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. The examples use SvelteKit 3; the notes mark what differs in 2.

1. Install the SDK and add your key

sh
npm install @openfingerprint/sdk

Create a secret key under API keys in the dashboard and put it in .env as OPENFINGERPRINT_KEY, without a PUBLIC_ prefix so it stays on the server. SvelteKit 3 reads server variables you declare in src/env.ts:

ts
// src/env.ts
import { defineEnvVars } from '@sveltejs/kit/env';

export const variables = defineEnvVars({
	OPENFINGERPRINT_KEY: { description: 'Secret key from the OpenFingerprint dashboard.' }
});

On SvelteKit 2, skip this file and import env from $env/dynamic/private instead.

2. Identify in the browser

Start watching and collecting when the form mounts, so the identification is usually ready by the time someone presses the button. use:enhance runs before each submit, so it adds the ID to the form data then. Collect only once your disclosure or consent requirements are met.

text
<!-- src/routes/signup/+page.svelte -->
<script lang="ts">
	import { enhance } from '$app/forms';
	import { identify, watch, type Identification } from '@openfingerprint/sdk';
	import { onMount } from 'svelte';

	let { form } = $props();
	let collecting: Promise<Identification> | null = null;

	onMount(() => {
		watch();
		collecting = identify({ endpoint: 'https://events.openfingerprint.com/v1/identify' });
	});
</script>

<form
	method="POST"
	use:enhance={async ({ formData }) => {
		const result = await collecting?.catch(() => null);
		if (result?.identificationId) formData.set('of_identification', result.identificationId);
	}}
>
	<input name="email" type="email" required />
	<button>Sign up</button>
	{#if form?.error}<p>{form.error}</p>{/if}
</form>

onMount runs only in the browser, so server rendering never starts a collection. If posting fails, or the form is submitted without JavaScript, identificationId is missing 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 form action calls POST /v1/check with the identification ID, the email address and the visitor's IP. The API never reads an IP from the connection: your server passes the address it saw, which in SvelteKit is getClientAddress().

ts
// src/routes/signup/+page.server.ts
import { OPENFINGERPRINT_KEY } from '$app/env/private';
import { fail } from '@sveltejs/kit';
import type { Actions } from './$types';

type Check = {
	visitorId?: string;
	replayed?: boolean;
	automation?: boolean;
	spoofing?: boolean;
	disposable?: boolean;
	tor?: boolean;
	vpn?: boolean;
};

async function check(input: { identificationId: string | null; email: string; ip: string }) {
	try {
		const response = await fetch('https://api.openfingerprint.com/v1/check', {
			method: 'POST',
			headers: { 'content-type': 'application/json', 'x-api-key': OPENFINGERPRINT_KEY },
			body: JSON.stringify(input),
			signal: AbortSignal.timeout(5000)
		});
		return response.ok ? ((await response.json()) as Check) : {};
	} catch {
		return {};
	}
}

export const actions: Actions = {
	default: async ({ request, getClientAddress }) => {
		const data = await request.formData();
		const email = String(data.get('email') ?? '');
		const identificationId = data.get('of_identification');

		const result: Check = await check({
			identificationId:
				typeof identificationId === 'string' && identificationId ? identificationId : null,
			email,
			ip: getClientAddress()
		});

		if (result.disposable) return fail(400, { error: 'Please use a permanent email address.' });
		const verifyFirst = result.automation || result.spoofing || result.replayed || result.tor;
		// create the account, store result.visitorId on it only when present, and ask for
		// email verification before any free credits when verifyFirst is true
		return { success: true, verifyFirst };
	}
};

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

Behind a proxy, getClientAddress() returns the proxy's address unless the adapter is told where the real one is. Most hosting adapters (Vercel, Cloudflare, Netlify) handle this for you. With adapter-node, set ADDRESS_HEADER=X-Forwarded-For and XFF_DEPTH to the number of proxies you run in front of the app: it then takes the address that your own proxy appended, not a value the browser could have sent. Without these settings, every signup would reach the check from one address.

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.