Node.js SDK
The official Node.js SDK for Veil Mail with full TypeScript support.
- Full TypeScript support with exported types
- Zero dependencies (uses native fetch)
- Typed error classes for easy error handling
- Tree-shakeable ESM module
Installation
# npm
npm install @veilmail/sdk
# pnpm
pnpm add @veilmail/sdk
# yarn
yarn add @veilmail/sdkQuick Start
quickstart.ts
import { VeilMail } from '@veilmail/sdk';
// Initialize the client
const client = new VeilMail('veil_live_xxxxx');
// Send an email
const email = await client.emails.send({
from: 'hello@yourdomain.com',
to: 'user@example.com',
subject: 'Welcome!',
html: '<h1>Welcome to our platform!</h1>',
});
console.log('Email sent:', email.id);Configuration
You can pass a configuration object instead of just an API key:
configuration.ts
const client = new VeilMail({
apiKey: 'veil_live_xxxxx',
baseUrl: 'https://api.veilmail.xyz', // Optional, defaults to production
timeout: 30000, // Optional, request timeout in ms
});Available Resources
| Resource | Description |
|---|---|
client.emails | Send and manage emails |
client.domains | Domain verification |
client.templates | Email templates |
client.audiences | Audience management |
client.campaigns | Email campaigns |
client.webhooks | Webhook configuration |
TypeScript Support
All types are exported from the package for use in your application:
types.ts
import type {
// Email types
Email,
EmailStatus,
SendEmailParams,
// Domain types
Domain,
DomainStatus,
// Template types
Template,
TemplateVariable,
// Audience types
Audience,
Subscriber,
SubscriberStatus,
// Campaign types
Campaign,
CampaignStatus,
CampaignStats,
// Webhook types
Webhook,
WebhookEvent,
// Common types
PaginatedResponse,
PaginationParams,
} from '@veilmail/sdk';Error Handling
The SDK provides typed error classes for different error scenarios:
error-handling.ts
import {
VeilMailError,
AuthenticationError,
ValidationError,
NotFoundError,
PiiDetectedError,
RateLimitError,
ServerError,
TimeoutError,
NetworkError,
} from '@veilmail/sdk';
try {
await client.emails.send({ ... });
} catch (error) {
if (error instanceof PiiDetectedError) {
console.log('PII types detected:', error.piiTypes);
} else if (error instanceof RateLimitError) {
console.log('Retry after:', error.retryAfter, 'seconds');
} else if (error instanceof VeilMailError) {
console.log('Error:', error.code, error.message);
}
}See the Errors documentation for more details on error handling.
Usage Examples
Send Email with Template
send-template.ts
const email = await client.emails.send({
from: 'orders@yourdomain.com',
to: 'customer@example.com',
templateId: 'template_xxxxx',
variables: {
customerName: 'John Doe',
orderNumber: 'ORD-12345',
items: [
{ name: 'Product A', price: 29.99 },
{ name: 'Product B', price: 49.99 },
],
total: 79.98,
},
});Manage Subscribers
manage-subscribers.ts
// Add a subscriber
const subscriber = await client.audiences.subscribers('audience_xxxxx').add({
email: 'user@example.com',
firstName: 'John',
lastName: 'Doe',
attributes: {
plan: 'premium',
},
});
// List active subscribers
const { data, hasMore } = await client.audiences.subscribers('audience_xxxxx').list({
status: 'active',
limit: 100,
});
// Remove a subscriber
await client.audiences.subscribers('audience_xxxxx').remove(subscriber.id);Create and Send Campaign
campaign.ts
// Create a campaign
const campaign = await client.campaigns.create({
name: 'Monthly Newsletter',
subject: 'Your December Update',
from: 'news@yourdomain.com',
audienceId: 'audience_xxxxx',
templateId: 'template_xxxxx',
variables: { month: 'December' },
});
// Schedule for later
await client.campaigns.schedule(campaign.id, {
scheduledAt: '2024-12-01T09:00:00Z',
});
// Or send immediately
await client.campaigns.send(campaign.id);Requirements
- Node.js 18 or later (uses native fetch)
- ESM module support