Documentation
Sign inGet started

TypeScript · Next.js

Send an email from a Next.js App Router Server Action 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.local as BIRD_API_KEY.

1. Install

Codebeispiel
npx create-next-app@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 app/actions/send-welcome.ts — the "use server" directive keeps it (and your API key) on the server:
Codebeispiel
"use server";

import { BirdClient } from "@messagebird/sdk";

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

export async function sendWelcome() {
  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 { 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

Wire the action to a form in app/page.tsx and click the button:
Codebeispiel
import { sendWelcome } from "./actions/send-welcome";

export default function Home() {
  return (
    <form action={sendWelcome}>
      <button type="submit">Send email</button>
    </form>
  );
}
Run npm run dev, open http://localhost:3000, and submit. The API responds with 202 — Bird has accepted the message and delivers it asynchronously:
Codebeispiel
{
  "id": "em_019c1930687b7bfa8a1b2c3d4e5f6789",
  "status": "accepted"
}

Next steps