Resend Node.js SDK

repository·canary·Indexed 21 days ago

https://github.com/resend/resend-node

A 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.

Tokens
8.3K
Snippets
26
Records
56
Agent score
76%

What's inside resend

  1. Send an email using React templates

    canary

    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" }),
    });
  2. Send a basic text email

    canary

    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`);
  3. Send an email using HTML

    canary

    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.`);
  4. Configure HTML format for receiving emails

    canary

    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'
    };
  5. Manage Resend Templates via Interface Definitions

    canary
    The 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.
  6. Configure options for updating a contact

    canary

    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
      }
    };
  7. Define Contact Segment interfaces

    canary

    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:

    • Adding a contact segment: AddContactSegment
    • Listing contact segments: ListContactSegments
    • Removing a contact segment: RemoveContactSegment
    • General segment properties: ContactSegments

    Use these interfaces to ensure type safety when calling methods related to contact segmentation.