@emailr/sdk
Official Emailr API SDK for TypeScript/JavaScript.
Installation
npm install @emailr/sdk
pnpm add @emailr/sdk
yarn add @emailr/sdk
Quick Start
import { Emailr } from '@emailr/sdk';
const emailr = new Emailr({ apiKey: 'your-api-key' });
const result = await emailr.emails.send({
to: 'recipient@example.com',
from: 'sender@yourdomain.com',
subject: 'Hello!',
html: '<h1>Hello World</h1>',
});
console.log(result.message_id);
Configuration
const emailr = new Emailr({
apiKey: 'your-api-key',
baseUrl: 'https://api.emailr.dev',
timeout: 30000,
});
Resources
Emails
const result = await emailr.emails.send({
to: 'recipient@example.com',
from: 'sender@yourdomain.com',
subject: 'Hello!',
html: '<h1>Hello World</h1>',
text: 'Hello World',
});
const result = await emailr.emails.send({
to: 'recipient@example.com',
template_id: 'template-uuid',
template_data: { name: 'John', company: 'Acme' },
});
const email = await emailr.emails.get('email-uuid');
const emails = await emailr.emails.list({ page: 1, limit: 50 });
Contacts
const contact = await emailr.contacts.create({
email: 'contact@example.com',
first_name: 'John',
last_name: 'Doe',
metadata: { source: 'website' },
});
const contacts = await emailr.contacts.list({ limit: 100 });
const updated = await emailr.contacts.update('contact-uuid', {
first_name: 'Jane',
});
await emailr.contacts.delete('contact-uuid');
const result = await emailr.contacts.bulkCreate({
contacts: [
{ email: 'user1@example.com', first_name: 'User 1' },
{ email: 'user2@example.com', first_name: 'User 2' },
],
});
Templates
const template = await emailr.templates.create({
name: 'Welcome Email',
subject: 'Welcome to {{company}}',
html_content: '<h1>Welcome {{name}}</h1>',
variables: ['name', 'company'],
});
const templates = await emailr.templates.list();
const updated = await emailr.templates.update('template-uuid', {
subject: 'New Subject',
});
await emailr.templates.delete('template-uuid');
Domains
const domain = await emailr.domains.add({ domain: 'example.com' });
const domains = await emailr.domains.list();
const status = await emailr.domains.verify('domain-uuid');
const dnsStatus = await emailr.domains.checkDns('domain-uuid');
await emailr.domains.delete('domain-uuid');
Webhooks
const webhook = await emailr.webhooks.create({
name: 'Email Events',
url: 'https://api.example.com/webhooks',
events: ['email.sent', 'email.delivered', 'email.bounced'],
});
const webhooks = await emailr.webhooks.list();
await emailr.webhooks.enable('webhook-uuid');
await emailr.webhooks.disable('webhook-uuid');
await emailr.webhooks.delete('webhook-uuid');
Broadcasts
const broadcast = await emailr.broadcasts.create({
name: 'Newsletter - January',
subject: 'Your monthly update',
from_email: 'newsletter@example.com',
segment_id: 'segment-uuid',
html_content: '<h1>Newsletter</h1>',
});
const result = await emailr.broadcasts.send('broadcast-uuid');
await emailr.broadcasts.schedule('broadcast-uuid', '2024-01-15T10:00:00Z');
await emailr.broadcasts.cancel('broadcast-uuid');
Segments
const segment = await emailr.segments.create({
name: 'Active Users',
description: 'Users who opened an email in the last 30 days',
conditions: { opened_email: { within_days: 30 } },
});
const segments = await emailr.segments.list();
const contacts = await emailr.segments.getContacts('segment-uuid');
const count = await emailr.segments.getCount('segment-uuid');
Error Handling
import { Emailr, EmailrError, ValidationError, RateLimitError } from '@emailr/sdk';
try {
await emailr.emails.send({ to: 'invalid-email' });
} catch (error) {
if (error instanceof ValidationError) {
console.error('Validation failed:', error.message, error.details);
} else if (error instanceof RateLimitError) {
console.error('Rate limited. Retry after:', error.retryAfter, 'seconds');
} else if (error instanceof EmailrError) {
console.error('API error:', error.message, error.statusCode, error.code);
}
}
TypeScript Support
This SDK is written in TypeScript and provides full type definitions for all API requests and responses.
import type { SendEmailRequest, Email, Contact } from '@emailr/sdk';
const request: SendEmailRequest = {
to: 'recipient@example.com',
subject: 'Hello',
html: '<h1>Hello</h1>',
};
License
MIT