Skip to content
Beta
All guides

The API and webhooks

Connect your other systems to Magpie with API tokens, and hear about changes the moment they happen with signed webhooks.

After reading this guide, you'll know:

  • What the API can do, and where to find the full reference.
  • How to create, scope and revoke API tokens.
  • How to add a webhook and choose its events.
  • How to check that a webhook really came from Magpie, and what happens when deliveries fail.
On this page

The API lets your own systems read and change your inventory, and webhooks tell them when something happens. Both are set up under Settings › API & webhooks, by people whose role lets them manage API tokens and webhooks (owners and admins).

1 What the API does

The API is a small JSON API at /api/v1. It follows the same rules as the app: permissions, location limits, approvals and stock checks all apply. With it you can:

  • read items, locations and stock levels,
  • create items and update their details (quantities only change through stock changes),
  • set or remove a location's minimum for an item,
  • add, remove, correct and move stock (a change that needs approval waits for it, like any other),
  • look up a scanned code,
  • read active counts and record count entries.

The Magpie phone apps use the same API to count offline.

The API reference covers authentication, conventions, every endpoint and the error codes. There's also a link to it, API documentation, at the top of Settings › API & webhooks.

Enlarge screenshot: Settings › API & webhooks, with the API tokens section and its form, the Webhooks section with its events, and Mobile devices
Settings › API & webhooks

2 Create an API token

Every request needs a token. Give each integration its own, with only the scopes it needs, so you can revoke one without breaking the others.

  1. Under API tokens, type a Name that says what it's for.
  2. Choose when it Expires: In 90 days, In 30 days, In a year or Never.
  3. Tick its Scopes (see below).
  4. Choose Create token.
  5. Copy the token straight away and keep it somewhere safe. Magpie shows it only once.
ScopeWhat it allows
readRead items, locations, stock and reports
writeCreate and update items and locations
stockPost stock adjustments and moves
countsRecord stock count entries
syncOffline synchronization

A token acts as the person who created it. Their role's permissions and location limit apply as well as the token's scopes, and their name is on every change it makes. If they're removed from the organization or suspended, their tokens stop working.

3 Revoke a token

The list under API tokens shows each token's name and first few characters, its scopes, who created it, when it was last used and when it expires. Choose Revoke and confirm, and it stops working straight away; it stays in the list marked Revoked. A token that expires stops working on its own.

4 Add a webhook

A webhook sends a message to your system each time something happens, so it doesn't have to keep asking.

  1. Under Webhooks, type the Endpoint URL. It must start with https:// and be reachable from the internet.
  2. Tick the Events to send, at least one.
  3. Choose Add webhook.

The confirmation shows the webhook's signing secret, which starts with whsec_. Copy it into your receiving system now: you need it to check signatures, and it isn't shown again.

EventWhen it's sent
stock.changedStock changed (any adjustment, move, receipt, count, pick…)
item.low_stockItem fell to or below a minimum (for all locations or one)
stock_count.completedA count was completed
purchase_order.status_changedA purchase order changed status
adjustment_request.status_changedA stock change was requested, approved, rejected or withdrawn
checkout.overdueA check-out became overdue

Each message is a JSON POST with the event's id, type, created_at, your organization and the event's data. The API reference has the details.

5 Check the signature

Anyone can send a request to your URL, so check that each one came from Magpie before you act on it. Every message has an X-Webhook-Signature header like t=1700000000,v1=…:

  1. Work out the HMAC-SHA256 of the timestamp, a dot and the raw body (t.body), using your signing secret.
  2. Compare it with v1, using a constant-time comparison.
  3. Reject the message if they differ, or if the timestamp is more than 5 minutes old.

The X-Webhook-Id header is the same each time a message is retried, so you can ignore one you've already handled.

6 When deliveries fail

Reply quickly with a 2xx status: Magpie waits up to 10 seconds. If your system doesn't answer or replies with an error, Magpie tries again later, waiting longer each time, up to 8 attempts.

Recent deliveries lists the latest 20 messages, with whether each was delivered or failed, the event, how many attempts it took and the HTTP status your system replied with.

If 20 messages in a row fail, Magpie stops sending to that webhook and it's marked disabled after repeated failures. Fix the problem, then Remove the webhook and add it again. The new one has a new signing secret.

7 Sign out mobile devices

When people sign in to the Magpie phone apps for offline counting, each phone gets its own sign-in for syncing. They're listed under Mobile devices, with the device, the person and when it was last active. Choose Sign out to end one, for example when a phone is lost. Removing or suspending a person signs out their devices too.

Still stuck?

Email support with what you were trying to do and any message you saw.

Contact support