Kraiter
SDK Reference

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.

npm version

Installation

npm install @kraiter/sdk

Or with your preferred package manager:

pnpm add @kraiter/sdk
yarn add @kraiter/sdk

Initialisation

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

OptionTypeRequiredDescription
apiKeystringYes*Your Kraiter API key. Generate one from Settings in the dashboard.
baseUrlstringNoOverride 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>NoDeprecated. An async function that returns a valid JWT. Use apiKey instead.
fetchtypeof fetchNoCustom fetch implementation. Defaults to the global fetch.
timeoutnumberNoRequest 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

PropertyTypeDescription
codestring | undefinedThe machine-readable error code from the API's error envelope (e.g. NOT_FOUND, VALIDATION_ERROR, RATE_LIMITED).
messagestringA human-readable description of the error.
statusCodenumberThe HTTP status code returned by the API.
detailsunknownStructured 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

CodeStatusDescription
VALIDATION_ERROR400The request body failed validation.
UNAUTHORISED401The API key or JWT is missing or invalid.
FORBIDDEN403The token does not grant access to this resource.
NOT_FOUND404The requested resource does not exist.
CONFLICT409A resource with this identifier already exists.
RATE_LIMIT_EXCEEDED429Too 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:

NamespaceDescription
kraiter.contactsCreate, read, update, and delete contacts.
kraiter.eventsTrack custom events against contacts.
kraiter.sendSend transactional emails.
kraiter.templatesManage email templates.
kraiter.sequencesBuild and manage automated sequences.
kraiter.segmentsDefine and compute audience segments.
kraiter.domainsRegister and verify sending domains.
kraiter.campaignsOrganise sequences and templates into campaigns.
kraiter.metricsRetrieve engagement analytics for sequences and templates.
kraiter.scheduledSendsView emails scheduled for future delivery.
kraiter.tenantManage tenant settings and sandbox whitelist.