<!-- Source: https://www.orderkraft.com/docs/developers/webhooks/ -->

# Webhooks

A webhook is Orderkraft calling you. Instead of asking the [API](/docs/developers/api/) every few minutes whether a manufacturing order changed, you give Orderkraft a URL and pick the changes you care about. When one happens, Orderkraft sends a `POST` request to that URL with the order as it is right then.

Your ERP can hear that an order was held for materials the moment it happens, or start invoicing as soon as production is complete.

## Subscribe to webhooks

Create a subscription with an API key that has the *Write* permission:

```sh
curl -X POST https://orderkraft.app/api/v1/webhook_subscriptions \
  -H "Authorization: Bearer ok_live_..." \
  -H "Content-Type: application/json" \
  -d '{ "url": "https://erp.example.com/orderkraft/webhooks", "topics": ["manufacturing_order.held", "manufacturing_order.completed"] }'
```

The answer includes a `secret`. That's the only time you see it, so store it straight away. You need it to [check that a webhook came from Orderkraft](#verify-a-webhook).

The URL has to use `https`. It can't point to Orderkraft itself, and it has to be reachable on the public internet: Orderkraft won't send webhooks to private or local network addresses.

To stop receiving webhooks, delete the subscription. See [webhook subscriptions](/docs/developers/api-reference/#webhook-subscriptions) for every endpoint. Merchants can also add a webhook in Orderkraft under **Settings → Webhooks**, where they see every subscription and the ones that stopped getting through, and can change one's URL or topics or stop it. Changing a subscription keeps its signing secret.

## Topics

A topic is the kind of change a webhook is about. Each change sends one webhook, under the topic that describes it best.

| Topic | Sent when |
| --- | --- |
| `manufacturing_order.created` | A manufacturing order is created |
| `manufacturing_order.started` | Work on the order starts |
| `manufacturing_order.held` | The order is put on hold |
| `manufacturing_order.released` | The order comes off hold, or leaves analysis for production |
| `manufacturing_order.completed` | The order is completed |
| `manufacturing_order.cancelled` | The order is cancelled |
| `manufacturing_order.updated` | Anything else about the order changes, like its materials, its work orders or its ship-by date |
| `work_order.created` | A work order is created |
| `work_order.started` | Work on it starts |
| `work_order.completed` | It's completed |
| `work_order.cancelled` | It's cancelled |
| `work_order.skipped` | It's skipped |
| `work_order.updated` | Anything else about it changes, like its work station |

A manufacturing order lists its work orders, so a change to a work order also sends `manufacturing_order.updated` for its order, when you subscribed to that topic.

## What a webhook looks like

Every webhook is a `POST` with a JSON body and these headers:

| Header | What it holds |
| --- | --- |
| `X-Orderkraft-Topic` | The topic, like `manufacturing_order.held` |
| `X-Orderkraft-Webhook-Id` | A unique id for this webhook. See [webhooks can arrive twice](#webhooks-can-arrive-twice) |
| `X-Orderkraft-Shop-Domain` | Your shop, like `your-shop.myshopify.com` |
| `X-Orderkraft-Triggered-At` | When the change happened |
| `X-Orderkraft-Hmac-Sha256` | The signature of the body. See [verify a webhook](#verify-a-webhook) |

The body is the manufacturing order or work order exactly as [Get a manufacturing order](/docs/developers/api-reference/#get-a-manufacturing-order) or [Get a work order](/docs/developers/api-reference/#get-a-work-order) returns it, at the moment of the change, so the code you wrote for the API can read webhooks as well.

```json
{
  "data": {
    "id": 5521,
    "number": "MO-4K2X9QZ1A",
    "status": "on_hold",
    "inputs": [],
    "workOrders": [],
    "holds": [
      { "id": 88, "note": "Waiting on oak planks", "createdAt": "2026-11-21T10:15:30.000+01:00", "releasedAt": null }
    ]
  }
}
```

Most fields are left out above. A real webhook has every field the API returns.

## Verify a webhook

Anyone can send a request to your URL, so check every webhook before you act on it. Orderkraft signs the body with your subscription's secret: `X-Orderkraft-Hmac-Sha256` is the HMAC-SHA256 of the raw body, Base64-encoded. Compute it yourself and compare.

Use the raw body exactly as it arrived. Parsing the JSON and writing it out again changes the bytes, and the signature won't match.

In Ruby:

```ruby
expected = Base64.strict_encode64(OpenSSL::HMAC.digest("SHA256", secret, request.raw_post))
valid = ActiveSupport::SecurityUtils.secure_compare(expected, request.headers["X-Orderkraft-Hmac-Sha256"])
```

In Node.js:

```js
import { createHmac, timingSafeEqual } from "node:crypto";

const expected = Buffer.from(createHmac("sha256", secret).update(rawBody).digest("base64"));
const received = Buffer.from(request.headers["x-orderkraft-hmac-sha256"] ?? "");
const valid = expected.length === received.length && timingSafeEqual(expected, received);
```

Compare in constant time, like above, rather than with `==`.

## Rotate the signing secret

When a secret may have leaked, or you lost it, give the subscription a new one:

```sh
curl -X POST https://orderkraft.app/api/v1/webhook_subscriptions/7/secret \
  -H "Authorization: Bearer ok_live_..."
```

The answer holds the new `secret`, and like on creation it's the only time you see it. Merchants can do the same with **Rotate secret** on the webhook's page under **Settings → Webhooks**.

The old secret stops working straight away, also for webhooks that are still waiting to be retried. Switch your receiver to the new secret right after rotating, or the webhooks in between fail verification.

## Answer quickly

Answer with any `2xx` status within 10 seconds, and do the actual work afterwards, in a background job. Orderkraft doesn't follow redirects, so a `3xx` counts as a failure.

## Failed webhooks are retried

When your URL doesn't answer with a `2xx` in time, Orderkraft tries again after 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours, 12 hours and 24 hours. If the last attempt fails too, about 45 hours after the first, Orderkraft stops trying that webhook.

Each webhook is retried on its own, so the webhooks after it don't wait.

## Webhooks can arrive twice

Now and then the same webhook reaches you more than once, for example when your answer got lost on the way back. `X-Orderkraft-Webhook-Id` stays the same on every attempt, so keep the ids you've handled and skip one you've seen before.

Webhooks can also arrive out of order, especially after a retry. Use `X-Orderkraft-Triggered-At`, or the `updatedAt` in the body, to tell which change came last.
