Interactive Lab

Resend Transactional Email & Polar.sh Monetization

Accept global developer payments with Polar.sh Merchant of Record and trigger reliable transactional emails via Resend.

billing-and-email.ts
import { Resend } from 'resend';
import { Polar } from '@polar-sh/sdk';

// 1. Resend Transactional Email Dispatcher
const resend = new Resend(process.env.RESEND_API_KEY);

export async function sendWelcomeEmail(toEmail: string, name: string) {
  return await resend.emails.send({
    from: 'Web Engine <billing@modern-web.org>',
    to: [toEmail],
    subject: 'Your Enterprise Workspace is Provisioned',
    html: `<h1>Welcome, ${name}</h1><p>Your subscription is active.</p>`
  });
}

// 2. Polar.sh Merchant of Record (MoR) Webhook Receiver
const polar = new Polar({ accessToken: process.env.POLAR_ACCESS_TOKEN });

export async function handlePolarWebhook(event: { type: string; data: Record<string, unknown> }) {
  switch (event.type) {
    case 'subscription.created':
    case 'subscription.updated': {
      const { customer, price, status } = event.data as { customer: { email: string; name: string }; price: { amount: number }; status: string };
      if (status === 'active') {
        await sendWelcomeEmail(customer.email, customer.name);
      }
      break;
    }
    case 'subscription.canceled':
      // Downgrade organization access in database
      break;
  }
}

Merchant of Record (Polar.sh) vs. Raw Gateways

  • Merchant of Record: Polar.sh calculates, collects, and remits global sales tax and VAT automatically, removing compliance liability from developers.
  • Modern Email Infrastructure (Resend): Operates over HTTP REST APIs with native React/HTML templates, custom domain DKIM authentication, and webhook delivery tracking.
Polar.sh Checkout & Resend Pipeline MoR + Email Lifecycle
Dispatched Webhook & Email Events:
Click "Simulate" to execute the subscription pipeline.