Documentation
Sign inGet started

TypeScript · Astro

Send an email from an Astro API route with @messagebird/sdk. You'll need a Bird API key — create one in Developers → API keys as covered in Send your first email, then put it in .env as BIRD_API_KEY.

1. Install

Przykład kodu
npm create astro@latest my-app && cd my-app
npm install @messagebird/sdk
# pnpm add @messagebird/sdk
# yarn add @messagebird/sdk
# bun add @messagebird/sdk

2. Send

Create src/pages/api/send.tsprerender = false makes this route render on the server, on demand:
Przykład kodu
import type { APIRoute } from "astro";
import { BirdClient } from "@messagebird/sdk";

export const prerender = false;

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

export const POST: APIRoute = async () => {
  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"
  return Response.json({ id: msg.id, status: msg.status });
};
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 region is inferred from your key's bk_us1_ / bk_eu1_ prefix, so there's no base URL to configure. The SDK auto-generates an Idempotency-Key per call, so retried sends are safe.

3. Try it

Run the dev server and POST to the route:
Przykład kodu
npm run dev
curl -X POST http://localhost:4321/api/send
The API responds with 202 — Bird has accepted the message and delivers it asynchronously:
Przykład kodu
{
  "id": "em_019c1930687b7bfa8a1b2c3d4e5f6789",
  "status": "accepted"
}
To deploy on-demand routes to production, add a server adapter (@astrojs/node, @astrojs/vercel, …) — static-only builds can't run them.

Next steps