SDK Overview
Install, configure, and use the Kraiter TypeScript/JavaScript SDK to interact with the Kraiter API.
The @kraiter/sdk package gives you a typed, Promise-based interface to every Kraiter API resource. It handles authentication, pagination, and error handling so you can focus on your integration logic.
Installation
npm install @kraiter/sdkOr with your preferred package manager:
pnpm add @kraiter/sdk
yarn add @kraiter/sdkInitialisation
Create a client instance by passing your API key. Generate one from Settings in the dashboard.
import { Mailer } from '@kraiter/sdk';
const kraiter = new Mailer({
apiKey: process.env.KRAITER_API_KEY,
});The exported client class is named Mailer. You can bind it to whatever variable name you like — the examples throughout this reference use kraiter.
Configuration options
| Option | Type | Required | Description |
|---|---|---|---|
apiKey | string | Yes* | Your Kraiter API key. Generate one from Settings in the dashboard. |
baseUrl | string | No | Override the API base URL. Defaults to https://api.kraiter.com/api. The /api suffix is part of the default and must be kept when overriding (e.g. https://staging.example.com/api). |
getToken | () => Promise<string> | No | Deprecated. An async function that returns a valid JWT. Use apiKey instead. |
fetch | typeof fetch | No | Custom fetch implementation. Defaults to the global fetch. |
timeout | number | No | Request timeout in milliseconds. Defaults to 30000. |
* Either apiKey or getToken must be provided.
Authentication
The SDK uses bearer-token authentication. When you provide an apiKey, it is sent as a Bearer token with every request. No token refresh logic is needed — API keys do not expire (but can be revoked from the dashboard).
const kraiter = new Mailer({
apiKey: process.env.KRAITER_API_KEY,
});Error handling
All SDK methods throw a MailerError when the API returns a non-2xx response. The error includes structured information about what went wrong. The SDK does not retry failed requests — retries, if any, are your responsibility.
import { MailerError } from '@kraiter/sdk';
try {
await kraiter.contacts.get('non-existent-id');
} catch (error) {
if (error instanceof MailerError) {
console.error(error.code); // e.g. "NOT_FOUND"
console.error(error.message); // e.g. "Contact not found"
console.error(error.statusCode); // e.g. 404
console.error(error.details); // structured details, e.g. per-field validation errors
}
}MailerError properties
| Property | Type | Description |
|---|---|---|
code | string | undefined | The machine-readable error code from the API's error envelope (e.g. NOT_FOUND, VALIDATION_ERROR, RATE_LIMITED). |
message | string | A human-readable description of the error. |
statusCode | number | The HTTP status code returned by the API. |
details | unknown | Structured error details when the API supplies them (e.g. per-field validation errors); otherwise the raw error body. |
Requests that exceed the configured timeout reject with a MailerTimeoutError instead — this is a separate class (not a subclass of MailerError), so check for it explicitly if you need to distinguish timeouts.
import { MailerTimeoutError } from '@kraiter/sdk';
try {
await kraiter.contacts.list();
} catch (error) {
if (error instanceof MailerTimeoutError) {
console.error('Request timed out');
}
}Common error codes
| Code | Status | Description |
|---|---|---|
VALIDATION_ERROR | 400 | The request body failed validation. |
UNAUTHORISED | 401 | The API key or JWT is missing or invalid. |
FORBIDDEN | 403 | The token does not grant access to this resource. |
NOT_FOUND | 404 | The requested resource does not exist. |
CONFLICT | 409 | A resource with this identifier already exists. |
RATE_LIMIT_EXCEEDED | 429 | Too many requests — back off and retry. |
Pagination
Methods that return collections resolve to a single page shaped as { items, nextCursor }. The SDK does no hidden fetching: pass limit to control page size, and feed nextCursor back in as cursor to fetch the next page. When nextCursor is undefined, you have reached the end.
// Fetch a single page of up to 50 contacts
const page = await kraiter.contacts.list({ limit: 50 });
for (const contact of page.items) {
console.log(contact.email);
}To walk every page, loop until nextCursor is undefined:
let cursor: string | undefined;
do {
const page = await kraiter.contacts.list({ cursor, limit: 50 });
for (const contact of page.items) {
console.log(contact.email);
}
cursor = page.nextCursor;
} while (cursor);To stop early, simply break out of your loop and stop requesting further pages.
Resource namespaces
The SDK organises methods under resource namespaces on the client instance:
| Namespace | Description |
|---|---|
kraiter.contacts | Create, read, update, and delete contacts. |
kraiter.events | Track custom events against contacts. |
kraiter.send | Send transactional emails. |
kraiter.templates | Manage email templates. |
kraiter.sequences | Build and manage automated sequences. |
kraiter.segments | Define and compute audience segments. |
kraiter.domains | Register and verify sending domains. |
kraiter.campaigns | Organise sequences and templates into campaigns. |
kraiter.metrics | Retrieve engagement analytics for sequences and templates. |
kraiter.scheduledSends | View emails scheduled for future delivery. |
kraiter.tenant | Manage tenant settings and sandbox whitelist. |