@cdv2/email
v0.1.3
Published
TypeScript SDK for the cd2 email API
Readme
@cdv2/email
TypeScript client for the cd2 sender API, modeled on Resend's SDK. Works in Node, Bun, and the browser — native fetch, zero deps, ESM-only.
📖 Full documentation: docs.cd2.app — send() params, the EmailStatus lifecycle, result handling, and sender-API rules.
Install
bun add @cdv2/emailQuick start
import { EmailClient } from "@cdv2/email";
const client = new EmailClient({
apiKey: "cd2-api-key", // sent as the X-API-Key header
baseUrl: "https://send.cd2.adpharm.digital", // client appends /v1/emails
});
const { data, error } = await client.send({
from: "Notifications <[email protected]>",
to: "[email protected]",
subject: "Welcome",
html: "<p>Hello</p>",
text: "Hello",
});
if (error) {
console.error(error.message);
} else {
console.log("Sent:", data.id); // store this id for delivery-status lookups
}Every method returns { data, error } and never throws — check error, then narrow. Retrieve a sent email and its delivery status with client.get(id).
Attach files with base64 content. Give an attachment a contentId to embed it in the HTML body instead of listing it as a download:
await client.send({
from: "Billing <[email protected]>",
to: "[email protected]",
subject: "Your invoice",
html: '<p>Invoice attached.</p><img src="cid:logo">',
attachments: [
{ filename: "invoice.pdf", content: pdfBase64, contentType: "application/pdf" },
{ filename: "logo.png", content: logoBase64, contentId: "logo" },
],
});Attachments travel inside the JSON request, so there's a 3MB cap on the decoded total per send. send() checks filenames, base64, and total size before it makes the request, so a bad attachment comes back as an error immediately rather than after uploading megabytes — the returned error.userFacing is true and error.message names the file and the problem. MAX_TOTAL_ATTACHMENT_BYTES and validateAttachments are exported if you'd rather check earlier still. Attachments aren't supported on scheduled sends yet.
See the full docs for everything else.
