Install the Resend Node.js SDK
canaryInstall the resend package using npm or yarn to start using the Resend API in your Node.js project.
npm install resend
# or
yarn add resendrepository·canary·Indexed 21 days ago
https://github.com/resend/resend-nodeA Node.js library for the Resend API that provides a programmatic interface to send emails via text, HTML, or React components. The SDK includes support for batch email sending, broadcast management, contact imports, and API key management, with built-in TypeScript interfaces and cursor-based pagination.
Install the resend package using npm or yarn to start using the Resend API in your Node.js project.
npm install resend
# or
yarn add resendTo use the SDK, import the Resend class and instantiate it with your API key obtained from the Resend Dashboard.
import { Resend } from 'resend';
const resend = new Resend('re_xxxx...xxxxxx');The audienceId field in LegacyCreateContactOptions is deprecated. To add a contact to one or more segments, use the segments array in the CreateContactOptions interface instead.
For migration details, refer to the Resend documentation: https://resend.com/docs/dashboard/segments/migrating-from-audiences-to-segments
resend/resend-node SDK provides a suite of interfaces for managing domains. These interfaces define the shapes of requests and responses for domain-related operations, including creating, retrieving, listing, removing, updating, and verifying domains.You can use React components as email templates by passing the rendered component to the react property in resend.emails.send().
Note: If you are in a file without JSX transpilation (like a standard .js or .ts file), use the jsx runtime function from react/jsx-runtime to pass the template.
// 1. Define your template
import React from 'react';
export default function EmailTemplate({ firstName, product }) {
return (
<div>
<h1>Welcome, {firstName}!</h1>
<p>Thanks for trying {product}. We’re thrilled to have you on board.</p>
</div>
);
}
// 2. Send using JSX (in .jsx/.tsx files)
import EmailTemplate from '../components/EmailTemplate';
const { data } = await resend.emails.send({
from: 'you@example.com',
to: 'user@gmail.com',
replyTo: 'you@example.com',
subject: 'hello world',
react: <EmailTemplate firstName="John" product="MyApp" />,
});
// 3. Send using jsx runtime (in .js/.ts files)
import { jsx } from 'react/jsx-runtime';
import EmailTemplate from '../components/EmailTemplate';
await resend.emails.send({
from: 'you@example.com',
to: 'user@gmail.com',
replyTo: 'you@example.com',
subject: 'hello world',
react: jsx(EmailTemplate, { firstName: "John", product: "MyApp" }),
});Use resend.emails.send() to send a simple text-based email. You must provide from, to, subject, and text fields. You can optionally include replyTo.
const { data } = await resend.emails.send({
from: 'you@example.com',
to: 'user@gmail.com',
replyTo: 'you@example.com',
subject: 'hello world',
text: 'it works!',
});
console.log(`Email ${data.id} has been sent`);To send an email with custom HTML formatting, use the html property instead of text in the resend.emails.send() method.
const { data } = await resend.emails.send({
from: 'you@example.com',
to: 'user@gmail.com',
replyTo: 'you@example.com',
subject: 'hello world',
html: '<strong>it works!</strong>',
});
console.log(`Email ${data.id} with custom HTML content has been sent.`);When retrieving received emails, you can control how inline images are represented within the html body using the html_format option in GetReceivingEmailOptions.
'data_uri' (default): Inline images are embedded directly into the html string as base64 data: URIs.'cid': The html string retains the original <img src="cid:..." /> references. In this mode, the src value will match the content_id field of an entry in the attachments array.// Example usage of GetReceivingEmailOptions
const options: GetReceivingEmailOptions = {
html_format: 'cid'
};src/templates/interfaces/index.ts entrypoint exports the type definitions and interfaces required to interact with Resend Templates. These interfaces define the shape of request and response objects for various template management operations, including creating, duplicating, retrieving, listing, publishing, removing, and updating templates.When updating an existing contact, use the UpdateContactOptions object. This object allows you to modify specific fields such as firstName, lastName, and unsubscribed status. You can also update custom metadata via the properties object.
To clear a field like firstName or lastName, pass null as the value. The properties object supports arbitrary key-value pairs where both the key and value are strings or numbers, or null to clear a property.
// Example of updating contact properties
const options: UpdateContactOptions = {
firstName: 'Jane',
lastName: null, // Clears the last name
unsubscribed: false,
properties: {
plan: 'premium',
login_count: 42,
custom_tag: null // Clears a custom property
}
};The src/contacts/segments/interfaces/index.ts module exports the core TypeScript interfaces used for managing contact segments in the Resend Node.js SDK. These interfaces define the shape of data required for the following operations:
AddContactSegmentListContactSegmentsRemoveContactSegmentContactSegmentsUse these interfaces to ensure type safety when calling methods related to contact segmentation.