Resend email sending with React-free templates, tested with MSW: working versions (tested 2026-10-15)
Send transactional email with the Resend Node SDK (HTML + text, tags, idempotency), handling API errors, tested offline by intercepting the Resend API with MSW.
passed2 passed · 0 failed2026-10-15 11:56 UTC- Status
- passed
- Last passed
- 2026-10-15 11:56 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
npm install --no-audit --no-fund --ignore-scripts=false npm test
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38730348195
The code
Exactly the files that passed. Your agent can pull them with get_recipe("resend-email-msw") (install), or see them on GitHub.
{
"name": "resend-email-msw",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"resend": "6.33.0"
},
"devDependencies": {
"tsx": "4.23.15",
"@types/node": "24.19.2",
"msw": "3.0.3"
}
}
import { Resend } from "resend";
export async function sendWelcome(resend: Resend, to: string, name: string) {
const safe = name.replace(/[<>&"]/g, "");
const { data, error } = await resend.emails.send({
from: "App <hello@example.com>",
to: [to],
subject: `Welcome, ${safe}!`,
html: `<h1>Welcome, ${safe}</h1><p>Thanks for signing up.</p>`,
text: `Welcome, ${safe}. Thanks for signing up.`,
tags: [{ name: "category", value: "welcome" }],
});
if (error) throw new Error(`resend: ${error.message}`); // the SDK returns errors instead of throwing
return data!.id;
}
import { test, before, after, afterEach } from "node:test";
import assert from "node:assert/strict";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { Resend } from "resend";
import { sendWelcome } from "../src/mailer.ts";
let lastBody: any;
const server = setupServer(http.post("https://api.resend.com/emails", async ({ request }) => {
lastBody = await request.json();
if (request.headers.get("authorization") !== "Bearer re_test") return HttpResponse.json({ statusCode: 401, name: "validation_error", message: "API key is invalid" }, { status: 401 });
return HttpResponse.json({ id: "email_123" });
}));
before(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => server.resetHandlers());
after(() => server.close());
test("sends html, text and tags", async () => {
assert.equal(await sendWelcome(new Resend("re_test"), "a@example.com", "Ada<script>"), "email_123");
assert.equal(lastBody.subject, "Welcome, Adascript!");
assert.deepEqual(lastBody.to, ["a@example.com"]);
assert.ok(lastBody.text && lastBody.html);
assert.deepEqual(lastBody.tags, [{ name: "category", value: "welcome" }]);
});
test("API errors are thrown with the message", async () => {
await assert.rejects(sendWelcome(new Resend("re_bad"), "a@example.com", "Ada"), /API key is invalid/);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.
Related stacks
graphql-request client with typed operations, mocked by MSW graphql handlers
Call a GraphQL API with graphql-request (variables, headers, error handling) and test offline with MSW's graphql.link(url).query/mutation handlers (msw 3 API). graphql stays on 16.x: graphql-request 7 peers on graphql 14 - 16.
ky HTTP client with hooks and retries, tested with MSW + Vitest
A ky 2 API client (prefix option, state-object hooks) with auth hooks, retry on 5xx and typed JSON, tested offline with MSW's Node server in Vitest.
Typed fetch API client tested with MSW + Zod
A typed fetch client that parses responses with Zod, tested offline by intercepting requests with MSW's Node server.