Docs menu

TypeScript SDK

The bubblr TypeScript SDK: typed methods for every endpoint, automatic idempotency keys, pagination, typed errors and webhook signature checks.

@bubblr/sdk is typed from the same OpenAPI description as the API, so every method, field and answer has a type. It uses fetch and Web Crypto and has no dependencies: it runs on Node 20.3 or newer, Deno, Bun, Cloudflare Workers and other runtimes that have both.

Shell
npm install @bubblr/sdk
TypeScript
import { Bubblr } from "@bubblr/sdk";

const bubblr = new Bubblr({ apiKey: process.env.BUBBLR_API_KEY });

Without apiKey it reads BUBBLR_API_KEY. Keep it on your server: a key in a browser can be read by anyone.

Calling the API

Every method takes one object with the endpoint's path parameters, query and body together, named as in the API:

TypeScript
const message = await bubblr.messages.send({ profile_id: "prf_2Wq8nT4kLm", to: "+14155550142", text: "Hi Hannah!" });
const chat = await bubblr.chats.get({ profile_id: "prf_2Wq8nT4kLm", chat_id: message.chat_id });
await bubblr.contacts.update({ address: "+14155550142", name: "Hannah Lee", tags: ["VIP"] });

Each endpoint's page in the API reference shows its SDK call. The methods are grouped like the reference: profiles, messages, chats, uploads, attachments, drafts, events, webhooks, contacts, calling, calls, voiceEndpoints, callRouting, voicemailGreeting, usage, plans and me().

Idempotency

Sends and calls need an Idempotency-Key. The SDK makes one for each call and reuses it on its own retries, so a retried send never goes out twice. Pass your own to stay safe across restarts of your program too:

TypeScript
await bubblr.messages.send({ profile_id, to, text }, { idempotencyKey: `booking-${booking.id}-reminder` });

Lists and pages

Lists that page return something you can await for one page, or iterate for every item:

TypeScript
const page = await bubblr.chats.list({ profile_id, limit: 50 });   // { data, has_more, next_before }

for await (const message of bubblr.messages.list({ profile_id, q: "invoice" })) {
  console.log(message.created_at, message.text);
}

Iterating fetches one page at a time, as you go. .toArray(n) collects up to n items.

Errors

API errors are thrown as APIError subclasses carrying the API's status, code, message and details:

TypeScript
import { APIError, RateLimitError } from "@bubblr/sdk";

try {
  await bubblr.messages.send({ profile_id, to, text });
} catch (err) {
  if (err instanceof RateLimitError) console.log(`retry in ${err.retryAfter} s`);
  else if (err instanceof APIError && err.code === "recipient_not_allowed") console.log("not on this key's list");
  else throw err;
}

The classes follow the status: BadRequestError, AuthenticationError, PaymentRequiredError, PermissionDeniedError, NotFoundError, ConflictError, PayloadTooLargeError, RateLimitError, ServerError. No answer at all is an APIConnectionError (or APITimeoutError). Reads, and sends with their idempotency key, are retried twice after network errors, 429 and 5xx; change it with maxRetries. The codes are on the errors page.

Webhooks

TypeScript
import { verifyWebhook } from "@bubblr/sdk";

const event = await verifyWebhook({ payload: rawBody, signature: request.headers.get("x-signature"), secret: process.env.BUBBLR_WEBHOOK_SECRET! });

It throws WebhookVerificationError when the signature is missing, wrong or more than 5 minutes old, and returns the event otherwise. Pass the raw body, before any JSON parsing. signWebhookPayload makes a signature for your tests. See Webhooks and events.

Live events

TypeScript
const stream = bubblr.events.stream({ types: "message.received" });
for await (const event of stream) {
  console.log(event.type, event.data);
}

The stream reconnects after a dropped connection and ends with stream.close(). It doesn't replay what happened while you weren't connected, so use webhooks for anything you must not miss.

Voice agents

verifyVoiceSignature checks the X-Bubblr-Signature header on calls to your voice agent; calls.transfer hands the caller back to your team. See Voice agents.

Options

TypeScript
new Bubblr({
  apiKey: "sk_live_…",            // default: BUBBLR_API_KEY
  baseUrl: "https://api.bubblr.io",   // default: BUBBLR_BASE_URL, then this
  maxRetries: 2,
  timeoutMs: 60_000,              // per attempt
  fetch: customFetch,             // your own fetch
  headers: { "x-request-source": "billing-worker" },
});

Per request, the second argument takes { idempotencyKey, signal, timeoutMs, maxRetries, headers }.

Other languages: the API is described in OpenAPI 3.1, which most languages can generate a client from. A Python SDK isn't available yet.

Keys come with your first line.

Request an invite and we'll set up a line for your team. API access is part of every plan.

Request an invite