NFC Agent

Overview

What the NFC agent is, when you need one, and how a tap reaches the Davi API.

The Davi NFC Agent is a small program that runs on the machine with the card reader. It talks to USB readers over PC/SC, accepts scans from paired phones and WebNFC browsers, and serves every scan to your application on a local WebSocket, so your application reads one stream whatever produced the tap.

It is a separate program with its own releases, not part of the Davi API. It runs on your hardware and works on its own.

When you need one

A browser cannot talk to a USB card reader. Anything that reads or writes a physical card from a desktop needs something native in between, and that is what the agent is:

  • A door or a kiosk reading cards to admit people to a session
  • A counter looking somebody up from the card they tap
  • Encoding: writing a Davi URL onto a new card before handing it over

You do not need it to use Davi. Every API in these docs works from a phone, a server or a browser with no reader anywhere. You need it when a physical card is part of the flow.

From a tap to Davi

A Davi card carries its URL, https://davi.social/c/{identifier}, in a URI record. The {identifier} segment is what Davi's identifier tagged union takes as { "type": "card", "value": identifier }. A card with no URL is identified by its chip UID only if it was registered by that UID.

So a door is one WebSocket and one HTTP call. This Node.js script admits whoever taps, using an org-scoped token that can admit (Org Activities lists the scopes):

const AGENT = `wss://localhost:9470/ws?secret=${encodeURIComponent(process.env.DAVI_NFC_SECRET)}`;
const DAVI = "https://api.davi.social/api/v1";
const SESSION_SLUG = "week-1";
const TOKEN = process.env.DAVI_ORG_TOKEN;

function cardIdentifier(tag) {
  const url = tag.message?.records.find((r) => r.type === "uri" && r.content)?.content;
  if (url) {
    const segments = new URL(url).pathname.split("/").filter(Boolean);
    const i = segments.indexOf("c");
    if (i !== -1 && segments[i + 1]) return decodeURIComponent(segments[i + 1]);
  }
  return tag.uid;
}

const ws = new WebSocket(AGENT);

ws.addEventListener("message", async (event) => {
  const msg = JSON.parse(event.data);
  if (msg.type !== "tagData" || !msg.payload.uid || msg.payload.err) return;

  const identifier = cardIdentifier(msg.payload);
  const res = await fetch(`${DAVI}/sessions/${SESSION_SLUG}/admit`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${TOKEN}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      identifier: { type: "card", value: identifier },
      idempotency_key: `${SESSION_SLUG}:${identifier}:${msg.payload.scannedAt}`,
      proof_metadata: { method: "nfc_tap" },
    }),
  });

  const admit = await res.json();
  console.log(admit.decision, admit.blocked_reason ?? "");
});

Run it with the agent's API secret and certificate, as in Getting started:

DAVI_NFC_SECRET="$(cat ~/.config/davi-nfc-agent/api-secret.txt)" \
NODE_EXTRA_CA_CERTS=~/.config/davi-nfc-agent/tls/server.crt \
DAVI_ORG_TOKEN=ACCESS_TOKEN \
node door.mjs

The agent needs no Davi credentials and never sees your token: it hands you the tag, and the call to Davi is yours. What each decision means is in Org Activities, and Membership Lifecycle covers a card that carries a tier.

Any number of applications can connect to the agent at once, and each receives every scan.

Where to go next

Getting startedRun the agent, read a tag and write to it
Connect from a browserLet a web page reach the agent
Client APIThe WebSocket your application speaks
Device APIFeeding the agent from a phone, a browser or your own hardware
Custom buildsEmbedding the agent as a Go library

The agent is open source under the MIT licence: dotside-studios/davi-nfc-agent.