Sending Emails using the Resend Node.js SDK
Let us install the official Node.js client package and trigger our first email dispatch.
1. Installation and Client Initialization
Install the Resend SDK package:
# Install the Resend Node.js SDK
npm install resendInitialize the client with your environment key variable:
// src/lib/resend.ts
import { Resend } from "resend";
const apiKey = process.env.RESEND_API_KEY;
if (!apiKey) {
throw new Error("Missing RESEND_API_KEY environment configuration");
}
// Export singleton instance
export const resend = new Resend(apiKey);2. Sending HTML and Text Emails
To dispatch a transactional message, call the .emails.send() method:
import { resend } from "./resend";
async function dispatchWelcomeMessage() {
try {
const { data, error } = await resend.emails.send({
from: "Welcome Team <welcome@mycompany.com>",
to: ["customer@gmail.com"],
subject: "Welcome to Our Platform",
text: "Hello! We are thrilled to welcome you to our platform.", // Fallback text representation
html: "<h1>Welcome!</h1><p>We are thrilled to welcome you to our platform.</p>",
});
if (error) {
console.error("Failed to deliver mail:", error.message);
return;
}
console.log("Email sent successfully! Message ID:", data?.id);
} catch (err: any) {
console.error("Server dispatch error:", err.message);
}
}3. Configuration Properties
When calling .send(), configure these parameters:
- from: The sender email address. Must use your verified domain.
- to: Array of recipient email strings.
- subject: String representing email subject.
- attachments: Array of attachment objects containing base64 data or server file paths.
Published on Last updated: