# SDK

The API as a typed TypeScript client: every method, with the same names and the same input.

## Get it

> The package is not on npm yet. Until it is published, use it from the repository, where it lives in `packages/opensend`. After that it installs as `@open-send/opensend`.

It runs wherever `fetch` does: Node.js 20 or newer, Bun, Deno and edge runtimes. It has no dependencies. This is version 0.1.0.

## Make a client

With nothing given, the client reads the key from `OPENSEND_API_KEY` and calls our cloud, at `https://api.opensend.sh`. Set `OPENSEND_API_URL` to reach a server of your own.

```ts
import { OpenSend } from '@open-send/opensend'

export const opensend = new OpenSend()
```

Or give them yourself.

```ts
import { OpenSend } from '@open-send/opensend'

export const opensend = new OpenSend(process.env.OPENSEND_API_KEY, {
  baseUrl: 'http://localhost:2222',
  timeoutMs: 10_000,
})
```

| Option | Meaning |
| --- | --- |
| `baseUrl` | The address of the API. Without it the client reads `OPENSEND_API_URL`, and without that it calls our cloud. |
| `timeoutMs` | How long to wait for an answer. Without it the client waits 30 seconds. |
| `fetch` | A `fetch` of your own, for a proxy or a test. |

`createOpenSend(key, options)` makes the same client without `new`.

## Call a method

A method is `opensend.<resource>.<method>(input)`, and it answers with what the API answers. A method that acts on one thing also takes the id first.

```ts
import { OpenSend } from '@open-send/opensend'

const opensend = new OpenSend()

const list = await opensend.emails.list({ status: 'bounced', limit: 20 })

const email = await opensend.emails.get('em_02')

const moved = await opensend.emails.update('em_09', { scheduledAt: '2026-12-05T09:00:00Z' })

const stopper = new AbortController()
const sent = await opensend.emails.send(
  {
    from: 'Acme <hello@mail.example.com>',
    to: 'ada@example.com',
    subject: 'Your receipt for November',
    text: 'Thanks for your order.',
  },
  { idempotencyKey: 'receipt-2026-11-ada', signal: stopper.signal },
)

console.log(list.total, email.status, moved.scheduledAt, sent.id)
```

The last argument holds what belongs to this one call.

| Option | Meaning |
| --- | --- |
| `idempotencyKey` | Sent as the `idempotency-key` header, so a retry sends nothing twice. |
| `signal` | An `AbortSignal` that gives the call up early. |

The input and the answer of every method are typed, from the same schemas the API checks them with.

## Errors

A call that fails throws `OpenSendError`. Its `code` is the one to branch on, `status` is the HTTP status, `message` says what is wrong, and `requestId` finds the request in the logs.

```ts
import { OpenSend, OpenSendError } from '@open-send/opensend'

const opensend = new OpenSend()

try {
  await opensend.domains.create({ name: 'mail.example.com' })
} catch (error) {
  if (!(error instanceof OpenSendError)) throw error
  if (error.code === 'domain_taken') {
    console.log(error.message, error.status, error.requestId)
  }
}
```

The API has its own [error codes](https://opensend.sh/docs/errors). The client adds these for what goes wrong before or without an answer, with `status` set to `null`.

| Code | Meaning |
| --- | --- |
| `missing_api_key` | No key was given to the client, and none is in the environment. |
| `missing_parameter` | A value the request needs, such as an id, was not given. |
| `network_error` | The API could not be reached. |
| `timeout` | The API did not answer in time. |
| `unexpected_response` | The API answered with something that is not JSON. |
| `invalid_signature` | The signature of a webhook is missing, too old or wrong. |

## Webhooks

`verifyWebhook` checks the signature of a request to your endpoint and returns the event. [Webhooks](https://opensend.sh/docs/webhooks) shows a whole handler. The name of the header, `opensend-signature`, is exported too, as `OPENSEND_SIGNATURE_HEADER`.

## Other languages

TypeScript is the only SDK there is. From any other language, call the API over HTTP: [the reference](https://opensend.sh/docs/api) shows every request as cURL, and `GET /v1/openapi.json` is an OpenAPI document that a generator can turn into a client.

## Every method

| Method | What it does |
| --- | --- |
| opensend.emails.send() | Send email |
| opensend.emails.batch() | Send emails in a batch |
| opensend.emails.update() | Reschedule email |
| opensend.emails.cancel() | Cancel email |
| opensend.emails.get() | Retrieve email |
| opensend.emails.list() | List emails |
| opensend.inbound.list() | List received emails |
| opensend.inbound.get() | Retrieve received email |
| opensend.domains.create() | Add domain |
| opensend.domains.verify() | Verify domain |
| opensend.domains.get() | Retrieve domain |
| opensend.domains.list() | List domains |
| opensend.domains.update() | Update domain |
| opensend.domains.remove() | Delete domain |
| opensend.templates.create() | Create template |
| opensend.templates.get() | Get template |
| opensend.templates.list() | List templates |
| opensend.templates.update() | Update template |
| opensend.templates.edit() | Edit template design |
| opensend.templates.publish() | Publish template |
| opensend.templates.test() | Send test of template |
| opensend.templates.render() | Render template design |
| opensend.templates.duplicate() | Duplicate template |
| opensend.templates.versions() | List template versions |
| opensend.templates.version() | Get template version |
| opensend.templates.restore() | Restore template version |
| opensend.templates.remove() | Delete template |
| opensend.templates.starters() | List template starters |
| opensend.templates.starter() | Get template starter |
| opensend.images.add() | Add image |
| opensend.images.upload() | Upload image |
| opensend.images.list() | List images |
| opensend.images.get() | Retrieve image |
| opensend.images.remove() | Delete image |
| opensend.broadcasts.create() | Create broadcast |
| opensend.broadcasts.update() | Update broadcast |
| opensend.broadcasts.send() | Send broadcast |
| opensend.broadcasts.test() | Send test of broadcast |
| opensend.broadcasts.duplicate() | Duplicate broadcast |
| opensend.broadcasts.get() | Retrieve broadcast |
| opensend.broadcasts.list() | List broadcasts |
| opensend.broadcasts.remove() | Delete broadcast |
| opensend.audiences.create() | Create audience |
| opensend.audiences.list() | List audiences |
| opensend.audiences.get() | Retrieve audience |
| opensend.audiences.update() | Rename audience |
| opensend.audiences.remove() | Delete audience |
| opensend.audiences.join() | Add contact to audience |
| opensend.audiences.leave() | Remove contact from audience |
| opensend.contacts.create() | Add contact |
| opensend.contacts.list() | List contacts |
| opensend.contacts.get() | Retrieve contact |
| opensend.contacts.update() | Update contact |
| opensend.contacts.remove() | Delete contact |
| opensend.contactProperties.create() | Create contact property |
| opensend.contactProperties.list() | List contact properties |
| opensend.contactProperties.get() | Retrieve contact property |
| opensend.contactProperties.update() | Update contact property |
| opensend.contactProperties.remove() | Delete contact property |
| opensend.topics.create() | Create topic |
| opensend.topics.list() | List topics |
| opensend.topics.get() | Retrieve topic |
| opensend.topics.update() | Update topic |
| opensend.topics.remove() | Delete topic |
| opensend.suppressions.create() | Suppress an address |
| opensend.suppressions.list() | List suppressed addresses |
| opensend.suppressions.remove() | Remove a suppressed address |
| opensend.automations.create() | Create automation |
| opensend.automations.update() | Update automation |
| opensend.automations.publish() | Publish automation |
| opensend.automations.pause() | Pause automation |
| opensend.automations.resume() | Resume automation |
| opensend.automations.test() | Test automation |
| opensend.automations.enroll() | Enrol in automation |
| opensend.automations.duplicate() | Duplicate automation |
| opensend.automations.get() | Retrieve automation |
| opensend.automations.list() | List automations |
| opensend.automations.remove() | Delete automation |
| opensend.runs.list() | List runs |
| opensend.runs.get() | Retrieve run |
| opensend.runs.cancel() | Cancel run |
| opensend.runs.retry() | Retry run |
| opensend.events.send() | Send event |
| opensend.events.create() | Define event |
| opensend.events.list() | List events |
| opensend.events.get() | Retrieve event |
| opensend.events.update() | Update event |
| opensend.events.remove() | Delete event |
| opensend.webhooks.create() | Add webhook |
| opensend.webhooks.list() | List webhooks |
| opensend.webhooks.update() | Update webhook |
| opensend.webhooks.remove() | Delete webhook |
| opensend.webhooks.test() | Send test event |
| opensend.webhooks.deliveries() | List webhook deliveries |
| opensend.metrics.get() | Retrieve metrics |
| opensend.logs.list() | List logs |
| opensend.logs.get() | Retrieve log |
| opensend.apiKeys.create() | Create API key |
| opensend.apiKeys.enroll() | Enroll existing API key |
| opensend.apiKeys.list() | List API keys |
| opensend.apiKeys.remove() | Revoke API key |
| opensend.imports.inspect() | Look into another account |
| opensend.imports.create() | Start import |
| opensend.imports.get() | Retrieve import |
| opensend.imports.list() | List imports |
| opensend.imports.cancel() | Cancel import |
| opensend.workspace.get() | Retrieve workspace |
| opensend.workspace.update() | Update workspace |
| opensend.workspace.close() | Close workspace |
| opensend.members.invite() | Invite member |
| opensend.members.list() | List members |
| opensend.members.update() | Update member |
| opensend.members.remove() | Remove member |
| opensend.billing.plans() | List plans |
| opensend.billing.checkout() | Start checkout |
| opensend.billing.portal() | Open billing portal |
| opensend.billing.invoices() | List invoices |
| opensend.server.get() | Retrieve server settings |
| opensend.server.update() | Update server settings |
| opensend.account.get() | Retrieve account |
| opensend.methods.list() | List methods |
| opensend.ask.send() | Ask the assistant |
| opensend.ask.chats() | List chats |
| opensend.ask.chat() | Retrieve chat |
| opensend.ask.save() | Keep chat |
| opensend.ask.forget() | Forget chat |
