@relwave/sdk
v1.0.0
Published
Official TypeScript SDK for the Relwave API (/api/v1) — subscribers, lists, campaigns and transactional messages.
Maintainers
Readme
@relwave/sdk
Official TypeScript SDK for the Relwave API (/api/v1).
Manage subscribers, mailing lists and campaigns, and send transactional email — fully typed, ESM, fetch-based. Works in Node 18+ (global fetch), Deno, and modern browsers.
Installation
npm install @relwave/sdkQuickstart
import { RelwaveClient } from '@relwave/sdk';
const client = new RelwaveClient({
apiKey: process.env.RELWAVE_API_KEY!, // "nl_xxxxx.secret"
baseUrl: 'https://relwave.com', // optional — "/api/v1" is appended automatically
});
// Send a transactional message
const message = await client.messages.send({
to: '[email protected]',
subject: 'Welcome aboard, {{ name }}!',
html: '<h1>Hi {{ name }}</h1><p>Glad to have you.</p>',
variables: { name: 'Jane' },
tags: ['welcome'],
});
console.log(message.id, message.message_id, message.status);Authentication
Every request is authenticated with your API key. By default it is sent as a bearer token:
Authorization: Bearer nl_xxxxx.secretTo use the alternative header instead, set useApiKeyHeader: true:
const client = new RelwaveClient({
apiKey: 'nl_xxxxx.secret',
useApiKeyHeader: true, // sends `X-Api-Key: nl_xxxxx.secret`
});API
The client exposes four resources. All methods return parsed, typed data
(the response data payload) and throw a RelwaveError on failure.
Subscribers
// List (paginated) — returns { data, meta: { current_page, last_page, per_page, total } }
const page = await client.subscribers.list({ page: 1, per_page: 50 });
console.log(page.data, page.meta.total);
// Create / upsert (idempotent on email)
const sub = await client.subscribers.create({
email: '[email protected]',
first_name: 'New',
status: 'confirmed', // 'pending' | 'confirmed' | 'unsubscribed'
attributes: { plan: 'pro' },
});
// Fetch one
const found = await client.subscribers.get(sub.id);
// Soft-unsubscribe (returns the updated record)
const removed = await client.subscribers.delete(sub.id);Lists
const lists = await client.lists.list(); // MailingList[]Campaigns
const campaigns = await client.campaigns.list(); // Campaign[]Messages
// Send (at least one of `html` / `text` is required)
const message = await client.messages.send({
to: '[email protected]',
subject: 'Your receipt',
text: 'Thanks for your order.',
from: '[email protected]', // optional — falls back to account default
reply_to: '[email protected]', // optional
metadata: { order_id: 4821 }, // optional, free-form
});
// Retrieve a message with its event timeline
const detail = await client.messages.get(message.id);
console.log(detail.status, detail.events);Attachments and templates
import { readFile } from 'node:fs/promises';
await client.messages.send({
to: '[email protected]',
subject: 'Your invoice',
template_id: 12, // the body fills the template's {{content}} slot
markdown: 'Thanks for your order, **{{ first_name }}**.',
variables: { first_name: 'Jane' },
attachments: [
{ filename: 'invoice.pdf', content: (await readFile('invoice.pdf')).toString('base64') },
],
});Up to 10 files, 10 MB in total. Executable and script types are rejected.
Error handling
Any non-2xx response, or a response with a populated errors map, throws a
typed RelwaveError. It carries the HTTP status, the structured errors
map (field -> string[]), and — for rate limits — retryAfter in seconds.
import { RelwaveClient, RelwaveError } from '@relwave/sdk';
try {
await client.messages.send({ to: 'bad', subject: 'Hi', text: 'yo' });
} catch (err) {
if (err instanceof RelwaveError) {
console.error('HTTP', err.status);
console.error(err.toReadableString()); // "to: invalid email address"
if (err.isValidationError) {
// err.status === 422
for (const [field, messages] of Object.entries(err.errors)) {
console.error(field, messages);
}
}
if (err.isRateLimited) {
// err.status === 429 — back off and retry
const waitSeconds = err.retryAfter ?? 1;
await new Promise((r) => setTimeout(r, waitSeconds * 1000));
}
}
throw err;
}Convenience flags: isUnauthorized (401), isForbidden (403),
isNotFound (404), isValidationError (422), isRateLimited (429).
Idempotency
Mutating calls (subscribers.create, subscribers.delete, messages.send)
accept an optional idempotencyKey. It is sent as the X-Idempotency-Key
header; re-sending the same key within 24h replays the original response
without re-running the operation. Use it to safely retry on network failure.
import { randomUUID } from 'node:crypto';
const key = randomUUID();
await client.messages.send(
{ to: '[email protected]', subject: 'Receipt', text: 'Thanks!' },
{ idempotencyKey: key },
);
// Safe to retry with the same key — no duplicate send.
await client.messages.send(
{ to: '[email protected]', subject: 'Receipt', text: 'Thanks!' },
{ idempotencyKey: key },
);Sandbox mode
API keys prefixed with nl_test_ are sandbox keys. Transactional sends
are mocked — the API returns a realistic sent response (sandbox: true) but
no real email is delivered. Useful for integration tests.
const client = new RelwaveClient({ apiKey: 'nl_test_abcde.secret' });
console.log(client.isSandbox); // true
const result = await client.messages.send({
to: '[email protected]',
subject: 'Test',
text: 'No real email is sent.',
});
console.log(result.sandbox); // trueConfiguration reference
| Option | Type | Default | Description |
| ----------------- | --------- | --------------------------- | ---------------------------------------------------- |
| apiKey | string | — (required) | API key, nl_xxxxx.secret. |
| baseUrl | string | https://relwave.com | App origin; /api/v1 is appended automatically. |
| useApiKeyHeader | boolean | false | Send X-Api-Key instead of Authorization: Bearer. |
| headers | object | {} | Extra headers merged into every request. |
| timeoutMs | number | 30000 | Per-request timeout (0 disables). |
| fetch | FetchLike | global fetch | Custom fetch implementation (testing/polyfills). |
Response envelope
Every endpoint responds with { data, meta, errors }. The SDK unwraps it for
you: success returns data (and meta for paginated lists), while a populated
errors map or HTTP ≥ 400 throws RelwaveError.
Types
All request/response shapes are exported: Subscriber, MailingList,
Campaign, Message, MessageStream, MessageEvent, Paginated<T>,
PaginationMeta, CreateSubscriberInput, SendMessageInput,
ListSubscribersParams, RequestOptions, and more.
License
MIT
