npm package: @sentd/node
Description: Official Node.js SDK for the SENTD Email API
Author: SENTD
Latest version: 1.0.0

# @sentd/node

Official Node.js SDK for SENTD - the email infrastructure platform.

## Installation

```bash
npm install @sentd/node
# or
yarn add @sentd/node
# or
pnpm add @sentd/node
```

## Quick Start

```typescript
import { Sentd } from '@sentd/node';

const sentd = new Sentd('em_live_your_api_key');

// Send an email
const { data } = await sentd.emails.send({
    from: 'hello@yourdomain.com',
    to: 'user@example.com',
    subject: 'Welcome!',
    html: '<h1>Hello World</h1>',
});

console.log('Email sent:', data.id);
```

## Configuration

```typescript
const sentd = new Sentd({
    apiKey: 'em_live_your_api_key',
    baseUrl: 'https://api.sentd.io', // optional, for self-hosted
    timeout: 30000, // optional, request timeout in ms
});
```

## Emails

### Send an Email

```typescript
const { data } = await sentd.emails.send({
    from: 'hello@yourdomain.com',
    to: 'user@example.com',
    subject: 'Welcome to our platform',
    html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
    text: 'Welcome! Thanks for signing up.',

    // Optional
    cc: ['cc@example.com'],
    bcc: ['bcc@example.com'],
    replyTo: 'support@yourdomain.com',

    // Attachments
    attachments: [
        {
            filename: 'invoice.pdf',
            content: base64EncodedContent,
        },
    ],

    // Tracking options
    tracking: {
        opens: true,
        clicks: true,
    },

    // Routing preferences
    routing: {
        strategy: 'reliable', // 'reliable' | 'cheapest' | 'fastest'
        priority: 'high',     // 'critical' | 'high' | 'normal' | 'low' | 'bulk'
    },
});
```

### Schedule an Email

```typescript
const { data } = await sentd.emails.send({
    from: 'hello@yourdomain.com',
    to: 'user@example.com',
    subject: 'Reminder',
    html: '<p>Just a friendly reminder!</p>',
    sendAt: new Date('2024-12-25T09:00:00Z'),
    sendAtTimezone: 'America/New_York',
});
```

### List, Get, Cancel, Reschedule, Resend

```typescript
// List emails
const { data } = await sentd.emails.list({ limit: 20, status: 'delivered' });

// Get email details
const email = await sentd.emails.get('email_id');

// Cancel scheduled email
await sentd.emails.cancel('email_id');

// Reschedule email
await sentd.emails.reschedule('email_id', new Date('2024-12-26T09:00:00Z'));

// Resend failed email
const { data: newEmail } = await sentd.emails.resend('email_id');
```

## Batch Sending

```typescript
const { data } = await sentd.batch.send({
    from: 'hello@yourdomain.com',
    subject: 'Hello {{name}}!',
    html: '<p>Hi {{name}}, your order #{{orderId}} is confirmed.</p>',
    emails: [
        { to: 'alice@example.com', data: { name: 'Alice', orderId: '1001' } },
        { to: 'bob@example.com', data: { name: 'Bob', orderId: '1002' } },
    ],
});
```

## Templates

```typescript
// Create template
await sentd.templates.create({
    name: 'Welcome Email',
    subjectTemplate: 'Welcome, {{name}}!',
    htmlTemplate: '<h1>Welcome, {{name}}!</h1>',
});

// List templates
const { data } = await sentd.templates.list();

// Preview template
const preview = await sentd.templates.preview('id', { data: { name: 'John' } });
```

## Domains

```typescript
// Add domain
const { data } = await sentd.domains.add({ domain: 'mail.yourdomain.com' });
console.log(data.records); // DNS records to configure

// Verify domain
await sentd.domains.verify('domain_id');
```

## Webhooks

```typescript
// Create webhook
const { data } = await sentd.webhooks.create({
    url: 'https://yourapp.com/webhooks/email',
    events: ['email.delivered', 'email.bounced', 'email.opened'],
});
```

## Analytics

```typescript
const { data } = await sentd.analytics.get({ days: 30 });
console.log(data.summary); // { sent, delivered, opened, clicked, bounced }
```

## Error Handling

```typescript
import { SentdError, RateLimitError, ValidationError } from '@sentd/node';

try {
    await sentd.emails.send({ ... });
} catch (error) {
    if (error instanceof RateLimitError) {
        console.log('Rate limited');
    } else if (error instanceof ValidationError) {
        console.log('Validation error:', error.details);
    }
}
```

## License

MIT