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 { Kraiter } from '@kraiter/sdk';
const kraiter = new Kraiter({
apiKey: process.env.KRAITER_API_KEY,
});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. |
getToken | () => Promise<string> | No | Deprecated. An async function that returns a valid JWT. Use apiKey instead. |
* 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 Kraiter({
apiKey: process.env.KRAITER_API_KEY,
});Error handling
All SDK methods throw a KraiterError when the API returns a non-2xx response. The error includes structured information about what went wrong.
import { KraiterError } from '@kraiter/sdk';
try {
await kraiter.contacts.get('non-existent-id');
} catch (error) {
if (error instanceof KraiterError) {
console.error(error.code); // e.g. "NOT_FOUND"
console.error(error.message); // e.g. "Contact not found"
console.error(error.statusCode); // e.g. 404
}
}KraiterError properties
| Property | Type | Description |
|---|---|---|
code | string | A machine-readable error code (e.g. NOT_FOUND, VALIDATION_ERROR, RATE_LIMIT_EXCEEDED). |
message | string | A human-readable description of the error. |
statusCode | number | The HTTP status code returned by the API. |
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 use async iterators. This means you can use for await...of to iterate through all results without manually managing cursors.
for await (const contact of kraiter.contacts.list()) {
console.log(contact.email);
}The SDK fetches pages behind the scenes and yields items one by one. You can pass limit and cursor to control page size and starting position.
// Fetch contacts in pages of 50
for await (const contact of kraiter.contacts.list({ limit: 50 })) {
console.log(contact.email);
}If you need to break out of iteration early, simply use break — the SDK will stop fetching subsequent pages.
let count = 0;
for await (const contact of kraiter.contacts.list()) {
console.log(contact.email);
count++;
if (count >= 10) break; // stop after 10 results
}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. |