Misar IO Docs

TypeScript / JavaScript SDK

Full reference for the MisarMail TypeScript and JavaScript client library.

TypeScript / JavaScript SDK

Installation

npm install misarmail
# or
pnpm add misarmail
# or
yarn add misarmail

Requires Node.js 18+ or any modern browser environment.

Setup

import { MisarMailClient } from 'misarmail';

const client = new MisarMailClient({
  apiKey: process.env.MISARMAIL_API_KEY!,
  // Optional:
  baseUrl: 'https://api.misar.io/mail/v1', // default
  timeout: 30_000,                          // ms, default 30s
  maxRetries: 3,                            // default 3
});

Email

Send email

const result = await client.email.send({
  from: { email: '[email protected]', name: 'Your Name' },
  to: [{ email: '[email protected]' }],
  subject: 'Hello!',
  html: '<p>Hello from MisarMail</p>',
  text: 'Hello from MisarMail',
  tags: ['transactional'],
  sandbox: false,
});
// result.messageId, result.accepted, result.rejected

Send with template

await client.email.send({
  from: { email: '[email protected]' },
  to: [{ email: '[email protected]' }],
  templateId: 'tmpl_01ABCDEF',
  variables: { first_name: 'Alice', order_id: '1234' },
});

Campaigns

// List
const { data } = await client.campaigns.list({ status: 'draft', limit: 20 });

// Create
const campaign = await client.campaigns.create({
  name: 'June Newsletter',
  subject: 'What\'s new',
  fromEmail: '[email protected]',
  html: '<p>...</p>',
});

// Send
await client.campaigns.send(campaign.id, { scheduledAt: '2025-06-15T09:00:00Z' });

// Get stats
const stats = await client.campaigns.get(campaign.id);

// A/B test
await client.abTests.create(campaign.id, {
  subject: 'Alternative subject',
  splitPercent: 20,
});

Contacts

// List
const { data, pagination } = await client.contacts.list({
  status: 'active',
  page: 1,
  limit: 50,
});

// Create
const contact = await client.contacts.create({
  email: '[email protected]',
  firstName: 'Alice',
  tags: ['customer'],
  metadata: { plan: 'pro' },
});

// Update
await client.contacts.update(contact.id, { tags: ['customer', 'vip'] });

// Bulk import
const result = await client.contacts.import({
  contacts: [
    { email: '[email protected]' },
    { email: '[email protected]' },
  ],
  tags: ['imported'],
});
// result.imported, result.skipped, result.failed

Templates

// Create
const template = await client.templates.create({
  name: 'Welcome',
  type: 'transactional',
  subject: 'Welcome, {{first_name}}!',
  html: '<h1>Hi {{first_name}}!</h1>',
  variables: ['first_name'],
});

// Preview (returns rendered HTML)
const preview = await client.templates.preview(template.id, {
  variables: { first_name: 'Alice' },
});

// Render without saving
const rendered = await client.templates.render({
  html: '<p>Hello {{name}}!</p>',
  subject: 'Hello {{name}}',
  variables: { name: 'Alice' },
});

Analytics

const metrics = await client.analytics.get({
  startDate: '2025-06-01',
  endDate: '2025-06-30',
  granularity: 'day',
});

const insights = await client.analytics.insights();

const report = await client.analytics.generateReport({
  type: 'campaign_performance',
  startDate: '2025-06-01',
  endDate: '2025-06-30',
});

Validation

// Single
const check = await client.validate.check('[email protected]');
// check.valid, check.result, check.checks

// Bulk
const results = await client.validate.bulk([
  '[email protected]',
  'bad@email',
]);

Webhooks

// Create
const hook = await client.webhooks.create({
  url: 'https://yourapp.com/hooks/mail',
  events: ['email.delivered', 'email.bounced'],
  secret: 'whsec_...',
});

// List
const { data } = await client.webhooks.list();

// Delete
await client.webhooks.delete(hook.id);

Tracking

await client.tracking.event({
  email: '[email protected]',
  event: 'trial_started',
  properties: { plan: 'pro' },
});

await client.tracking.purchase({
  email: '[email protected]',
  orderId: 'order_1234',
  revenue: 49.99,
  currency: 'USD',
});

AI

const subjects = await client.ai.subjectLines({
  context: 'B2B SaaS product launch newsletter',
  count: 5,
  tone: 'professional',
});

const segment = await client.ai.buildSegment({
  description: 'Contacts who opened 3+ emails in 30 days but never purchased',
});

Error handling

import { MisarMailError, MisarMailNetworkError } from 'misarmail';

try {
  await client.email.send({ ... });
} catch (err) {
  if (err instanceof MisarMailError) {
    // err.status    — HTTP status code (number)
    // err.error     — machine-readable error key (string)
    // err.message   — human-readable message (string)
    // err.requestId — for support tickets
    console.error(`[${err.status}] ${err.error}: ${err.message}`);
  } else if (err instanceof MisarMailNetworkError) {
    console.error('Network error:', err.message);
  }
}

TypeScript types

All request and response types are exported:

import type {
  SendEmailOptions,
  SendEmailResponse,
  Campaign,
  CreateCampaignOptions,
  Contact,
  CreateContactOptions,
  EmailTemplate,
  AnalyticsMetrics,
  WebhookEvent,
} from 'misarmail';