# TypeScript SDK

> For AI agents: the index of these docs is at https://shippified.net/docs/llms.txt (every page it lists has a Markdown version like this one).

Install and use shippified-sdk, the typed client for every Shippified resource, plus its webhook signature helper.

`shippified-sdk` is a small TypeScript client for the REST API with no dependencies. It gives you typed methods for each resource and a verifier for [webhook signatures](https://shippified.net/docs/webhooks#verify-the-signature). It runs anywhere `fetch` exists: Node 18+, browsers, Cloudflare Workers, Deno and Bun. The current version is **0.2.2**. Source: [github.com/Shippified/shippified-sdk](https://github.com/Shippified/shippified-sdk).

## Install

Install it straight from GitHub. The repository ships the built `dist/`, so there's no build step:

```bash
npm install shippified-sdk
```

It's on npm as [`shippified-sdk`](https://www.npmjs.com/package/shippified-sdk). To use the latest `main` instead, run `npm install github:Shippified/shippified-sdk`. It's ESM-only (`"type": "module"`), ships its own type declarations, and needs Node 18 or newer.

### What changed in 0.2.2

Published to npm (`npm install shippified-sdk`). No code changes from 0.2.1.

### What changed in 0.2.1

- `apiKeys.create(name, { scope })` takes the key's scope (`"read"` or `"write"`), and `ApiKey` has `scope`.
- `Order.createdAt`: when Shippified created the order.
- `EmailSource.lastRebuild` has `status` (`running`, `done`, `failed` or `interrupted`), `message` and `dismissed`.
- Intake log outcomes add `skipped` (a message from an order you deleted) and `test` (a test post).
- Share-card snapshot spend fields (`totalSpendCents`, `deliveredCount`, `fulfillmentPct`) are optional: they're missing on cards made without Stats.

## Create a client

```typescript
import { ShippifiedClient } from "shippified-sdk";

const shippified = new ShippifiedClient({
  token: process.env.SHIPPIFIED_API_KEY,   // "sk_…" API key (or a session token)
  // baseUrl: "https://shippified.net",     // the default
});
```

| Option | Default | Description |
| --- | --- | --- |
| `token` | none | An [API key](https://shippified.net/docs/authentication) or session token. Needed for everything except the `public` methods. A **Read-only** key can only use methods that make `GET` requests. |
| `baseUrl` | `https://shippified.net` | Instance origin, **without** `/api`. A trailing slash is removed. |
| `fetch` | `globalThis.fetch` | Your own `fetch`, for runtimes without one. Without either, the constructor throws. |

If `token` is missing, authenticated methods throw a plain `Error` (`ShippifiedClient: token required for GET /api/orders`) before sending anything.

:::hint{style="danger"}
Don't create a client with an API key in browser code: the key would be visible to anyone. Run the SDK on your server.
:::

## Resource clients

| Property | Methods | Endpoints |
| --- | --- | --- |
| `orders` | `list(filters)`, `get(id)`, `create(input)`, `patch(id, patch)`, `delete(id)`, `getTracking(id)`, `refreshTracking(id)`, `carrierStatus()`, `syncTracking()`, `reparse()` | `/api/orders…`, `/api/carrier-status` |
| `bots` | `list(opts)`, `get(id)`, `create(body)`, `update(id, patch)`, `delete(id)` | `/api/bots…` |
| `emailSources` | `list(opts)`, `get(id)`, `create(body)`, `update(id, patch)`, `delete(id)`, `poll(id)`, `rebuild(id, { sinceDays })`, `import(body)` | `/api/email-sources…`, `/api/email/import` |
| `templates` | `list()` (built-ins), `listCustom()`, `createCustom(body)`, `updateCustom(id, body)`, `deleteCustom(id)`, `preview(body)`, `matchPreview(body)`, `detect(body)` | `/api/templates…` |
| `subscriptions` | `list(opts)`, `get(id)`, `create(body)`, `update(id, patch)`, `delete(id)` | `/api/subscriptions…` (recurring costs) |
| `shares` | `list()`, `create(body)`, `delete(slug)`, `imageUrl(slug)`, `pageUrl(slug)` | `/api/shares…` |
| `apiKeys` | `list()`, `create(name, { scope })`, `delete(id)`. `create` and `delete` need a session token. | `/api/account/api-keys…` |
| `account` | `setUsername(u)`, `checkAvailability(u)`, `setDisplayName(name)`, `setVisibility(bool)`, `setPublicStats(bool)`, `setTimezone(tz)` | `/api/account/…` |
| `webhookLogs` | `list({ limit })`, `clear()` | `/api/webhook-logs` |
| `webhookSubscriptions` | `list(opts)`, `get(id)`, `create(body)`, `update(id, patch)`, `delete(id)`, `test(id, eventType)`, `rotateSecret(id)` | `/api/webhook-subscriptions…` |
| `billing` | `getUsage()`, `createCheckout(plan, opts)`, `createPortal(returnUrl)` | `/api/billing/…` |
| `public` | `getLandingStats()`, `getProfile(u)`, `getHealth()`, `getOpenApi()` | Public endpoints, no token |

Shortcuts on the client itself: `getState()` (`GET /api/state`), `listOrders()` (every order, paging with `limit=200` until `hasMore` is false), `listBots()` (every bot as `{ bots }`, paging with `limit=200` until `hasMore` is false), `createBot()`, `createShare()`, `getLandingStats()`, `getPublicProfile()`.

No SDK method exists for these; call them with `fetch`:

- `GET /api/bots/:id/logs` (one bot's log), `GET /api/inbox` and `GET /api/inbox/:id`
- Insights: `GET /api/profitability`, `GET /api/calendar`, `GET /api/leaderboard`
- `POST /api/settings`, the embed-copier and Discord-bot endpoints
- `GET /api/account/export` and `DELETE /api/account`, which need a signed-in session and refuse API keys anyway. See [Authentication](https://shippified.net/docs/authentication#actions-that-refuse-api-keys).

## Examples

### List orders with filters

```typescript
const page = await shippified.orders.list({
  status: "shipped",
  hasTracking: true,
  from: "2026-09-01",
  limit: 100,
});

for (const order of page.items) {
  console.log(order.id, order.itemSummary, order.trackingNumber);
}
console.log(`${page.items.length} of ${page.total}, hasMore=${page.hasMore}`);
```

Filters that are `undefined`, `null` or `""` are left out of the query string. `OrderStatus` is `"ordered" | "canceled" | "shipped" | "delivered" | "issue"`, also exported as `ORDER_STATUSES`. The server ignores a status it doesn't recognise and returns **unfiltered** results, so use the typed values instead of casting strings.

### Walk every order

```typescript
const all = await shippified.listOrders();
```

### Create an order

```typescript
const { order, merged } = await shippified.orders.create({
  itemSummary: "Example Console Bundle",
  store: "bestbuy",
  orderNumber: "BBY01-000000000001",
  trackingNumber: "1Z999AA10123456784",
  costCents: 49999,
});
console.log(order.id, merged ? "merged into an existing order" : "created");
```

`OrderCreateInput` matches [`POST /api/orders`](https://shippified.net/docs/orders#create-an-order), including `eventType` (exported values: `ORDER_EVENT_TYPES`).

### Record a sale price

```typescript
await shippified.orders.patch("ord_mpvu9uim_aeh5t", { salePriceCents: 62000 });
await shippified.orders.patch("ord_mpvu9uim_aeh5t", { salePriceCents: null });   // clear it
```

`OrderPatch` lists only what the server honours. `salePriceCents` is always writable; every other field (`costCents`, `estimatedDelivery`, `trackingNumber`, `carrier`, `orderNumber`, `itemSummary`, `quantity`) is written only when the order has no value yet. See [Orders → Update an order](https://shippified.net/docs/orders#update-an-order).

### Tracking

```typescript
const { carriers } = await shippified.orders.carrierStatus();
const cached = await shippified.orders.getTracking("ord_mpvu9uim_aeh5t");
const live = await shippified.orders.refreshTracking("ord_mpvu9uim_aeh5t");
const summary = await shippified.orders.syncTracking();   // { total, refreshed, failed, delivered }
```

`getTracking` and `refreshTracking` return `Promise<unknown>`; the shape is in [Orders → Tracking](https://shippified.net/docs/orders#get-tracking). Carriers without live tracking come back with `liveUnavailable: true`. A background tracker already refreshes in-flight orders, so you don't need to call `syncTracking` on a schedule.

### Create a bot and build its URL

```typescript
const bot = await shippified.bots.create({ name: "Acme Monitor", slug: "acme-monitor", type: "monitor" });
const { user } = await shippified.getState();
const url = `https://shippified.net/api/webhooks/discord/${user.webhookHandle}/${bot.slug}`;
```

### Import an email

```typescript
import { readFile } from "node:fs/promises";

const raw = await readFile("order-confirmation.eml", "utf8");
const result = await shippified.emailSources.import({ emailSourceId: "email_m1x2y3z5", raw });
console.log(result.order.id, result.merged, result.duplicate ?? false);
```

It goes through the same pipeline as live mail. It throws `ShippifiedApiError` with status `403` for a blocked sender, `422` when the email can't be read as an order, and `429` at the plan limit. See [Import a raw email](https://shippified.net/docs/intake#import-a-raw-email).

### Poll and rebuild an email source

```typescript
await shippified.emailSources.poll("email_m1x2y3z5");                       // check mail now
await shippified.emailSources.rebuild("email_m1x2y3z5", { sinceDays: 30 });  // 202, runs in the background

const source = await shippified.emailSources.get("email_m1x2y3z5");
console.log(source.rebuilding, source.lastRebuild, source.lastError);
```

`rebuild` throws `400` for forwarding sources and for sources that need reconnecting (`needs_auth`, `auth_failed`) or are `paused`, before anything is deleted, and `409` while one is already running. To re-run parsing on stored messages without reading the mailbox, use `orders.reparse()`.

### Templates

```typescript
// Which template would win for this email, and what order would it make? Read-only.
const detected = await shippified.templates.detect({ source: "email", sample: rawEmail });

// Custom templates, newest first (the order they're tried in).
const templates = await shippified.templates.listCustom();

// Partial update: omitted fields keep their values. `source` can't change.
await shippified.templates.updateCustom(templates[0].id, { name: "Renamed" });

// Apply template changes to existing orders.
const summary = await shippified.orders.reparse();   // { reparsed, changed, merged, removed, skipped }
```

See the [Templates API](https://shippified.net/docs/templates-api).

### Webhook subscriptions

```typescript
const sub = await shippified.webhookSubscriptions.create({
  url: "https://hooks.example.com/shippified",
  eventTypes: ["order.created", "order.shipped", "order.delivered"],
});
await saveSecret(sub.id, sub.secret!);   // only in this response

const result = await shippified.webhookSubscriptions.test(sub.id, "order.created");
console.log(result.ok, result.status, result.error);

// Subscriptions from before secrets were stored need a rotation to resume.
for (const s of (await shippified.webhookSubscriptions.list()).items) {
  if (s.needsSecretRotation) {
    const rotated = await shippified.webhookSubscriptions.rotateSecret(s.id);
    await saveSecret(rotated.id, rotated.secret);
  }
}
```

### API keys

Any client can list keys; each has `scope` (`"read"` or `"write"`). Creating and revoking keys only works with a session token: with an API key they throw `ShippifiedApiError` with status `403` ("API keys can't manage API keys — use the dashboard."). In practice, create keys in **Settings → Developer**.

```typescript
const { items } = await shippified.apiKeys.list();

// Creating and revoking need a client built with a session token:
const session = new ShippifiedClient({ token: sessionToken });
const { key, record } = await session.apiKeys.create("CI pipeline", { scope: "read" });
// `key` is the raw sk_… secret, returned only now. `scope` defaults to "write".
await session.apiKeys.delete(record.id);
```

## Verify webhook signatures

`verifyShippifiedSignature` checks the `X-Shippified-Signature` header with WebCrypto HMAC-SHA256 and a constant-time comparison. It works in Node 18+, browsers and edge runtimes.

```typescript
import { verifyShippifiedSignature } from "shippified-sdk";

const valid = await verifyShippifiedSignature({
  rawBody,                                             // the exact body string
  header: request.headers.get("x-shippified-signature") ?? "",
  secret: process.env.SHIPPIFIED_WEBHOOK_SECRET!,
});
```

| Option | Description |
| --- | --- |
| `rawBody` | The raw request body as a string. Don't re-serialise parsed JSON. |
| `header` | The `X-Shippified-Signature` value. The `sha256=` prefix is optional. |
| `secret` | The subscription's `whsec_…` secret. |
| `crypto` | Optional `Crypto` implementation where `globalThis.crypto` is missing; otherwise it throws `verifyShippifiedSignature: WebCrypto unavailable…`. |

For the scheme and examples in other languages, see [Webhooks → Verify the signature](https://shippified.net/docs/webhooks#verify-the-signature).

## Errors

Any non-`2xx` response throws `ShippifiedApiError`:

```typescript
import { ShippifiedApiError } from "shippified-sdk";

try {
  await shippified.orders.get("ord_does_not_exist");
} catch (err) {
  if (err instanceof ShippifiedApiError) {
    console.error(err.status);   // 404
    console.error(err.message);  // the API's `error` string, or "HTTP <status>"
    console.error(err.body);     // parsed JSON body (or raw text)
  } else {
    throw err;                   // network error, missing token, …
  }
}
```

| Status | What to do |
| --- | --- |
| `401` | The token is invalid, revoked or expired. Don't retry with it. |
| `403` | The key's scope doesn't allow it: a **Read-only** key made a non-`GET` request, or an API key tried to create or revoke keys. `err.message` says which. |
| `404` | It doesn't exist, or isn't in this workspace. |
| `429` | A rate limit or cap. `err.body.reason === "plan_limit_reached"` means the free plan's monthly limit. |
| `5xx` | Retry with backoff. For `orders.create`, include an `orderNumber` so a retry merges instead of duplicating. `getHealth()` throws a `503` whose `body` is the health report. |

## Types

The exported types (`Order`, `OrderPatch`, `OrderCreateInput`, `Bot`, `EmailSource`, `CustomTemplate`, `CustomTemplateInput`, `MatchRule`, `WebhookSubscription`, `WebhookEventType`, `BillingUsage`, `PublicProfile`, `Paginated<T>` and more) follow the API's wire format as of 0.2.1. They're written by hand. `Order` has an index signature, so a field the server adds later is still reachable as `unknown`. `PublicProfile.level` is `null` when the owner has public stats turned off. For the authoritative field list, see [Orders](https://shippified.net/docs/orders) and the [API reference](https://shippified.net/docs/overview#api-reference).

:::cards
::card{title="MCP server" href="./mcp-server"} Built on this SDK.
::card{title="Orders" href="./orders"} The main resource.
::card{title="Webhooks" href="./webhooks"} Events and signature scheme.
:::

---

## For AI agents

- This page for a human reader: https://shippified.net/docs/sdk
- The whole space as one Markdown file: https://shippified.net/docs/llms-full.txt
- Live search + fetch over MCP: https://shippified.net/docs/_api/public/orgs/5b8789b1-481a-41f5-8e5a-5f30d11f5151/spaces/ae0c5af4-cc84-4899-9078-2faf8e7a6fc5/mcp
  - Install (Claude CLI): `claude mcp add api-docs --transport http https://shippified.net/docs/_api/public/orgs/5b8789b1-481a-41f5-8e5a-5f30d11f5151/spaces/ae0c5af4-cc84-4899-9078-2faf8e7a6fc5/mcp`

This Markdown is the same content the page renders — safe to quote and to ground answers in.
