# Reference links: know which order or campaign a WhatsApp chat came from

> Make a short WhatsApp link (and a QR code) with your own reference. When the customer sends the message, every event of that conversation carries your reference and data.

A reference link is a short link to your WhatsApp number with a message already filled in. It carries a short code, so you know which order, lead or campaign the chat is about without asking.

```bash
curl -X POST "https://api.app2email.com/v1/YOUR_NUMBER/ref-links" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reference": "order-4711", "text": "Hi, about my order", "data": {"campaign": "spring"}}'
```

The answer has `url` (`https://www.app2email.com/r/K7Q3MX9P`) and `qr_svg_url` (a QR code of the same link). Put either on an invoice, an e-mail, a website or a flyer. The key needs the `messages:send` scope.

<a id="what-happens"></a>

## What happens

1. The customer opens the link. It goes to `https://wa.me/<your number>` with the text `Hi, about my order (Ref K7Q3MX9P)` filled in, and the click is counted (no IP address is kept).
2. They send the message. Your `message.received` event carries `"reference": {"code": "K7Q3MX9P", "reference": "order-4711", "data": {"campaign": "spring"}}`.
3. Every later `message.status`, `message.failed` and `conversation.window_closing` event of that conversation carries it too, until another code replaces it or `sticky_days` pass (30 unless you set it). `GET /v1/{number}/conversations/{contact}` shows it as well.

The code stays visible in the message on purpose: there are no hidden characters.

<a id="claim"></a>

## A reference is a claim, not proof

Anyone can type a code, or forward the link. So:

- `reference_reused: true` means another conversation used the same code too.
- `single_use: true` gives the reference to the first contact who sends it, and no one else.
- To prove who someone is, use a verification, not a reference.

<a id="options"></a>

## Options

| Field | Default | What it does |
| --- | --- | --- |
| `reference` | (needed) | Your own id, at most 200 characters. |
| `text` | none | The prefilled message. " (Ref <code>)" is added. |
| `data` | none | Any JSON up to 1 KB, given back in the events. |
| `expires_in_seconds` | never | After this, `/r/<code>` answers "Not found." and new messages no longer pick up the code. |
| `single_use` | `false` | Only the first contact gets the reference. |
| `sticky_days` | `30` | How long a conversation keeps the reference. |

`GET /v1/{number}/ref-links` lists your links with `clicks` and `uses`. `GET /v1/{number}/ref-links/{code}` shows one, with how many conversations came through it. `DELETE /v1/{number}/ref-links/{code}` removes one. AI agents use the MCP tools `create_ref_link`, `list_ref_links` and `delete_ref_link`.

<a id="errors"></a>

## Errors

| Code | Status | Meaning |
| --- | --- | --- |
| `invalid_ref_link` | 400 | A field is missing or not allowed (the message names it). |
| `ref_link_not_found` | 404 | No reference link with that code on the number. |
| `sandbox_key` | 403 | A sandbox key cannot make or delete reference links. |

The complete list is on [the errors page](/api/errors).
