mailglyph
v1.2.0
Published
Official MailGlyph Node.js / TypeScript SDK
Readme
MailGlyph Node.js SDK
Official Node.js / TypeScript SDK for the MailGlyph API.
Install
npm install mailglyphyarn add mailglyphInitialize
import MailGlyph from 'mailglyph';
// Secret key client (all endpoints except /v1/track)
const client = new MailGlyph('sk_your_api_key');
// Public key client (/v1/track only)
const tracker = new MailGlyph('pk_your_public_key');Emails
// HTML + explicit text
const result = await client.emails.send({
to: '[email protected]',
from: { name: 'My App', email: '[email protected]' },
subject: 'Welcome!',
body: '<h1>Hello {{name}}</h1>',
text: 'Hello {{name}}',
data: { name: 'John' }
});
// HTML only (backend auto-generates text from body)
await client.emails.send({
to: '[email protected]',
from: '[email protected]',
subject: 'HTML only',
body: '<h1>Hello</h1><p>This is HTML-only content.</p>'
});
// HTML + text="" (opt out of text/plain part)
await client.emails.send({
to: '[email protected]',
from: '[email protected]',
subject: 'No text/plain part',
body: '<h1>Hello</h1>',
text: ''
});
const verification = await client.emails.verify('[email protected]');
console.log(verification.data.valid, verification.data.isRandomInput);Events
await tracker.events.track({
email: '[email protected]',
event: 'purchase',
data: { product: 'Premium', amount: 99 }
});
const { eventNames } = await client.events.listNames();Contacts
const { data, cursor, hasMore } = await client.contacts.list({ limit: 50 });
console.log(data.length, cursor, hasMore);
const contact = await client.contacts.create({
email: '[email protected]',
data: { firstName: 'John', plan: 'premium' }
});
await client.contacts.update(contact.id, { subscribed: false });
await client.contacts.delete(contact.id);Templates
const templates = await client.templates.list({ type: 'TRANSACTIONAL', search: 'welcome' });
console.log(templates.data.length, templates.totalPages);
const template = await client.templates.create({
name: 'Welcome',
subject: 'Welcome!',
body: '<h1>Welcome</h1>',
type: 'TRANSACTIONAL'
});
await client.templates.update(template.id, { subject: 'Welcome to MailGlyph' });
await client.templates.delete(template.id);Segments
const segment = await client.segments.create({
name: 'Premium Users',
condition: {
logic: 'AND',
groups: [{ filters: [{ field: 'data.plan', operator: 'equals', value: 'premium' }] }]
},
trackMembership: true
});
const members = await client.segments.listContacts(segment.id, { page: 1 });
const addMembersResult = await client.segments.addStaticMembers(segment.id, {
emails: ['[email protected]', '[email protected]']
});
console.log(addMembersResult.added, addMembersResult.notFound);
const removeMembersResult = await client.segments.removeStaticMembers(segment.id, {
emails: ['[email protected]']
});
console.log(removeMembersResult.removed);Campaigns
const campaign = await client.campaigns.create({
name: 'Product Launch',
subject: 'Introducing our new feature!',
body: '<h1>Big news!</h1><p>Check out our latest feature.</p>',
from: '[email protected]',
audienceType: 'FILTERED',
audienceCondition: {
logic: 'AND',
groups: [{ filters: [{ field: 'subscribed', operator: 'equals', value: true }] }]
}
});
const campaignPage = await client.campaigns.list({ page: 1, pageSize: 20, status: 'DRAFT' });
console.log(campaignPage.data.length, campaignPage.total);
await client.campaigns.send(campaign.id, {
scheduledFor: '2026-03-01T10:00:00Z'
});
await client.campaigns.test(campaign.id, '[email protected]');
const stats = await client.campaigns.stats(campaign.id);
await client.campaigns.cancel(campaign.id);Configuration
const customClient = new MailGlyph('sk_your_api_key', {
baseUrl: 'https://api.mailglyph.com',
timeout: 30000
});Development
npm run lint
npm run typecheck
npm test
npm run build