The official TypeScript SDK for the Unsent API. Send transactional emails, manage contacts, campaigns, and more with ease.
npm install @unsent/sdkyarn add @unsent/sdkpnpm add @unsent/sdkbun add @unsent/sdkimport { unsent } from "@unsent/sdk";
const client = new unsent("un_xxxx");You can also set your API key using environment variables:
// Set UNSENT_API_KEY in your environment
// Then initialize without passing the key
const client = new unsent();const { data, error } = await client.emails.send({
to: "hello@acme.com",
from: "hello@company.com",
subject: "unsent email",
html: "<p>unsent is the best email service provider</p>",
text: "unsent is the best email service provider",
});
if (error) {
console.error("Error:", error);
} else {
console.log("Email sent! ID:", data.id);
}const { data, error } = await client.emails.get("email_id");const emails = [
{ to: "user1@example.com", from: "hello@company.com", subject: "Hi", html: "<p>Hi</p>" },
{ to: "user2@example.com", from: "hello@company.com", subject: "Hello", html: "<p>Hello</p>" }
];
const { data, error } = await client.emails.batch(emails);const { data, error } = await client.emails.send({
to: "hello@acme.com",
from: "hello@company.com",
subject: "Scheduled email",
html: "<p>This is scheduled</p>",
scheduledAt: "2024-12-25T10:00:00Z",
});const { data, error } = await client.emails.cancel("email_id");const { data, error } = await client.contacts.create("contact_book_id", {
email: "user@example.com",
firstName: "John",
lastName: "Doe",
metadata: { role: "Admin" }
});const { data, error } = await client.contacts.list("contact_book_id", {
page: 1,
limit: 20,
emails: "user@example.com",
ids: "id1,id2"
});const { data, error } = await client.contacts.get("contact_book_id", "contact_id");const { data, error } = await client.contacts.update("contact_book_id", "contact_id", {
firstName: "Jane"
});const { data, error } = await client.contacts.upsert("contact_book_id", "contact_id", {
email: "user@example.com",
firstName: "John"
});const { data, error } = await client.contacts.delete("contact_book_id", "contact_id");const { data, error } = await client.contactBooks.list();const { data, error } = await client.contactBooks.create({
name: "Newsletter Subscribers",
emoji: "📧"
});const { data, error } = await client.contactBooks.get("book_id");const { data, error } = await client.contactBooks.update("book_id", {
name: "New Name"
});const { data, error } = await client.contactBooks.delete("book_id");const { data, error } = await client.campaigns.create({
name: "Monthly Newsletter",
from: "news@company.com",
subject: "January Updates",
contactBookId: "book_id",
html: "<h1>News</h1>",
sendNow: false // set to true to send immediately
});const { data, error } = await client.campaigns.get("campaign_id");const { data, error } = await client.campaigns.schedule("campaign_id", {
scheduledAt: "2025-01-01T09:00:00Z",
batchSize: 500
});await client.campaigns.pause("campaign_id");
await client.campaigns.resume("campaign_id");const { data, error } = await client.templates.list();const { data, error } = await client.templates.create({
name: "Welcome Email",
subject: "Welcome {{name}}",
html: "<h1>Welcome!</h1>"
});const { data, error } = await client.templates.get("template_id");const { data, error } = await client.templates.update("template_id", {
subject: "New Subject"
});const { data, error } = await client.templates.delete("template_id");Note: Webhooks are currently in development. The following methods are placeholders for future implementation.
const { data, error } = await client.webhooks.list();const { data, error } = await client.webhooks.create({
url: "https://api.myapp.com/webhooks/unsent",
events: ["email.sent", "email.opened"]
});const { data, error } = await client.webhooks.update("webhook_id", {
events: ["email.bounced"]
});const { data, error } = await client.webhooks.delete("webhook_id");const { data, error } = await client.domains.list();const { data, error } = await client.domains.create({
domain: "mail.example.com"
});const { data, error } = await client.domains.verify("123"); // Uses domain ID (string)const { data, error } = await client.domains.get("123");const { data, error } = await client.domains.delete("123");const { data, error } = await client.analytics.get();const { data, error } = await client.analytics.getTimeSeries({
days: 30,
domain: "example.com"
});const { data, error } = await client.analytics.getReputation({
domain: "example.com"
});const { data, error } = await client.suppressions.list({
page: 1,
limit: 10,
search: "test",
reason: "COMPLAINT"
});const { data, error } = await client.suppressions.add({
email: "bad@example.com",
reason: "COMPLAINT"
});const { data, error } = await client.suppressions.delete("bad@example.com");const { data, error } = await client.apiKeys.list();const { data, error } = await client.apiKeys.create({
name: "Prod Key",
permission: "FULL"
});const { data, error } = await client.apiKeys.delete("key_id");const { data, error } = await client.settings.get();The SDK returns a consistent response structure with data and error:
const { data, error } = await client.emails.send({ ... });
if (error) {
// error: { code: string; message: string; }
console.error(`Error ${error.code}: ${error.message}`);
return;
}
console.log("Success:", data);The SDK is fully typed. You can use inferred types:
import { unsent } from "@unsent/sdk";
const client = new unsent();
// Types are automatically inferred
const { data } = await client.domains.list();MIT