Skip to Content
Docs
Frameworks

Express & Node.js

Send transactional emails and handle webhooks in Node.js and Express using the official @emitd/sdk or native fetch.

Integrating emitd with Node.js and Express takes less than five minutes. You can use the official @emitd/sdk for full TypeScript autocomplete or plain fetch.

1. Install the SDK

npm install @emitd/sdk

Or with pnpm / yarn:

pnpm add @emitd/sdk

2. Configure your environment

Add your API key to your .env file:

EMITD_API_KEY=esk_live_...

3. Send email from an Express route

server.ts
import express from "express";
import { Emitd } from "@emitd/sdk";

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

const emitd = new Emitd({ apiKey: process.env.EMITD_API_KEY! });

app.post("/api/signup", async (req, res) => {
  const { email, name } = req.body;

  try {
    const result = await emitd.email.send({
      from: "hello@yourdomain.com",
      to: [email],
      subject: `Welcome to our platform, ${name}!`,
      html_body: `
        <h1>Welcome, ${name}!</h1>
        <p>Thanks for joining. Get started with your new account today.</p>
      `,
      tags: { flow: "onboarding" },
      message_stream: "outbound",
    });

    res.status(200).json({ success: true, messageId: result.message_id });
  } catch (err: any) {
    res.status(500).json({ error: err.message });
  }
});

app.listen(3000, () => console.log("Server listening on port 3000"));

4. Receive signed webhooks

To track email delivery status, bounces, and spam complaints, register a webhook route that verifies the HMAC-SHA256 signature:

webhooks.ts
import express from "express";
import crypto from "crypto";

const app = express();

// Use express.raw() to capture the exact unmodified payload for HMAC verification
app.post(
  "/webhooks/emitd",
  express.raw({ type: "application/json" }),
  (req, res) => {
    const signature = req.headers["emitd-signature"] as string;
    const secret = process.env.EMITD_WEBHOOK_SECRET!;

    const expected = crypto
      .createHmac("sha256", secret)
      .update(req.body)
      .digest("hex");

    if (signature !== expected) {
      return res.status(401).send("Invalid webhook signature");
    }

    const event = JSON.parse(req.body.toString());
    console.log(`Received event: ${event.type} for message ${event.data.message_id}`);

    if (event.type === "email.bounced") {
      // Mark user address as bounced in your database
    }

    res.status(200).send("OK");
  }
);

What's next?

On this page