Skip to main content

Use this pre-built prompt with your AI agent to build with the Resend Chat SDK adapter.

CursorOpen in Cursor
The @resend/chat-sdk-adapter package is a Vercel Chat SDK adapter that turns email into a two-way communication channel via Resend. Receive inbound emails through webhooks, reply through the Resend API, and let the adapter handle threading automatically. The adapter is open source and available on GitHub.

Prerequisites

To get started, you’ll need to:

1. Install

npm install @resend/chat-sdk-adapter chat @chat-adapter/state-memory

2. Configure the adapter

Create a Resend adapter and pass it to the Chat SDK.
import { createResendAdapter } from '@resend/chat-sdk-adapter';
import { MemoryStateAdapter } from '@chat-adapter/state-memory';
import { Chat } from 'chat';

const resend = createResendAdapter({
  fromAddress: 'bot@yourdomain.com',
  fromName: 'My Bot',
});

const chat = new Chat({
  userName: 'email-bot',
  adapters: { resend },
  state: new MemoryStateAdapter(),
});
Set RESEND_API_KEY and RESEND_WEBHOOK_SECRET as environment variables. You can also pass apiKey and webhookSecret directly in the config — explicit values take precedence over env vars.

Configuration options

ParameterTypeRequiredDescription
fromAddressstringYesSender email address
fromNamestringNoDisplay name for the From header
apiKeystringNoResend API key. Falls back to RESEND_API_KEY env var
webhookSecretstringNoWebhook signing secret. Falls back to RESEND_WEBHOOK_SECRET env var

3. Handle inbound emails

Register handlers for incoming emails. onNewMention fires when a new thread starts, and onSubscribedMessage fires for follow-up emails in threads you’ve subscribed to.
chat.onNewMention(async (thread, message) => {
  console.log(`New email from ${message.author.userId}: ${message.text}`);
  await thread.subscribe();
  await thread.post(`Got your email: ${message.text}`);
});

chat.onSubscribedMessage(async (thread, message) => {
  await thread.post(`Reply: ${message.text}`);
});

4. Forward webhooks

Point your Resend webhooks to your server’s /webhook endpoint. The adapter verifies the signature and parses the payload.
if (req.method === 'POST' && req.url === '/webhook') {
  const result = await chat.webhooks.resend(request);
  res.writeHead(result.status);
  res.end();
}
The handler expects a Web Request object. If you’re using Node.js http, convert IncomingMessage to a Request first — see the basic example for a full working server. For webhook setup details, see Managing Webhooks and Verify Webhook Requests.

How threading works

The adapter resolves threads using standard Message-ID, In-Reply-To, and References email headers. Reply chains are automatically grouped into Chat SDK threads — no extra configuration needed.
Email is immutable. The following operations throw NotImplementedError: editMessage, deleteMessage, addReaction, removeReaction, and startTyping.

Features

Examples