@unsent/sdk 0.25.3 → 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 +183 -103
- package/dist/index.d.mts +350 -87
- package/dist/index.d.ts +350 -87
- package/dist/index.js +51 -7
- package/dist/index.mjs +50 -7
- package/package.json +24 -5
package/README.md
CHANGED
|
@@ -36,9 +36,9 @@ bun add @unsent/sdk
|
|
|
36
36
|
### Basic Setup
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
|
-
import { unsent } from "@unsent/sdk"
|
|
39
|
+
import { unsent } from "@unsent/sdk";
|
|
40
40
|
|
|
41
|
-
const client = new unsent("us_12345")
|
|
41
|
+
const client = new unsent("us_12345");
|
|
42
42
|
```
|
|
43
43
|
|
|
44
44
|
### Environment Variables
|
|
@@ -48,7 +48,7 @@ You can also set your API key using environment variables:
|
|
|
48
48
|
```typescript
|
|
49
49
|
// Set UNSENT_API_KEY or UNSENT_API_KEY in your environment
|
|
50
50
|
// Then initialize without passing the key
|
|
51
|
-
const client = new unsent()
|
|
51
|
+
const client = new unsent();
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
### Sending Emails
|
|
@@ -57,17 +57,17 @@ const client = new unsent()
|
|
|
57
57
|
|
|
58
58
|
```typescript
|
|
59
59
|
const { data, error } = await client.emails.send({
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
60
|
+
to: "hello@acme.com",
|
|
61
|
+
from: "hello@company.com",
|
|
62
|
+
subject: "unsent email",
|
|
63
|
+
html: "<p>unsent is the best open source product to send emails</p>",
|
|
64
|
+
text: "unsent is the best open source product to send emails",
|
|
65
|
+
});
|
|
66
66
|
|
|
67
67
|
if (error) {
|
|
68
|
-
|
|
68
|
+
console.error("Error:", error);
|
|
69
69
|
} else {
|
|
70
|
-
|
|
70
|
+
console.log("Email sent! ID:", data.id);
|
|
71
71
|
}
|
|
72
72
|
```
|
|
73
73
|
|
|
@@ -75,17 +75,17 @@ if (error) {
|
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
77
|
const { data, error } = await client.emails.send({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
})
|
|
78
|
+
to: "hello@acme.com",
|
|
79
|
+
from: "hello@company.com",
|
|
80
|
+
subject: "Email with attachment",
|
|
81
|
+
html: "<p>Please find the attachment below</p>",
|
|
82
|
+
attachments: [
|
|
83
|
+
{
|
|
84
|
+
filename: "document.pdf",
|
|
85
|
+
content: "base64-encoded-content-here",
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
#### Email with React Component
|
|
@@ -104,42 +104,42 @@ const { data, error } = await client.emails.send({
|
|
|
104
104
|
#### Scheduled Email
|
|
105
105
|
|
|
106
106
|
```typescript
|
|
107
|
-
const scheduledTime = new Date()
|
|
108
|
-
scheduledTime.setHours(scheduledTime.getHours() + 1) // Schedule for 1 hour from now
|
|
107
|
+
const scheduledTime = new Date();
|
|
108
|
+
scheduledTime.setHours(scheduledTime.getHours() + 1); // Schedule for 1 hour from now
|
|
109
109
|
|
|
110
110
|
const { data, error } = await client.emails.send({
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
})
|
|
111
|
+
to: "hello@acme.com",
|
|
112
|
+
from: "hello@company.com",
|
|
113
|
+
subject: "Scheduled email",
|
|
114
|
+
html: "<p>This email was scheduled</p>",
|
|
115
|
+
scheduledAt: scheduledTime.toISOString(),
|
|
116
|
+
});
|
|
117
117
|
```
|
|
118
118
|
|
|
119
119
|
#### Batch Emails
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
122
|
const emails = [
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
]
|
|
136
|
-
|
|
137
|
-
const { data, error } = await client.emails.batch(emails)
|
|
123
|
+
{
|
|
124
|
+
to: "user1@example.com",
|
|
125
|
+
from: "hello@company.com",
|
|
126
|
+
subject: "Hello User 1",
|
|
127
|
+
html: "<p>Welcome User 1</p>",
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
to: "user2@example.com",
|
|
131
|
+
from: "hello@company.com",
|
|
132
|
+
subject: "Hello User 2",
|
|
133
|
+
html: "<p>Welcome User 2</p>",
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
const { data, error } = await client.emails.batch(emails);
|
|
138
138
|
|
|
139
139
|
if (error) {
|
|
140
|
-
|
|
140
|
+
console.error("Error:", error);
|
|
141
141
|
} else {
|
|
142
|
-
|
|
142
|
+
console.log(`Sent ${data.length} emails`);
|
|
143
143
|
}
|
|
144
144
|
```
|
|
145
145
|
|
|
@@ -148,12 +148,12 @@ if (error) {
|
|
|
148
148
|
#### Get Email Details
|
|
149
149
|
|
|
150
150
|
```typescript
|
|
151
|
-
const { data, error } = await client.emails.get("email_id")
|
|
151
|
+
const { data, error } = await client.emails.get("email_id");
|
|
152
152
|
|
|
153
153
|
if (error) {
|
|
154
|
-
|
|
154
|
+
console.error("Error:", error);
|
|
155
155
|
} else {
|
|
156
|
-
|
|
156
|
+
console.log("Email status:", data.status);
|
|
157
157
|
}
|
|
158
158
|
```
|
|
159
159
|
|
|
@@ -161,20 +161,20 @@ if (error) {
|
|
|
161
161
|
|
|
162
162
|
```typescript
|
|
163
163
|
const { data, error } = await client.emails.update("email_id", {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
})
|
|
164
|
+
subject: "Updated subject",
|
|
165
|
+
html: "<p>Updated content</p>",
|
|
166
|
+
});
|
|
167
167
|
```
|
|
168
168
|
|
|
169
169
|
#### Cancel Scheduled Email
|
|
170
170
|
|
|
171
171
|
```typescript
|
|
172
|
-
const { data, error } = await client.emails.cancel("email_id")
|
|
172
|
+
const { data, error } = await client.emails.cancel("email_id");
|
|
173
173
|
|
|
174
174
|
if (error) {
|
|
175
|
-
|
|
175
|
+
console.error("Error:", error);
|
|
176
176
|
} else {
|
|
177
|
-
|
|
177
|
+
console.log("Email cancelled successfully");
|
|
178
178
|
}
|
|
179
179
|
```
|
|
180
180
|
|
|
@@ -184,38 +184,38 @@ if (error) {
|
|
|
184
184
|
|
|
185
185
|
```typescript
|
|
186
186
|
const { data, error } = await client.contacts.create("contact_book_id", {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
})
|
|
187
|
+
email: "user@example.com",
|
|
188
|
+
firstName: "John",
|
|
189
|
+
lastName: "Doe",
|
|
190
|
+
metadata: {
|
|
191
|
+
company: "Acme Inc",
|
|
192
|
+
role: "Developer",
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
195
|
```
|
|
196
196
|
|
|
197
197
|
#### Get Contact
|
|
198
198
|
|
|
199
199
|
```typescript
|
|
200
200
|
const { data, error } = await client.contacts.get(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
)
|
|
201
|
+
"contact_book_id",
|
|
202
|
+
"contact_id",
|
|
203
|
+
);
|
|
204
204
|
```
|
|
205
205
|
|
|
206
206
|
#### Update Contact
|
|
207
207
|
|
|
208
208
|
```typescript
|
|
209
209
|
const { data, error } = await client.contacts.update(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
)
|
|
210
|
+
"contact_book_id",
|
|
211
|
+
"contact_id",
|
|
212
|
+
{
|
|
213
|
+
firstName: "Jane",
|
|
214
|
+
metadata: {
|
|
215
|
+
role: "Senior Developer",
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
219
|
```
|
|
220
220
|
|
|
221
221
|
#### Upsert Contact
|
|
@@ -223,23 +223,89 @@ const { data, error } = await client.contacts.update(
|
|
|
223
223
|
```typescript
|
|
224
224
|
// Creates if doesn't exist, updates if exists
|
|
225
225
|
const { data, error } = await client.contacts.upsert(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
)
|
|
226
|
+
"contact_book_id",
|
|
227
|
+
"contact_id",
|
|
228
|
+
{
|
|
229
|
+
email: "user@example.com",
|
|
230
|
+
firstName: "John",
|
|
231
|
+
lastName: "Doe",
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
234
|
```
|
|
235
235
|
|
|
236
236
|
#### Delete Contact
|
|
237
237
|
|
|
238
238
|
```typescript
|
|
239
239
|
const { data, error } = await client.contacts.delete(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
)
|
|
240
|
+
"contact_book_id",
|
|
241
|
+
"contact_id",
|
|
242
|
+
);
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Managing Campaigns
|
|
246
|
+
|
|
247
|
+
Create and manage email campaigns:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { unsent } from "@unsent/sdk";
|
|
251
|
+
|
|
252
|
+
const client = new unsent("us_12345");
|
|
253
|
+
|
|
254
|
+
// Create a campaign
|
|
255
|
+
const campaign = await client.campaigns.create({
|
|
256
|
+
name: "Welcome Series",
|
|
257
|
+
from: "hello@company.com",
|
|
258
|
+
subject: "Welcome to our platform!",
|
|
259
|
+
contactBookId: "cb_12345",
|
|
260
|
+
html: "<h1>Welcome!</h1><p>Thanks for joining us.</p>",
|
|
261
|
+
sendNow: false,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
if (campaign.error) {
|
|
265
|
+
console.error("Error creating campaign:", campaign.error);
|
|
266
|
+
} else {
|
|
267
|
+
console.log("Campaign created:", campaign.data.id);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Schedule a campaign
|
|
271
|
+
const scheduleResult = await client.campaigns.schedule(campaign.data.id, {
|
|
272
|
+
scheduledAt: "2024-12-01T09:00:00Z",
|
|
273
|
+
batchSize: 1000,
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
if (scheduleResult.error) {
|
|
277
|
+
console.error("Error scheduling campaign:", scheduleResult.error);
|
|
278
|
+
} else {
|
|
279
|
+
console.log("Campaign scheduled successfully");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Get campaign details
|
|
283
|
+
const details = await client.campaigns.get(campaign.data.id);
|
|
284
|
+
|
|
285
|
+
if (details.error) {
|
|
286
|
+
console.error("Error getting details:", details.error);
|
|
287
|
+
} else {
|
|
288
|
+
console.log("Campaign status:", details.data.status);
|
|
289
|
+
console.log("Total recipients:", details.data.total);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Pause a campaign
|
|
293
|
+
const pauseResult = await client.campaigns.pause(campaign.data.id);
|
|
294
|
+
|
|
295
|
+
if (pauseResult.error) {
|
|
296
|
+
console.error("Error pausing campaign:", pauseResult.error);
|
|
297
|
+
} else {
|
|
298
|
+
console.log("Campaign paused successfully");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Resume a campaign
|
|
302
|
+
const resumeResult = await client.campaigns.resume(campaign.data.id);
|
|
303
|
+
|
|
304
|
+
if (resumeResult.error) {
|
|
305
|
+
console.error("Error resuming campaign:", resumeResult.error);
|
|
306
|
+
} else {
|
|
307
|
+
console.log("Campaign resumed successfully");
|
|
308
|
+
}
|
|
243
309
|
```
|
|
244
310
|
|
|
245
311
|
### Error Handling
|
|
@@ -248,19 +314,19 @@ The SDK returns a consistent response structure with `data` and `error`:
|
|
|
248
314
|
|
|
249
315
|
```typescript
|
|
250
316
|
const { data, error } = await client.emails.send({
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
})
|
|
317
|
+
to: "invalid-email",
|
|
318
|
+
from: "hello@company.com",
|
|
319
|
+
subject: "Test",
|
|
320
|
+
html: "<p>Test</p>",
|
|
321
|
+
});
|
|
256
322
|
|
|
257
323
|
if (error) {
|
|
258
|
-
|
|
259
|
-
|
|
324
|
+
console.error(`Error ${error.code}: ${error.message}`);
|
|
325
|
+
return;
|
|
260
326
|
}
|
|
261
327
|
|
|
262
328
|
// Safe to use data here
|
|
263
|
-
console.log("Email sent:", data.id)
|
|
329
|
+
console.log("Email sent:", data.id);
|
|
264
330
|
```
|
|
265
331
|
|
|
266
332
|
### TypeScript Support
|
|
@@ -268,21 +334,21 @@ console.log("Email sent:", data.id)
|
|
|
268
334
|
The SDK is fully typed with TypeScript:
|
|
269
335
|
|
|
270
336
|
```typescript
|
|
271
|
-
import { unsent } from "@unsent/sdk"
|
|
337
|
+
import { unsent } from "@unsent/sdk";
|
|
272
338
|
|
|
273
|
-
const client = new unsent("us_12345")
|
|
339
|
+
const client = new unsent("us_12345");
|
|
274
340
|
|
|
275
341
|
// Full type inference and autocomplete
|
|
276
342
|
const result = await client.emails.send({
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
})
|
|
343
|
+
to: "hello@acme.com",
|
|
344
|
+
from: "hello@company.com",
|
|
345
|
+
subject: "Typed email",
|
|
346
|
+
html: "<p>Fully typed!</p>",
|
|
347
|
+
});
|
|
282
348
|
|
|
283
349
|
// Type-safe access
|
|
284
350
|
if (result.data) {
|
|
285
|
-
|
|
351
|
+
const emailId: string = result.data.id;
|
|
286
352
|
}
|
|
287
353
|
```
|
|
288
354
|
|
|
@@ -309,6 +375,14 @@ if (result.data) {
|
|
|
309
375
|
- `client.contacts.upsert(bookId, contactId, payload)` - Upsert a contact
|
|
310
376
|
- `client.contacts.delete(bookId, contactId)` - Delete a contact
|
|
311
377
|
|
|
378
|
+
### Campaign Methods
|
|
379
|
+
|
|
380
|
+
- `client.campaigns.create(payload)` - Create a campaign
|
|
381
|
+
- `client.campaigns.get(campaignId)` - Get campaign details
|
|
382
|
+
- `client.campaigns.schedule(campaignId, payload)` - Schedule a campaign
|
|
383
|
+
- `client.campaigns.pause(campaignId)` - Pause a campaign
|
|
384
|
+
- `client.campaigns.resume(campaignId)` - Resume a campaign
|
|
385
|
+
|
|
312
386
|
## Features
|
|
313
387
|
|
|
314
388
|
- 🔐 **Type-safe** - Full TypeScript support with type inference
|
|
@@ -326,3 +400,9 @@ if (result.data) {
|
|
|
326
400
|
## License
|
|
327
401
|
|
|
328
402
|
MIT
|
|
403
|
+
|
|
404
|
+
## Support
|
|
405
|
+
|
|
406
|
+
- [Documentation](https://docs.unsent.dev)
|
|
407
|
+
- [GitHub Issues](https://github.com/souravsspace/unsent-typescript/issues)
|
|
408
|
+
- [Discord Community](https://discord.gg/unsent)
|