@emailr/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +241 -0
- package/dist/index.d.mts +958 -0
- package/dist/index.d.ts +958 -0
- package/dist/index.js +700 -0
- package/dist/index.mjs +666 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# @emailr/sdk
|
|
2
|
+
|
|
3
|
+
Official Emailr API SDK for TypeScript/JavaScript.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @emailr/sdk
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @emailr/sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @emailr/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Emailr } from '@emailr/sdk';
|
|
19
|
+
|
|
20
|
+
const emailr = new Emailr({ apiKey: 'your-api-key' });
|
|
21
|
+
|
|
22
|
+
// Send an email
|
|
23
|
+
const result = await emailr.emails.send({
|
|
24
|
+
to: 'recipient@example.com',
|
|
25
|
+
from: 'sender@yourdomain.com',
|
|
26
|
+
subject: 'Hello!',
|
|
27
|
+
html: '<h1>Hello World</h1>',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.log(result.message_id);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Configuration
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const emailr = new Emailr({
|
|
37
|
+
apiKey: 'your-api-key',
|
|
38
|
+
baseUrl: 'https://api.emailr.dev', // optional
|
|
39
|
+
timeout: 30000, // optional, in milliseconds
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Resources
|
|
44
|
+
|
|
45
|
+
### Emails
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Send an email
|
|
49
|
+
const result = await emailr.emails.send({
|
|
50
|
+
to: 'recipient@example.com',
|
|
51
|
+
from: 'sender@yourdomain.com',
|
|
52
|
+
subject: 'Hello!',
|
|
53
|
+
html: '<h1>Hello World</h1>',
|
|
54
|
+
text: 'Hello World',
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Send with a template
|
|
58
|
+
const result = await emailr.emails.send({
|
|
59
|
+
to: 'recipient@example.com',
|
|
60
|
+
template_id: 'template-uuid',
|
|
61
|
+
template_data: { name: 'John', company: 'Acme' },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Get email by ID
|
|
65
|
+
const email = await emailr.emails.get('email-uuid');
|
|
66
|
+
|
|
67
|
+
// List emails
|
|
68
|
+
const emails = await emailr.emails.list({ page: 1, limit: 50 });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Contacts
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// Create a contact
|
|
75
|
+
const contact = await emailr.contacts.create({
|
|
76
|
+
email: 'contact@example.com',
|
|
77
|
+
first_name: 'John',
|
|
78
|
+
last_name: 'Doe',
|
|
79
|
+
metadata: { source: 'website' },
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// List contacts
|
|
83
|
+
const contacts = await emailr.contacts.list({ limit: 100 });
|
|
84
|
+
|
|
85
|
+
// Update a contact
|
|
86
|
+
const updated = await emailr.contacts.update('contact-uuid', {
|
|
87
|
+
first_name: 'Jane',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Delete a contact
|
|
91
|
+
await emailr.contacts.delete('contact-uuid');
|
|
92
|
+
|
|
93
|
+
// Bulk create contacts
|
|
94
|
+
const result = await emailr.contacts.bulkCreate({
|
|
95
|
+
contacts: [
|
|
96
|
+
{ email: 'user1@example.com', first_name: 'User 1' },
|
|
97
|
+
{ email: 'user2@example.com', first_name: 'User 2' },
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Templates
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Create a template
|
|
106
|
+
const template = await emailr.templates.create({
|
|
107
|
+
name: 'Welcome Email',
|
|
108
|
+
subject: 'Welcome to {{company}}',
|
|
109
|
+
html_content: '<h1>Welcome {{name}}</h1>',
|
|
110
|
+
variables: ['name', 'company'],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// List templates
|
|
114
|
+
const templates = await emailr.templates.list();
|
|
115
|
+
|
|
116
|
+
// Update a template
|
|
117
|
+
const updated = await emailr.templates.update('template-uuid', {
|
|
118
|
+
subject: 'New Subject',
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Delete a template
|
|
122
|
+
await emailr.templates.delete('template-uuid');
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Domains
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// Add a domain
|
|
129
|
+
const domain = await emailr.domains.add({ domain: 'example.com' });
|
|
130
|
+
|
|
131
|
+
// List domains
|
|
132
|
+
const domains = await emailr.domains.list();
|
|
133
|
+
|
|
134
|
+
// Verify domain
|
|
135
|
+
const status = await emailr.domains.verify('domain-uuid');
|
|
136
|
+
|
|
137
|
+
// Check DNS status
|
|
138
|
+
const dnsStatus = await emailr.domains.checkDns('domain-uuid');
|
|
139
|
+
|
|
140
|
+
// Delete a domain
|
|
141
|
+
await emailr.domains.delete('domain-uuid');
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Webhooks
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// Create a webhook
|
|
148
|
+
const webhook = await emailr.webhooks.create({
|
|
149
|
+
name: 'Email Events',
|
|
150
|
+
url: 'https://api.example.com/webhooks',
|
|
151
|
+
events: ['email.sent', 'email.delivered', 'email.bounced'],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// List webhooks
|
|
155
|
+
const webhooks = await emailr.webhooks.list();
|
|
156
|
+
|
|
157
|
+
// Enable/disable webhook
|
|
158
|
+
await emailr.webhooks.enable('webhook-uuid');
|
|
159
|
+
await emailr.webhooks.disable('webhook-uuid');
|
|
160
|
+
|
|
161
|
+
// Delete a webhook
|
|
162
|
+
await emailr.webhooks.delete('webhook-uuid');
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Broadcasts
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
// Create a broadcast
|
|
169
|
+
const broadcast = await emailr.broadcasts.create({
|
|
170
|
+
name: 'Newsletter - January',
|
|
171
|
+
subject: 'Your monthly update',
|
|
172
|
+
from_email: 'newsletter@example.com',
|
|
173
|
+
segment_id: 'segment-uuid',
|
|
174
|
+
html_content: '<h1>Newsletter</h1>',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Send a broadcast
|
|
178
|
+
const result = await emailr.broadcasts.send('broadcast-uuid');
|
|
179
|
+
|
|
180
|
+
// Schedule a broadcast
|
|
181
|
+
await emailr.broadcasts.schedule('broadcast-uuid', '2024-01-15T10:00:00Z');
|
|
182
|
+
|
|
183
|
+
// Cancel a scheduled broadcast
|
|
184
|
+
await emailr.broadcasts.cancel('broadcast-uuid');
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Segments
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
// Create a segment
|
|
191
|
+
const segment = await emailr.segments.create({
|
|
192
|
+
name: 'Active Users',
|
|
193
|
+
description: 'Users who opened an email in the last 30 days',
|
|
194
|
+
conditions: { opened_email: { within_days: 30 } },
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// List segments
|
|
198
|
+
const segments = await emailr.segments.list();
|
|
199
|
+
|
|
200
|
+
// Get contacts in a segment
|
|
201
|
+
const contacts = await emailr.segments.getContacts('segment-uuid');
|
|
202
|
+
|
|
203
|
+
// Get segment count
|
|
204
|
+
const count = await emailr.segments.getCount('segment-uuid');
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Error Handling
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { Emailr, EmailrError, ValidationError, RateLimitError } from '@emailr/sdk';
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
await emailr.emails.send({ to: 'invalid-email' });
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error instanceof ValidationError) {
|
|
216
|
+
console.error('Validation failed:', error.message, error.details);
|
|
217
|
+
} else if (error instanceof RateLimitError) {
|
|
218
|
+
console.error('Rate limited. Retry after:', error.retryAfter, 'seconds');
|
|
219
|
+
} else if (error instanceof EmailrError) {
|
|
220
|
+
console.error('API error:', error.message, error.statusCode, error.code);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## TypeScript Support
|
|
226
|
+
|
|
227
|
+
This SDK is written in TypeScript and provides full type definitions for all API requests and responses.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import type { SendEmailRequest, Email, Contact } from '@emailr/sdk';
|
|
231
|
+
|
|
232
|
+
const request: SendEmailRequest = {
|
|
233
|
+
to: 'recipient@example.com',
|
|
234
|
+
subject: 'Hello',
|
|
235
|
+
html: '<h1>Hello</h1>',
|
|
236
|
+
};
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## License
|
|
240
|
+
|
|
241
|
+
MIT
|