Documentation
Sign inGet started

TypeScript · SvelteKit

Send an email from a SvelteKit +server.ts endpoint with @messagebird/sdk. You need a Bird API key: create one in Developers → API keys (Send your first email walks through it), then put it in .env as BIRD_API_KEY.

1. Install

Codebeispiel
npx sv create 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/routes/api/send/+server.ts. +server.ts files run only on the server, and $env/static/private reads BIRD_API_KEY from .env without ever shipping it to the client:
Codebeispiel
import { json } from "@sveltejs/kit";
import { BIRD_API_KEY } from "$env/static/private";
import { BirdClient } from "@messagebird/sdk";

const bird = new BirdClient({ apiKey: BIRD_API_KEY });

export async function POST() {
  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 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 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 dev server and POST to the endpoint:
Codebeispiel
npm run dev
curl -X POST http://localhost:5173/api/send
Bird accepts the email with a 202 and delivers it asynchronously; your endpoint returns the em_ ID and status:
Codebeispiel
{
  "id": "em_01ky7ma8y2es1s2akzk53tmjn0",
  "status": "accepted"
}

Next steps