Authentication
Every key you make in Settings › Developer is a pair: a secret key for your server and a publishable key for a browser or an app.
| Secret key | Publishable key | |
|---|---|---|
| Looks like | sk_live_… or sk_test_…, 64 hex digits after the prefix |
pk_live_… or pk_test_…, 32 hex digits after the prefix |
| Send it as | Authorization: Bearer sk_… |
X-Sabiwave-Public-Key: pk_… |
| Keep it | On your server only | Anywhere: it’s meant to be seen |
| It may | Everything the key’s scopes allow | Read the catalogue and check promo codes, nothing else |
| You see it | Once, when the key is made or rotated | Whenever you open the key |
| Calls a minute | 600 | 100 from each visitor’s address |
test or live in the prefix is the key’s mode: see Test and live keys.
What a key may do
Section titled “What a key may do”When you make a key you tick what it may do. Each tick is a scope, and every endpoint names the one
it needs. GET /ping works with any key.
| Scope | In the dashboard | Lets the key call | Publishable key |
|---|---|---|---|
catalog:read |
Read the catalogue | GET /catalog/products, GET /catalog/products/{idOrSlug}, GET /catalog/categories |
Yes |
promotions:read |
Check promo codes | POST /promotions/validate |
Yes |
orders:write |
Place and cancel orders | POST /orders, POST /orders/{id}/cancel, GET /orders/{id}/status |
No |
orders:read |
Read orders | GET /orders, GET /orders/{id}, GET /orders/{id}/status |
No |
customers:write |
Add customers | POST /customers |
No |
customers:read |
Read customers | GET /customers/{id}, GET /customers/{id}/orders |
No |
inventory:read |
Read stock levels | GET /inventory |
No |
A publishable key holds only the first two, whatever else its pair was given. A call the key doesn’t have the scope for is refused:
curl "https://api.sabiwave.com/api/v1/public/inventory?productIds=01a2160c-6b00-7627-af42-d534badeaea4" \ -H "X-Sabiwave-Public-Key: $SABIWAVE_PUBLISHABLE_KEY"const res = await fetch('https://api.sabiwave.com/api/v1/public/inventory?productIds=01a2160c-6b00-7627-af42-d534badeaea4', { headers: { 'X-Sabiwave-Public-Key': SABIWAVE_PUBLISHABLE_KEY, },});const result = await res.json();{ "success": false, "message": "A publishable key only reads the catalogue and promotions: use your secret key, server-side.", "error": { "name": "ForbiddenError", "code": "SCOPE_MISSING", "statusCode": 403, "details": [ { "scope": "inventory:read" } ] }, "requestId": "53779cab-4bdb-4e70-afb3-8dbbdf327665"}The secret key
Section titled “The secret key”Send it as a Bearer token from your server:
curl "https://api.sabiwave.com/api/v1/public/ping" \ -H "Authorization: Bearer $SABIWAVE_SECRET_KEY"const res = await fetch('https://api.sabiwave.com/api/v1/public/ping', { headers: { Authorization: `Bearer ${process.env.SABIWAVE_SECRET_KEY}`, },});const result = await res.json();Without a key, or with one that isn’t ours, the answer is 401:
{ "success": false, "message": "Send your secret key as a Bearer token, or your publishable key in X-Sabiwave-Public-Key.", "error": { "name": "UnauthorizedError", "code": "MISSING_API_KEY", "statusCode": 401 }, "requestId": "e5b919c7-6391-43ef-bd2b-57e3be346ba5"}| Code | Status | What happened |
|---|---|---|
MISSING_API_KEY |
401 | Neither header was sent. |
INVALID_API_KEY |
401 | It isn’t shaped like a Sabiwave key, or no business holds it. |
API_KEY_REVOKED |
401 | Someone revoked the key. |
API_KEY_EXPIRED |
401 | The key was rotated and its grace period is over: use the new one. |
IP_NOT_ALLOWED |
403 | The key only accepts calls from certain addresses, and this isn’t one. |
BUSINESS_UNAVAILABLE |
403 | The business can’t take requests just now. |
Lock it to your servers. Under Only from these addresses, list your servers’ IP addresses
(up to 20, single addresses rather than ranges). A secret key then refuses calls from anywhere else
with 403 IP_NOT_ALLOWED. Leave it empty to accept calls from anywhere.
The publishable key, in a browser
Section titled “The publishable key, in a browser”A publishable key reads your catalogue and checks promo codes, so your website’s pages can call
Sabiwave directly. List your website under Websites that may use the publishable key: up to
20 origins, each a scheme and host such as https://shop.example.com (add https://www.… too if
your site answers on both). A local address such as http://localhost:3000 works while you build.
curl "https://api.sabiwave.com/api/v1/public/catalog/products/weekend-gift-hamper" \ -H "X-Sabiwave-Public-Key: $SABIWAVE_PUBLISHABLE_KEY" \ -H "Origin: https://shop.example.com"// In the browser. The publishable key is safe in your page.const SABIWAVE_PUBLISHABLE_KEY = 'pk_live_…';
const res = await fetch('https://api.sabiwave.com/api/v1/public/catalog/products/weekend-gift-hamper', { headers: { 'X-Sabiwave-Public-Key': SABIWAVE_PUBLISHABLE_KEY, },});const result = await res.json();HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://shop.example.comVary: Origin{ "success": true, "data": { "product": { "id": "01a2160c-6b00-7627-af42-d534badeaea4", "name": "Weekend Gift Hamper", "slug": "weekend-gift-hamper", "description": "A weekend of treats in a woven basket: biscuits, chocolate, juice and a card.", "price": 20000, "category": { "id": "01a22538-d1f7-765d-8fbb-efd7afcc9459", "name": "Hampers" }, "images": [], "inStock": true, "stockLevel": "low_stock", "variants": [], "seo": { "title": null, "description": null } } }}{ "success": false, "message": "This key doesn’t accept calls from this website: add it under the key’s allowed origins.", "error": { "name": "ForbiddenError", "code": "ORIGIN_NOT_ALLOWED", "statusCode": 403 }, "requestId": "f22cf713-6a8f-43f7-a734-b30d77bd82e5"}Browsers ask first with a preflight OPTIONS request. Sabiwave answers it for any origin and lets
the browser keep the answer for 10 minutes; the real request is the one checked against your list.
curl -X OPTIONS "https://api.sabiwave.com/api/v1/public/catalog/products" \ -H "Origin: https://shop.example.com" \ -H "Access-Control-Request-Method: GET" \ -H "Access-Control-Request-Headers: x-sabiwave-public-key"const res = await fetch('https://api.sabiwave.com/api/v1/public/catalog/products', { method: 'OPTIONS', headers: { Origin: 'https://shop.example.com', 'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'x-sabiwave-public-key', },});const result = await res.json();HTTP/1.1 204 No ContentAccess-Control-Allow-Origin: https://shop.example.comAccess-Control-Allow-Headers: x-sabiwave-public-keyAccess-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETEAccess-Control-Max-Age: 600The API never uses cookies, so leave fetch’s credentials option at its default.
Rotating and revoking keys
Section titled “Rotating and revoking keys”Open a key in Settings › Developer to change it:
- Change what it may do, its websites or its addresses. The change applies to the next call.
- Rotate it to get a new pair with the same name, scopes, websites and addresses. You choose
how long the old pair keeps working: Stop it now, For a day, For 7 days or For 30
days. After that it answers
401 API_KEY_EXPIRED. The new secret key is shown once. - Revoke it to stop it at once. It answers
401 API_KEY_REVOKEDfrom then on.
The list shows when each key was last used. A business can have 20 active keys; giving each website or tool its own key lets you rotate or revoke one without touching the others.