# Send your first iMessage with TypeScript

Install @bubbl3/sdk, create a client with a test API key, and call messages.create with a test number and an idempotency key. Replies arrive as signed message.received webhooks, which verifyWebhook checks. With a test key everything stays in the sandbox, and going live means swapping in a live key.

## The short version

1. **Install the SDK** Add @bubbl3/sdk to a Node 20 or later project.
2. **Get a test key** Ask a workspace owner or admin for a test API key, and keep it on your server.
3. **Send a message** Call messages.create with a test number, a body and your own idempotency key.
4. **Receive replies** Create a webhook endpoint and check each delivery with verifyWebhook before you trust it.

This guide sends a message from Node with the bubbl3 TypeScript SDK, receives the reply, and covers the two things every integration needs: idempotency and the sandbox. Every example uses a test key and a test number, so nothing reaches a real person.

## Install `@bubbl3/sdk`

`@bubbl3/sdk` is a typed client generated from the same OpenAPI document as the API reference, plus webhook verification. It runs on Node 20 or later.

```bash
npm install @bubbl3/sdk
```

## API key

Ask your workspace owner or an admin for a key. Keys are created in the dashboard under Developers and then API keys, and each is shown once.

- A **test key** starts with `b3_test_`. Its messages go to the simulator, and it can only send to test numbers.
- A **live key** starts with `b3_live_` and sends to real contacts through your numbers.

Keep keys on your server, in an environment variable such as `BUBBL3_API_KEY`. Never put one in a browser, an app bundle or a repository.

## Send

```ts
import { Bubbl3 } from '@bubbl3/sdk';

const b3 = new Bubbl3({ apiKey: process.env.BUBBL3_API_KEY });

const msg = await b3.messages.create(
  {
    to: '+19175550142',
    pool_id: 'pool_reminders', // rotate across a pool
    body: 'Your order has shipped',
  },
  { idempotencyKey: 'order-8812-shipped' },
);

console.log(msg.channel, msg.status); // "imessage" "queued"
```

The message starts as `queued`. bubbl3 then picks a number, checks consent, quiet hours and the number's capacity, and sends it. A send over a number's limit isn't refused; it waits until it can go. See [the TypeScript SDK](/developers/sdk) for every method.

## Webhook for replies

Replies, delivery status and opt-outs come to your server as signed webhooks. Create an endpoint in the dashboard under Developers and then Webhooks, store its `whsec_` secret, and check every delivery against the **raw** body before you parse it:

```ts
import express from 'express';
import { verifyWebhook } from '@bubbl3/sdk';

const app = express();

app.post('/hooks/bubbl3', express.raw({ type: 'application/json' }), (req, res) => {
  const signature = req.headers['bubbl3-signature'];
  if (!verifyWebhook(req.body, signature, process.env.BUBBL3_WEBHOOK_SECRET)) {
    return res.status(400).end();
  }
  const event = JSON.parse(req.body.toString('utf8'));
  res.status(200).end(); // answer within 10 seconds, then do the work
  handle(event); // skip event.id if you have seen it before
});
```

Deliveries are retried for up to 24 hours and can arrive more than once, so record each event's `id` and skip repeats. See [webhooks](/developers/webhooks).

## Idempotency

Network errors happen, and a retry must never send twice. Pass an idempotency key on every send, named after the thing you're doing once, such as `order-8812-shipped`, not a fresh random value per attempt.

- The same key with the same request returns the first response instead of sending again.
- The same key with a different request is refused with `409`.
- Keys last 24 hours.

The SDK retries reads, and writes that have an idempotency key, after a network error, a `429` or a `5xx`. See [idempotency key](/glossary/idempotency-key).

## Sandbox

With a test key, messages go to your workspace's sandbox number and the simulator. They follow the whole pipeline, with status changes, receipts and fallback, so your webhooks fire as they would live. You can reply as the contact would:

```ts
import { Bubbl3 } from '@bubbl3/sdk';

// With a test key (b3_test_…), every call stays in the sandbox
const b3 = new Bubbl3({ apiKey: process.env.BUBBL3_API_KEY });

await b3.messages.create(
  { to: '+19175550142', body: 'Hi Maya, still good for Thursday at 4:30?' },
  { idempotencyKey: 'confirm-4821' },
);

// Reply as the contact would, from the same test number
await b3.sandbox.simulateInbound({
  from: '+19175550142',
  body: 'Yes, see you then',
});
// → your message.received webhook fires, as it would live
```

When it works, swap in a live key. Nothing else changes. See [the sandbox](/developers/sandbox).

## Keep reading

- [TypeScript SDK](https://bubbl3.com/developers/sdk)
- [Webhooks](https://bubbl3.com/developers/webhooks)
- [Sandbox](https://bubbl3.com/developers/sandbox)

Updated: 2026-10-15
