Skip to content

The helper package

Look up each function of `@handline/handoff`, the helper that reads and verifies the visit token, reports outcomes and verifies webhooks.

Updated

@handline/handoff carries the visit token from a Handline page into your product, verifies it, reports outcomes against it, and verifies webhook deliveries. It has no dependencies and runs in a browser and on a server: Node 20 or later, Deno, Bun and edge runtimes. It is MIT licensed.

Functions#

FunctionWhereWhat it does
readToken(url)Browser or serverReturns the hl_v token from a URL, or null when there is none or it is malformed.
keepToken(token, store, key?)BrowserSaves the token in storage you choose, such as sessionStorage. The key defaults to hl_v.
carryToken(href, token)BrowserAdds the token to a link inside your product, so it survives browsing before sign-up.
verifyToken(token, opts?)ServerChecks the signature and expiry with Handline's public keys. Returns { productId, visitId, issuedAt, expiresAt } or null.
reportOutcome(report)ServerReports an outcome with your product key. Returns { id, traced }; throws with Handline's message and next step on an error.
hashEmail(email)ServerTrims, lower-cases and hashes an email with SHA-256, as ad platforms expect.
verifyWebhook(body, header, secret, opts?)ServerChecks a webhook or lead form delivery's Handline-Signature. Returns true or false.

verifyToken options#

OptionTypeWhat it does
productIdstringReturn null for a token from another product.
keysarrayVerify offline with these public keys instead of fetching them.
apiBasestringHandline's address. Defaults to https://app.handline.io.
fetchfunctionYour own fetch.
nowDateThe time to check expiry against.

The keys are fetched from https://app.handline.io/.well-known/handline-visit-keys and kept for an hour.

reportOutcome fields#

FieldTypeRequiredWhat it is
productKeystringYesThe product key from Keys. Keep it on your server.
productIdstringYesThe product's id.
stagestringYesOne of the product's stages, such as signed-up.
externalIdstringYesYour own opaque id for who reached the stage. Never an email or a name.
tokenstring or nullNoThe visit token kept at sign-up. Leave it out when there is none.
occurredAtDateNoWhen it happened. Defaults to now.
valueobjectNo{ amount, currency }, for stages that carry a value.
hashedEmailstringNoFrom hashEmail, only when the product sends hashed emails. Never stored.
testbooleanNoA test outcome: shown on the set-up checklist, never counted or sent on.
apiBase, fetchNoAs for verifyToken.

verifyWebhook options#

toleranceSeconds (default 300) is how far the signature's time may be from your clock; now sets the clock. Pass the raw body exactly as received.

Example#

signup.ts (server)TypeScript
import { verifyToken, reportOutcome } from '@handline/handoff'; const visit = await verifyToken(req.body.hl_v, { productId: process.env.HANDLINE_PRODUCT_ID });const token = visit ? req.body.hl_v : null;await db.accounts.update(user.id, { handlineToken: token });await reportOutcome({  productKey: process.env.HANDLINE_PRODUCT_KEY!,  productId: process.env.HANDLINE_PRODUCT_ID!,  token,  stage: 'signed-up',  externalId: `signup:${user.id}`,});

Was this page helpful?