Resend Provider
Workers Auth integrates with Resend for sending magic link emails. Resend offers a generous free tier and a clean API, making it the recommended email provider.
1. Create a Resend account
Section titled “1. Create a Resend account”Sign up at resend.com and verify your sending domain.
2. Create an API key
Section titled “2. Create an API key”In the Resend dashboard, go to API Keys and create a new key.
3. Set the secret
Section titled “3. Set the secret”wrangler secret put RESEND_API_KEY4. Configure the provider
Section titled “4. Configure the provider”import { ResendProvider } from 'workers-auth/providers/resend';
ResendProvider({ apiKey: process.env.RESEND_API_KEY!, from: 'auth@yourdomain.com',});Configuration
Section titled “Configuration”| Option | Type | Description |
|---|---|---|
apiKey | string | Your Resend API key |
from | string | Sender email address (must be from a verified domain) |
Use with MagicLinkStrategy
Section titled “Use with MagicLinkStrategy”import { MagicLinkStrategy } from 'workers-auth/authn/magic-link';import { ResendProvider } from 'workers-auth/providers/resend';import { defaultTemplate } from 'workers-auth/templates/default';
MagicLinkStrategy({ provider: ResendProvider({ apiKey: process.env.RESEND_API_KEY!, from: 'auth@yourdomain.com', }), template: defaultTemplate,});EmailProvider interface
Section titled “EmailProvider interface”The Resend provider implements the EmailProvider interface:
interface EmailProvider { send(options: { to: string; subject: string; html: string; }): Promise<void>;}Writing a custom provider
Section titled “Writing a custom provider”To use a different email service (SendGrid, Mailgun, AWS SES, etc.), implement EmailProvider:
import type { EmailProvider } from 'workers-auth';
const sendgridProvider: EmailProvider = { async send({ to, subject, html }) { await fetch('https://api.sendgrid.com/v3/mail/send', { method: 'POST', headers: { Authorization: `Bearer ${SENDGRID_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ personalizations: [{ to: [{ email: to }] }], from: { email: 'auth@yourdomain.com' }, subject, content: [{ type: 'text/html', value: html }], }), }); },};Pass your custom provider to MagicLinkStrategy:
MagicLinkStrategy({ provider: sendgridProvider, template: defaultTemplate,});