> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sequenzy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Send Email with Express

> Send transactional emails using Sequenzy and Express

# Send Email with Express

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

## Prerequisites

* A Sequenzy account with an [API key](/authentication)
* A [verified sending domain](/guides/domain-verification)
* Node.js 18+ installed

## 1. Install

Install the Sequenzy SDK and Express:

<CodeGroup>
  ```bash npm theme={null}
  npm install sequenzy express
  ```

  ```bash pnpm theme={null}
  pnpm add sequenzy express
  ```

  ```bash bun theme={null}
  bun add sequenzy express
  ```
</CodeGroup>

For TypeScript support:

```bash theme={null}
npm install -D @types/express typescript
```

## 2. Create Your Server

Create a basic Express server that sends emails:

```ts index.ts theme={null}
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:

```bash theme={null}
npx tsx index.ts
```

Send a test request:

```bash theme={null}
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:

```ts theme={null}
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](https://react.email) with Express:

```bash theme={null}
npm install @react-email/components @react-email/render
```

Create a template:

```tsx emails/welcome-email.tsx theme={null}
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:

```ts theme={null}
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:

```ts theme={null}
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

<CardGroup cols={2}>
  <Card title="React Email Components" icon="react" href="https://react.email/docs/components/html">
    Explore all available React Email components
  </Card>

  <Card title="Transactional Emails" icon="envelope" href="/concepts/transactional-emails">
    Learn more about transactional emails
  </Card>
</CardGroup>
