# The helper package

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

`@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.

:::note[Getting the package]
The package is not on the public npm registry yet. Until it is, call the API directly: every function below is a thin wrapper, and the pages on [the hand-off](/docs/developers/hand-off), [outcomes](/docs/developers/outcomes-api) and [webhooks](/docs/developers/webhooks) show the code without it.
:::

## Functions

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

## `verifyToken` options

| Option | Type | What it does |
| --- | --- | --- |
| `productId` | string | Return `null` for a token from another product. |
| `keys` | array | Verify offline with these public keys instead of fetching them. |
| `apiBase` | string | Handline's address. Defaults to `https://app.handline.io`. |
| `fetch` | function | Your own `fetch`. |
| `now` | Date | The 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

| Field | Type | Required | What it is |
| --- | --- | --- | --- |
| `productKey` | string | Yes | The product key from **Keys**. Keep it on your server. |
| `productId` | string | Yes | The product's id. |
| `stage` | string | Yes | One of the product's stages, such as `signed-up`. |
| `externalId` | string | Yes | Your own opaque id for who reached the stage. Never an email or a name. |
| `token` | string or null | No | The visit token kept at sign-up. Leave it out when there is none. |
| `occurredAt` | Date | No | When it happened. Defaults to now. |
| `value` | object | No | `{ amount, currency }`, for stages that carry a value. |
| `hashedEmail` | string | No | From `hashEmail`, only when the product sends hashed emails. Never stored. |
| `test` | boolean | No | A test outcome: shown on the set-up checklist, never counted or sent on. |
| `apiBase`, `fetch` | | No | As 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

```ts title="signup.ts (server)"
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}`,
});
```
