> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thrad.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# SKILL

> Send an advertiser's conversions to Thrad from their backend with the Server-to-server Conversions API — purchases, leads, signups and subscriptions, deduplicated with the browser tag. Triggers on "Thrad Conversions API", "Thrad CAPI", "server-side conversions", "events.thrad.ai", "THRAD_SERVER_TOKEN", "THRAD_EVENTS_API_KEY", "server token", "events:write", "getServerContext".

# Thrad Conversions API

This skill sends conversions from the **advertiser's backend** to Thrad:

```http theme={null}
POST https://events.thrad.ai/v1/api/events
```

Server events survive ad blockers and browser shutdowns, can carry hashed customer identity, and cover offline and delayed events. One request format serves Thrad attribution and, where Thrad has enabled it for the tag, OpenAI measurement.

The browser tag comes first. If the site doesn't have it yet, follow the `thrad-tag` skill ([https://docs.thrads.ai/skills/thrad-tag/SKILL.md](https://docs.thrads.ai/skills/thrad-tag/SKILL.md)) before this one. That skill ends by recommending server events and, if the user agrees, sends you here in the same session.

Before using any documentation URL, fetch and read the LLM-friendly docs sitemap:

[https://docs.thrads.ai/llms.txt](https://docs.thrads.ai/llms.txt)

Use the sitemap to locate the latest canonical page for each topic. Prefer sitemap-discovered URLs over the hardcoded ones below if they differ.

## When to use this skill

* "Send Thrad conversions from my server"
* "Set up the Thrad Conversions API"
* "Our purchases are missing because of ad blockers"
* "Send signups or CRM events to Thrad"

## Procedure

### Step 1 — Get the tag ID, and plan for the server token

* The **tag ID** (`adv_<name>_<id>`). If you came here from the `thrad-tag` skill, you already have it. Otherwise ask the user, or read it from the site's existing Thrad loader. It's on the ad account's **Attribution** page in the Thrad platform: [https://platform.thrad.ai](https://platform.thrad.ai)
* A **server token** for that tag (it carries the `events:write` permission). You never see it. Your code reads it from the environment variable `THRAD_SERVER_TOKEN`. If the backend already reads a Thrad token under another name, such as `THRAD_EVENTS_API_KEY`, keep that name.

Write the code first; the token doesn't need to exist yet. When the change is in, tell the user: open the ad account's **Attribution** page in Thrad, choose **Create server token** (on some accounts the page still has tabs: there it is **Create token** under **Setup → Send conversions from your server**), and save it as `THRAD_SERVER_TOKEN` wherever the server keeps secrets. Only organization owners and admins can create one, and it's shown once.

The token is a secret. **Never ask the user to paste it into the chat**, never write it into code, a committed `.env` file or a test fixture, and never send it to a browser or mobile app. If `THRAD_SERVER_TOKEN` isn't set at runtime, the code must skip sending and log a warning, never fail the checkout.

### Step 2 — Read the reference before writing the request

Fetch these, in this order:

* Overview: [https://docs.thrads.ai/advertisers/attribution/server-events/overview.md](https://docs.thrads.ai/advertisers/attribution/server-events/overview.md)
* Event reference: [https://docs.thrads.ai/advertisers/attribution/server-events/event-reference.md](https://docs.thrads.ai/advertisers/attribution/server-events/event-reference.md)
* Identity and consent: [https://docs.thrads.ai/advertisers/attribution/server-events/identity-and-consent.md](https://docs.thrads.ai/advertisers/attribution/server-events/identity-and-consent.md)
* Testing: [https://docs.thrads.ai/advertisers/attribution/server-events/testing.md](https://docs.thrads.ai/advertisers/attribution/server-events/testing.md)

### Step 3 — Carry the browser context to the server

At conversion time, read the tag's context in the browser and send it to your own backend **unchanged**, with the order or lead:

```js theme={null}
const context = window.thradTag.getServerContext();
await fetch("/api/complete-order", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ orderId, context }),
});
```

Extend an existing checkout or form request rather than adding a new one where you can. Store the context with the order if the conversion is sent later (a webhook, a CRM status change). Never edit, re-sign or rebuild it.

### Step 4 — Send the event from the backend

Send it where the conversion is **finalized**: the payment-succeeded webhook, the order-created handler, the CRM stage change. Not where the user clicks.

```json theme={null}
{
  "version": 1,
  "tag_id": "adv_example_123",
  "events": [{
    "event_id": "purchase:v1:<see Step 5>",
    "event_name": "order_created",
    "timestamp_ms": 1773892800000,
    "action_source": "web",
    "source_url": "https://shop.example.com/checkout/confirmation",
    "context": { "...": "the object from getServerContext(), unchanged" },
    "user": {
      "emails_sha256": ["<lowercase hex SHA-256 of the normalized email>"],
      "countries": ["US"]
    },
    "data": {
      "type": "contents",
      "amount": 2599,
      "currency": "USD",
      "contents": [{ "id": "sku_123", "quantity": 1 }]
    }
  }]
}
```

with `Authorization: Bearer $THRAD_SERVER_TOKEN` and `Content-Type: application/json`.

Rules that are easy to get wrong:

* `data.amount` is an **integer in minor units** (`2599` for \$25.99). The browser tag takes major units; the server doesn't.
* `timestamp_ms` is Unix **milliseconds**, at most seven days old and ten minutes ahead. Use when the conversion happened, not when you send it.
* `action_source: "web"` requires `source_url`.
* `data.type` must match the event: `contents` for page, product, cart, checkout and order events; `customer_action` for leads, registrations and appointments; `plan_enrollment` for trials and subscriptions; `custom` for `custom` (which also needs `custom_event_name`).
* Use the canonical event names from the reference. Unknown fields are rejected.
* **Identity:** send `user` fields only when the user has measurement consent. Prefer SHA-256 hashes, normalized exactly as the identity reference says, and check your code against its test vectors. Never put identity in `data.custom_fields`.
* **Consent:** when the user declined measurement, send `consent: { "measurement": false }` or don't send the event.

### Step 5 — Use one identity per conversion

Every event needs a stable `event_id`. Reuse it on every retry. Never generate a new one per attempt.

* **Purchases** reported by both the browser tag and the server must use the browser's canonical purchase identity, or they count twice:

  ```js theme={null}
  import { createHash } from "node:crypto";

  function purchaseEventId(tagId, orderId) {
    const tag = tagId.trim();
    const order = orderId.trim().replace(/^gid:\/\/shopify\/Order\/([1-9][0-9]*)$/, "$1");
    if (!tag || !order) throw new Error("Tag ID and final order ID are required");
    return "purchase:v1:" + createHash("sha256")
      .update(JSON.stringify([tag, order]), "utf8").digest("hex");
  }
  ```

  Port it exactly to the backend's language: compact JSON of `[tag, order]`, UTF-8, lowercase hex.
* **Leads and signups:** the same opaque `event_id` the browser sent for that submission, such as `lead:<submission id>`.
* If the backend **already** sends purchases to Thrad with other IDs, don't change the IDs on your own. Stop and tell the user: Thrad has to reconcile existing identities before a cutover.

### Step 6 — Handle the response

* `200`: inspect **every** entry in `results`. Statuses are `accepted`, `duplicate`, `suppressed`, `invalid` and, in test mode, `validated`. Log `invalid` with its `errors`, and don't retry it unchanged.
* `401` / `403`: the token is wrong, revoked, or not issued for this tag. Don't retry; tell the user.
* `422`: the envelope is malformed. Fix the request.
* `503`: the outcome is unknown. Retry the whole request with the **same** event IDs, with backoff.

Send from a background job or queue where the backend has one, so a slow request can't fail the checkout. Batch up to 1,000 events per envelope.

### Step 7 — Verify before declaring done

Do not tell the user the integration is complete until:

1. A request with `"test_mode": true` returns `200` with `validated` for each event type you wired. Test mode records nothing and forwards nothing. You can only run this if the user has already set `THRAD_SERVER_TOKEN` in the environment you run in. If it isn't set, don't ask for the token: test the handler with the HTTP call mocked, and tell the user the live test is theirs to run after deploy.
2. The purchase `event_id` your server computes matches the browser's for the same tag and order.
3. Amounts are integer minor units and timestamps are milliseconds.
4. The token is read from `THRAD_SERVER_TOKEN`, nothing in the diff contains it, and the handler skips sending without crashing when it's unset.
5. A deliberately bad event (say, `timestamp_ms` in seconds) comes back `invalid` and is logged without crashing the handler.

Then tell the user what's left for them: create the server token on the ad account's **Attribution** page, as in Step 1, and save it as `THRAD_SERVER_TOKEN`, deploy with `test_mode` removed, and place a test order. The Attribution page shows when the first server event arrives (on the tabbed page, under **Status**). An `accepted` result is a receipt, not proof of conversion credit.

## Common gotchas

* **Token in the browser** — the server token is server-only. The browser uses only the tag ID.
* **A new `event_id` per retry** — every retry becomes a new conversion.
* **Cart or checkout token as the order** — breaks deduplication with the browser purchase.
* **Altered context** — changing `oppref`, `oppcref`, click signatures or `obref` returns `invalid_context`.
* **Seconds instead of milliseconds** — returns `invalid_timestamp`.
* **Plaintext identity in the wrong field** — only the documented `user` fields are hashed at ingress.
* **Inventing fields** — the envelope rejects unknown fields. If it isn't in the reference, it doesn't exist.

## References

* Docs sitemap: [https://docs.thrads.ai/llms.txt](https://docs.thrads.ai/llms.txt)
* Overview: [https://docs.thrads.ai/advertisers/attribution/server-events/overview.md](https://docs.thrads.ai/advertisers/attribution/server-events/overview.md)
* Authentication: [https://docs.thrads.ai/advertisers/attribution/server-events/authentication.md](https://docs.thrads.ai/advertisers/attribution/server-events/authentication.md)
* Identity and consent: [https://docs.thrads.ai/advertisers/attribution/server-events/identity-and-consent.md](https://docs.thrads.ai/advertisers/attribution/server-events/identity-and-consent.md)
* Testing: [https://docs.thrads.ai/advertisers/attribution/server-events/testing.md](https://docs.thrads.ai/advertisers/attribution/server-events/testing.md)
* Event reference: [https://docs.thrads.ai/advertisers/attribution/server-events/event-reference.md](https://docs.thrads.ai/advertisers/attribution/server-events/event-reference.md)
* Browser tag skill: [https://docs.thrads.ai/skills/thrad-tag/SKILL.md](https://docs.thrads.ai/skills/thrad-tag/SKILL.md)
* Thrad platform: [https://platform.thrad.ai](https://platform.thrad.ai)
* Support: [contact@thrad.ai](mailto:contact@thrad.ai)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.