Documentation
Sign inGet started

TypeScript · Express

Send an email from an Express 5 POST route with @messagebird/sdk. You need a Bird API key: create one in Developers → API keys (Send your first email walks through it), then export it as BIRD_API_KEY.

1. Install

Codebeispiel
npm install express @messagebird/sdk
# pnpm add express @messagebird/sdk
# yarn add express @messagebird/sdk
# bun add express @messagebird/sdk

2. Send

Create src/server.ts. Express 5 forwards rejections from async handlers to the error middleware, so no try/catch wrapper is needed:
Codebeispiel
import express from "express";
import { BirdClient } from "@messagebird/sdk";

const bird = new BirdClient({ apiKey: process.env.BIRD_API_KEY! });
const app = express();

app.post("/send", async (req, res) => {
  const msg = await bird.email.send({
    from: "onboarding@messagebird.dev",
    to: ["delivered@messagebird.dev"],
    subject: "Hello from Bird",
    html: "<p>My first Bird email.</p>",
  });

  console.log(msg.id, msg.status); // "em_…", "accepted"
  res.json({ id: msg.id, status: msg.status });
});

app.listen(3000);
onboarding@messagebird.dev is Bird's shared onboarding sender and delivered@messagebird.dev is a sandbox recipient that always delivers: no domain verification needed. The SDK infers the region from your key's bk_us1_ / bk_eu1_ prefix (no base URL to configure) and auto-generates an Idempotency-Key per call, so retried sends are safe.

3. Try it

Run the server and POST to the route:
Codebeispiel
npx tsx src/server.ts
curl -X POST http://localhost:3000/send
Bird accepts the email with a 202 and delivers it asynchronously; your route returns the em_ ID and status:
Codebeispiel
{
  "id": "em_01ky7ma8y2es1s2akzk53tmjn0",
  "status": "accepted"
}

Next steps