# Receive SmartFACT Data hooks

The contract in one page for your developer: signing, headers, the envelope, modes, answers, retries, results, the catch-up read, and where the OpenAPI document and examples are.

Source: https://help.smartfact.ai/manuals/configsheets/receive-data-hooks

Review edition: 1 December 2026

Roles: owner

SmartFACT Data sends your system the events you choose, signed, to an HTTPS address you give it. Your system answers at once, does its work, and tells Data what it did. This guide is the contract in one page, for the developer who builds the receiving side. The Owner who sets the connection up in Data gives you the two keys and the results address, as described in [Send events to your own system](/manuals/configsheets/send-events-to-your-system). The contract is public: it needs no secret to read, and it is the same for every connection.

In version 1 there is one event, `purchasing.order.send_requested`: a supervisor in SmartFACT Purchasing approved a supplier order, and your system should send it to the supplier. SmartFACT Data sends your system that order, and your system tells Data whether it placed it. Version 1 never sends a changed or cancelled order. Those stay with a person in Purchasing.

## Start from the examples

Do not write the receiver from nothing. Data generates a working receiver for each connection, with a marked place for your own code.

- **The connection's own OpenAPI document and code examples.** On the connection's **Code examples** tab in Data, press **Download this connection's OpenAPI document**, and **Download the PHP example** (or Python, Node.js or C#). Each example is one commented file with its package file. It lists only the events the connection has chosen, with the fields, types and descriptions of each.
- **The generic OpenAPI document.** It describes every event Data can send, with no connection's details. It is public, at `/api/v1/hooks/openapi.json` on the address of your SmartFACT Data (on hosted Data, `https://data.smartfact.ai/api/v1/hooks/openapi.json`). It is OpenAPI 3.1: its `webhooks` are what Data sends to you, and its `paths` are what you send to Data.
- **No secret is ever written into an example.** Each reads its settings from the environment: `SMARTFACT_HOOK_SECRET` (the key Data signs with), `SMARTFACT_RESULTS_URL` (the results address), `SMARTFACT_RESULTS_SECRET` (the key you sign results with) and `SMARTFACT_STORE` (a full path, in a folder no web server serves). Keep the values in your system's private settings.
- **The three jobs of an example.** `serve` receives, checks the signature, refuses what is wrong, stores each event once and answers. `work` processes what was stored, runs your code at the marked place, and sends results through a local outbox with the same backoff Data uses. `send-result` sends one outcome by hand. The example needs PHP 8.1 or later, Python 3.9 or later with Flask and requests, Node.js 22.13 or later with Express, or C# on .NET 10.
- **Signature vectors.** `vectors.json` holds signed requests with synthetic keys, each marked valid or not, including a request signed with two keys during a key change. Use them to test your own signature check. SmartFACT keeps them beside the contract and does not serve them from the website, so the Owner can ask SmartFACT support for a copy.
- **The receiver check.** `check_receiver.py` sends your receiver signed and badly signed deliveries and says whether each answer is what the contract requires. It is the same check Data's **Send test event** makes. It uses only Python's standard library. Ask SmartFACT support for it in the same way.

## Check each request's signature

Every request in both directions is signed with [Standard Webhooks](https://www.standardwebhooks.com/) 1.0.0, symmetric version `v1` (HMAC-SHA256). Each event is a `POST` with `content-type: application/json` and these headers:

| Header | Holds |
| --- | --- |
| `webhook-id` | The event's id, `evt_` and a ULID. The same on every attempt, and equal to the body's `id`. |
| `webhook-timestamp` | Unix seconds when this attempt was signed. |
| `webhook-signature` | `v1,` and a base64 signature. During a key change, two, separated by a space. |
| `user-agent` | `SmartFACT-Data-Hooks/1`. |
| `smartfact-hook-type`, `smartfact-hook-mode`, `smartfact-hook-attempt` | For routing and logs only. They are not signed, so never trust them. |

To check one:

1. Keep the raw request body exactly as received. Never verify a copy you parsed and wrote out again. In a framework, read the raw body, and leave the route out of CSRF protection.
2. Refuse a timestamp more than 300 seconds from your own clock.
3. Take the key from the secret: the bytes of the base64 text after `whsec_`. A secret is `whsec_` and the base64 of 32 random bytes.
4. Compute `HMAC-SHA256(key, webhook-id + "." + webhook-timestamp + "." + raw body)`, base64 it, and compare `v1,` and that value with each signature in the header, in constant time. One match is enough.
5. Only then read the JSON, and check that `contract` is `smartfact.hook/v1` and `id` equals `webhook-id`.

Answer 401 to a request that fails. Never answer 2xx to an event you could not verify. Your language's Standard Webhooks library does the same check, and the examples name it as an alternative.

Results go the other way, signed with the second key, the results secret, in the same format. Data checks them in the same way. There are two keys because they have two purposes, and your system holds one key of each kind at a time. When an Owner changes a key, Data signs with both the old and the new key for up to seven days, and accepts results signed with either. Swap yours at any point in that window.

## Read the envelope

The body is one JSON object, at most 64 KiB. Every event has the same envelope, and `data` depends on `type` and `type_version`.

| Field | Holds |
| --- | --- |
| `contract` | `smartfact.hook/v1`. |
| `id` | The event's id, equal to `webhook-id`. Your key for the delivery. |
| `type`, `type_version` | Such as `purchasing.order.send_requested` and 1. |
| `timestamp` | When it happened, in UTC. |
| `mode` | `live`, `shadow` or `test`. |
| `connection`, `site` | Each an `id` and a `name`. |
| `sequence` | The event's place among everything Data has recorded for this connection. The catch-up read's cursor. |
| `subject` | `kind` and `reference` of what it is about, and its own `sequence`. |
| `data` | The event's facts. |

For `purchasing.order.send_requested` version 1, `data` holds:

| Field | Holds |
| --- | --- |
| `handover_id` | Purchasing's id for this hand-over. |
| `kind`, `resend_reason` | `first`, or `resend` with a reason when a supervisor asked for the same revision again. |
| `order` | `reference`, `revision`, `approved_at`, `document_date`, `wanted_by` (set only when every line has the same date) and `notes`. |
| `supplier` | `code`, `name` and `account_number` (an empty string when there is none). |
| `deliver_to` | The site's `name`, and `lines`: the delivery address exactly as it was approved, top to bottom. Data does not label a town or a postcode, because a wrong guess would put a wrong address on an order. |
| `lines` | For each line: `number`, `supplier_code`, `description`, `quantity` (a decimal string in the supplier's unit), `unit`, `pack_size` (how many of your own units one supplier unit holds, or null), `our_item` and `expected_on`. |

The event carries no personal facts. Within a `type_version`, fields are only added, so ignore fields you do not know. Any other change is a new `type_version`, offered beside the old one for a notice period, and the connection chooses when to move.

## Treat each mode correctly

- **live:** a real order. Do the real-world action.
- **shadow:** a real order sent as a rehearsal while Purchasing sends it its usual way. Do everything except the real-world action, such as building the file but not sending it, and report results as usual. Data keeps shadow results and tells Purchasing nothing of them.
- **test:** from **Send test event** in Data. Answer 2xx and keep nothing.

## Never act on an event twice

Data sends each event at least once, with the same `webhook-id` and the same bytes on every attempt, and a fresh timestamp and signature. Store each `id` and answer a repeat with 2xx. For a supplier order the key to what you did is the pair `data.order.reference` and `data.order.revision`: never send the same pair to the supplier twice. When a supervisor asks for another send, the event has `kind` set to `resend`, a new `id` and the same reference and revision. Answer 2xx, do not place it again, and report `placed` for the new event with the reference of what you already did.

## Sequences and gaps

`sequence` runs upwards across everything for the connection, and `subject.sequence` across the events about one subject, such as one order. Events about one subject are sent in order: Data does not attempt a later one while an earlier one is waiting or stopped. Events about different subjects can arrive in any order.

Both numbers can have gaps, because test events and events a supervisor set aside take numbers too. Never wait for a missing number. Use `subject.sequence` only to ignore an event older than one you have already applied.

## Answer correctly

Data waits at most 15 seconds, and 5 seconds to connect. Store the event, answer, and do the slow work afterwards.

| Your answer | What Data does |
| --- | --- |
| Any 2xx | Delivered. Answer only after the event is safely stored. |
| 429, any 5xx, or no answer | Retries. It waits 30 seconds, then doubles to every 30 minutes, each wait 80 to 120 percent of its value, for 72 hours from the first attempt. It respects `Retry-After` up to 30 minutes. |
| 410 | Stops sending that event and pauses the connection. Never send to this address again. |
| Any other 4xx, including 401 and 422, or a redirect | Stops that event at once and alerts the connection's contact. Data never follows a redirect. |
| Still failing after 72 hours | Stops. |

A stopped event is sent again by a supervisor, or read through the catch-up read, only where it spends nothing. A live supplier order that stops is never sent again from Data. It is set aside, and Purchasing is told what your system may have.

What an answer proves matters for that. A 5xx, a 408, a redirect or no answer says "not now". It never says that your system has not stored the event, so Data treats a live order as possibly taken: if Purchasing then takes the order back, or Data stops sending it, Purchasing is told that your system may have it, and a person finds out what became of it. Answer 429 or another 4xx only when your system has kept nothing of the event. Data keeps the first 1 KiB of your reply, for 30 days, and reads at most 64 KiB.

## Tell Data what happened

Send each outcome as a signed `POST` to the results address on the connection's **Set-up** tab, `/api/v1/hooks/{endpoint}/results`, signed with the results secret. Choose a `webhook-id` for each result that is the same every time you send the same result, such as a hash of its content, so Data stores it once however often you send it.

The body has `contract` (`smartfact.hook-result/v1`), `event_id` (the event it answers), `type`, `outcome`, `timestamp` (when it happened, not when you sent it), `subject` (`kind`, `reference` and `revision`), `external_reference` (your own reference, such as a file name or the supplier's order number, up to 120 characters), `message` (up to 500 characters, for a buyer to read), `lines` and `refusal`.

| Outcome | When | What else |
| --- | --- | --- |
| `placed` | You sent the order. | An `external_reference`. |
| `refused` | You cannot send it. | `refusal` with a short `code` of your own, a `message` a buyer reads in Purchasing, and the line numbers it concerns, each once and at most 250, or none for the whole order. |
| `confirmed` | The supplier confirmed lines. | At least one entry in `lines`, each with `number`, `confirmed_quantity` and `confirmed_on`. A line may be confirmed in parts, in one result or across several. Purchasing treats a confirmed quantity of zero as a rejection. |
| `despatched` | The supplier despatched. | Nothing more is needed. |

Report `placed` or `refused` first, and only one of them for each event. `confirmed` and `despatched` follow `placed`, and either may come more than once. Data answers:

| Answer | Means |
| --- | --- |
| 202 | Stored. |
| 200 | Already stored under this `webhook-id` with the same body. |
| 401 | The signature is missing or wrong, or the timestamp is more than 300 seconds out. |
| 404 | Data sent no event with this `event_id` on this connection. |
| 409 | `result_id_reused`: this `webhook-id` was used for a different body. `outcome_conflict`: the outcome contradicts one already stored. `outcome_out_of_order`: it came before `placed`. |
| 422 | The body is outside the contract, or the outcome is not one this event accepts. |
| 429 | More than 600 results a minute for the connection. Send again after `Retry-After`. |
| 503 | Not now. Send the same result again later, under the same `webhook-id`. |

Retry 429 and 503 with the same `webhook-id`. Do not retry a 401, 404, 409 or 422 until you have put right what it says. Results for a shadow event are kept and passed to nobody. For a live event Data passes them to Purchasing, where `placed` marks the order as ordered with your reference.

## Catch up after downtime

If your system was down for longer than Data retries, or as a safety net on a schedule, send a signed `POST` to `/api/v1/hooks/{endpoint}/events/read` with `after` (the last `sequence` you have, or 0) and `limit` (1 to 100). Sign it as a result, with a fresh `webhook-id` for every read: a repeated one is refused with 409 `read_repeated`. It answers `events`, the bodies Data sent, in `sequence` order, and `more`.

The read returns events recorded in the last 30 days. It includes events Data has not sent yet: ones waiting for their first attempt or behind another, and, for shadow events, ones held for an earlier address. Keep one as you would keep the hook, under its `id`. Once you have read a live order, Data counts it as one your system has, and may still send the hook, so store by `id` and do the work once.

Left out are test events, and live supplier orders Data would not send now: ones Purchasing took back, ones Data could not hand over, ones held for an earlier address, ones that have waited four days, and, while the connection is not live, any that your system has not accepted. A 422 means the request is not a read request, and 429 means more than 600 reads a minute.

## Test your receiver in Data

With the connection's address saved, press **Send test event** in Data. It sends a signed `test` event, the same event signed with another key, and the same event again, and shows each answer in the checklist: **Test event accepted**, **Bad signature refused**, **Repeat answered**. Then run `send-result` from your example for the test event's id, so that **Test result received and verified** passes. A shadow period and a reviewer's approval follow before real orders are sent live.
