Skip to main content

Send Email with Express

This guide shows you how to send transactional emails using Sequenzy in an Express application.

Prerequisites

1. Install

Install the Sequenzy SDK and Express:
npm install sequenzy express
For TypeScript support:
npm install -D @types/express typescript

2. Create Your Server

Create a basic Express server that sends emails:
index.ts
import express, { Request, Response } from "express";
import Sequenzy from "sequenzy";

const app = express();
app.use(express.json());

const client = new Sequenzy({
  apiKey: process.env.SEQUENZY_API_KEY!,
});

app.post("/send", async (req: Request, res: Response) => {
  const { email, firstName } = req.body;

  const { data, error } = await client.transactional.send({
    to: email,
    subject: "Welcome to Our Platform!",
    body: `<h1>Welcome, ${firstName}!</h1><p>Thanks for signing up.</p>`,
  });

  if (error) {
    return res.status(400).json({ error });
  }

  return res.status(200).json({ data });
});

app.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

3. Run

Start your server:
npx tsx index.ts
Send a test request:
curl -X POST http://localhost:3000/send \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "firstName": "John"}'

Using Templates

Instead of inline HTML, use pre-built templates from the Sequenzy dashboard:
app.post("/send-welcome", async (req: Request, res: Response) => {
  const { email, firstName } = req.body;

  const { data, error } = await client.transactional.send({
    to: email,
    slug: "welcome-email",
    variables: {
      firstName,
      dashboardUrl: "https://app.example.com/dashboard",
    },
  });

  if (error) {
    return res.status(400).json({ error });
  }

  return res.status(200).json({ data });
});

Using React Email

For complex email templates, use React Email with Express:
npm install @react-email/components @react-email/render
Create a template:
emails/welcome-email.tsx
import {
  Body,
  Button,
  Container,
  Head,
  Heading,
  Html,
  Preview,
  Text,
} from "@react-email/components";
import * as React from "react";

interface WelcomeEmailProps {
  firstName: string;
  dashboardUrl: string;
}

export function WelcomeEmail({ firstName, dashboardUrl }: WelcomeEmailProps) {
  return (
    <Html>
      <Head />
      <Preview>Welcome to our platform</Preview>
      <Body style={{ fontFamily: "sans-serif", padding: "20px" }}>
        <Container>
          <Heading>Welcome, {firstName}!</Heading>
          <Text>
            Thanks for signing up. Get started by visiting your dashboard:
          </Text>
          <Button
            href={dashboardUrl}
            style={{
              backgroundColor: "#c95a3c",
              color: "white",
              padding: "12px 24px",
              borderRadius: "6px",
            }}
          >
            Go to Dashboard
          </Button>
        </Container>
      </Body>
    </Html>
  );
}
Use it in your route:
import { render } from "@react-email/render";
import { WelcomeEmail } from "./emails/welcome-email";

app.post("/send-welcome", async (req: Request, res: Response) => {
  const { email, firstName } = req.body;

  const html = await render(
    WelcomeEmail({
      firstName,
      dashboardUrl: "https://app.example.com/dashboard",
    })
  );

  const { data, error } = await client.transactional.send({
    to: email,
    subject: "Welcome to Our Platform!",
    body: html,
  });

  if (error) {
    return res.status(400).json({ error });
  }

  return res.status(200).json({ data });
});

Error Handling

Add proper error handling for production:
app.post("/send", async (req: Request, res: Response) => {
  try {
    const { email, firstName } = req.body;

    if (!email || !firstName) {
      return res
        .status(400)
        .json({ error: "Email and firstName are required" });
    }

    const { data, error } = await client.transactional.send({
      to: email,
      slug: "welcome-email",
      variables: { firstName },
    });

    if (error) {
      console.error("Email sending failed:", error);
      return res.status(400).json({ error });
    }

    return res.status(200).json({ data });
  } catch (err) {
    console.error("Unexpected error:", err);
    return res.status(500).json({ error: "Internal server error" });
  }
});

Next Steps