Introduction
Relevant source files
The following files were used as context for generating this wiki page:
The Resend Node.js SDK provides a library for interacting with the Resend API using JavaScript or TypeScript. It manages authentication, request serialization, and response handling for various Resend services including emails, audiences, and contact management.
This SDK requires Node.js version 20 or higher. It supports modern environments through both CommonJS and ESM exports.
Sources: package.json:1-42, readme.md:27-28
SDK Architecture
The SDK follows a modular design centered around the Resend class. This class acts as the main entry point and orchestrates communication with the Resend API. When you instantiate the Resend class, it initializes specialized resource modules that provide methods for specific API features.
Core Components
The following diagram illustrates the relationship between the main SDK class and its resource modules.
The diagram shows the Resend class as the owner of multiple resource-specific classes that encapsulate API logic.
Sources: src/resend.ts:40-75
Setup and Initialization
To use the SDK, you must provide an API key. You can pass this key directly to the constructor or set it via the RESEND_API_KEY environment variable.
- Import the
Resendclass. - Initialize the class with your API key.
- Access specific resources (e.g.,
emails,templates) via class properties.
import { Resend } from 'resend';
const resend = new Resend('re_123456789');
Sources: readme.md:42-45, src/resend.ts:77-94
Configuration Options
The Resend constructor accepts an optional configuration object to customize the SDK's behavior.
| Option | Type | Default | Description |
|---|---|---|---|
baseUrl | string | https://api.resend.com | The base URL for API requests. Can be overridden by RESEND_BASE_URL env var. |
userAgent | string | resend-node:[version] | The User-Agent header sent with requests. |
Sources: src/resend.ts:31-38, src/resend.ts:96-97
Request Lifecycle and Logic
The SDK uses the fetch API to communicate with Resend servers. The fetchRequest method in the src/resend.ts file manages the core request logic, including error handling and response parsing.
Request Flow
The following flow diagram describes how a request is processed from a resource call to the final response.
The diagram shows the internal validation and error handling logic within the fetchRequest method.
Sources: src/resend.ts:121-209
Advanced Request Options
Every SDK method accepts a RequestOptions object as the final argument. This allows for per-request configuration.
- Idempotency: Pass
idempotencyKeyto prevent duplicate operations on POST requests. - Cancellation: Pass an
AbortSignalto thesignalproperty to cancel requests or implement timeouts. - Custom Headers: Pass a
headersobject to include additional HTTP headers.
Sources: src/resend.ts:212-230, readme.md:120-130, src/batch/batch.spec.ts:98-124
Core Modules Overview
The SDK exposes several modules to manage different aspects of the Resend platform.
| Module | Purpose | Key Operations |
|---|---|---|
emails | Send and manage emails | send(), get(), list() |
templates | Manage reusable email templates | create(), publish(), duplicate() |
apiKeys | Manage access tokens | create(), remove(), list() |
segments | Manage contact lists (Audiences) | create(), list(), remove() |
inboxes | Manage support inboxes and threads | create(), list(), threads.list() |
batch | Send multiple emails at once | send(), create() |
Sources: src/resend.ts:47-75, src/request-options.spec.ts:13-149
Conclusion
The Resend Node.js SDK provides a structured, type-safe interface for the Resend API. It abstracts complex networking details such as authentication headers, serialization of React components into HTML, and standardized error reporting, allowing developers to focus on application logic.
Sources: readme.md:47-118, src/resend.ts:121-125