framework

Browser fingerprinting in Rails

Add browser fingerprinting and bot, disposable email and IP checks to a Rails signup form: the SDK through importmap, one check call with Net::HTTP, and request.remote_ip.

This guide adds OpenFingerprint to a Rails signup form. 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 controller 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. Pin the SDK and add your key

With importmap-rails, pin the package. It has no dependencies, so it pins as a single file in vendor/javascript:

sh
bin/importmap pin @openfingerprint/sdk

With jsbundling-rails, yarn add @openfingerprint/sdk instead. Create a secret key under API keys in the dashboard and set it in the server's environment as OPENFINGERPRINT_KEY, or store it in your encrypted credentials.

2. Identify in the browser

The form has a hidden field for the identification ID:

erb
<%= form_with url: signup_path, id: "signup" do |f| %>
  <%= f.email_field :email, required: true %>
  <%= hidden_field_tag :of_identification %>
  <%= f.submit "Sign up" %>
<% end %>

In app/javascript/application.js, start watching and collecting, and fill the fields before the form is sent. Collect only once your disclosure or consent requirements are met. requestSubmit() keeps the submission inside Turbo.

js
import { identify, watch } from '@openfingerprint/sdk';

document.addEventListener('turbo:load', () => {
	const form = document.getElementById('signup');
	if (!form) return;
	watch();
	const collecting = 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();
	});
});

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 controller calls POST /v1/check with Ruby's Net::HTTP. The API never reads an IP from the connection: your server passes the address it saw. request.remote_ip skips the proxies Rails trusts (private addresses by default); if your load balancer has a public address, add it to config.action_dispatch.trusted_proxies, so a forwarded address the browser sent isn't taken at face value.

ruby
# app/controllers/signups_controller.rb
require "net/http"

class SignupsController < ApplicationController
  CHECK_URI = URI("https://api.openfingerprint.com/v1/check")

  def create
    result = openfingerprint_check(
      identificationId: params[:of_identification].presence,
      email: params[:email],
      ip: request.remote_ip
    )

    if result["disposable"]
      return redirect_to new_signup_path, alert: "Please use a permanent email address."
    end
    verify_first = 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 verify_first is true
    redirect_to welcome_path
  end

  private

  def openfingerprint_check(**input)
    response = Net::HTTP.start(CHECK_URI.host, CHECK_URI.port, use_ssl: true,
                               open_timeout: 3, read_timeout: 5) do |http|
      http.post(CHECK_URI.path, input.to_json,
                "content-type" => "application/json",
                "x-api-key" => ENV.fetch("OPENFINGERPRINT_KEY"))
    end
    response.is_a?(Net::HTTPSuccess) ? JSON.parse(response.body) : {}
  rescue StandardError
    {}
  end
end

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.