---
title: "TypeScript SDK"
description: "Publish, wait, paginate, and verify webhooks from JavaScript and TypeScript."
---

> Documentation Index
> Fetch the complete documentation index at: https://pubnio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

`@pubnio/sdk` is the JavaScript and TypeScript client for the PubNio HTTP API.
It ships ESM and CommonJS builds with TypeScript declarations, plus a Solid
Query adapter at `@pubnio/sdk/solid`.

```sh
bun add @pubnio/sdk
# or: npm install @pubnio/sdk
```

Node 20.19 or newer is required for the CommonJS entry.

## Publish in one call

```ts
import { createPubNioClient } from "@pubnio/sdk";

const client = createPubNioClient({
  baseUrl: "https://pubnio.com",
  apiKey: process.env.PUBNIO_API_KEY,
});
const workspace = client.workspace(workspaceId);

const accepted = await workspace.posts.create({
  text: "CSV exports are live. Download a week of reports in one click.",
  platforms: ["x"],
});
console.log(accepted.postId, accepted.idempotencyKey, accepted.targets);
```

Send exactly one selector: `platforms`, `accounts`, or `targets`. The server
derives the provider and defaults the placement. Use `targets` when a
destination needs a placement, schedule, queue, or target-specific text. The
[platform capability matrix](https://pubnio.com/docs/capabilities/) lists every placement, media type,
and caption limit.

`posts.create` generates an `Idempotency-Key` when you omit one and returns it
as `idempotencyKey`. A failed mutation keeps the key on `PubNioError`, so an
ambiguous transport failure can be retried safely.

## Wait for delivery

```ts
const result = await workspace.posts.wait(accepted.postId, {
  timeoutMs: 60_000,
});
console.log(result.settled, result.post.status);

if (!result.settled) {
  console.log("Still working; retry after", result.retryAfterSeconds);
}
```

`posts.wait` stops on a terminal delivery state or returns a still-pending
result at the deadline. `timeoutMs: 0` reads once. Media has the same bounded
pattern:

```ts
const intake = await workspace.media.importUrl(
  "https://cdn.example.com/launch.mp4",
);
const ready = await workspace.media.wait(intake.mediaId, {
  timeoutMs: 30_000,
});
if (ready.status === "ready") console.log(ready.asset?.mediaId);
```

## Pagination

Every cursor list has an `iterate` method that reads pages for you and yields
rows. Filters stay applied on every page.

```ts
for await (const post of workspace.posts.iterate({
  isDraft: true,
  limit: 50,
})) {
  console.log(post.postId, post.textPreview);
}
```

The generic helpers `iterateItems(fetchPage, options)` and
`iteratePages(fetchPage, options)` are exported. A repeated cursor throws
instead of looping.

## Errors

```ts
import { PubNioError } from "@pubnio/sdk";

try {
  await workspace.posts.create({ text: "Launch day", platforms: ["x"] });
} catch (error) {
  if (error instanceof PubNioError) {
    console.error(error.code, error.status, error.requestId);
    console.error(error.param, error.nextAction, error.candidates);
  }
}
```

`code` is stable and machine-readable. `param` names the offending field,
`nextAction` says how to recover, and `candidates` carries account choices
when a lookup is ambiguous.

## Webhooks

Verify the raw body before decoding it. `verifyWebhookSignature` implements
Standard Webhooks HMAC-SHA256, a five-minute timestamp tolerance, and a
constant-time comparison.

```ts
import { verifyWebhookSignature } from "@pubnio/sdk";

const event = await verifyWebhookSignature({
  secret: process.env.PUBNIO_WEBHOOK_SECRET!,
  body: rawBody, // the exact bytes or string received, before JSON.parse
  headers: request.headers,
});
console.log(event.id, event.type);
```

Failures throw `WebhookVerificationError` with a `reason` and a `code`.
Deliveries are at least once: deduplicate by `event.id` and apply each event
to its stated attempt. See [Webhooks](https://pubnio.com/docs/webhooks/) for the full event catalog.

## Surface

The root client exposes account-level namespaces `me`, `workspaces`, and
`audit`. `client.workspace(workspaceId)` returns a scoped client with
`posts`, `media`, `accounts`, `profiles`, `capabilities`, `limits`,
`apiKeys`, `webhooks`, `queues`, `schedule`, `analytics`, `inbox`,
`ads`, `audit`, `notifications`, `members`, and `invites`.

## Solid Query

Use `@pubnio/sdk/solid` with `@tanstack/solid-query` v6. The helpers take
the scoped client, so query keys include its workspace ID.

```ts
import { useMutation, useQuery } from "@tanstack/solid-query";
import { postsCreateMutation, postsListOptions } from "@pubnio/sdk/solid";

const workspace = client.workspace(workspaceId);
const drafts = useQuery(() => postsListOptions(workspace, { isDraft: true }));
const createDraft = useMutation(() => postsCreateMutation(workspace));
```

> **Next**
>
> Read [Publishing](https://pubnio.com/docs/publishing/) for targets and delivery outcomes,
> [Webhooks](https://pubnio.com/docs/webhooks/) for endpoint setup, or use the [Python
> SDK](https://pubnio.com/docs/sdk-python/) for the same surface in Python.

Source: https://pubnio.com/docs/sdk-typescript/index.mdx
