# TypeScript · Bun

Send an email from a `Bun.serve` fetch handler with `@messagebird/sdk`. Bun runs TypeScript directly, no build step. You need a Bird API key: create one in **Developers → API keys** ([Send your first email](/docs/get-started/send-your-first-email) walks through it), then put it in `.env` as `BIRD_API_KEY` (Bun loads `.env` automatically).

## 1. Install

```bash
bun init -y
bun add @messagebird/sdk
# npm install @messagebird/sdk
# pnpm add @messagebird/sdk
# yarn add @messagebird/sdk
```

## 2. Send

Create `server.ts`:

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

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

Bun.serve({
  port: 3000,
  async fetch(req) {
    if (req.method !== "POST") return new Response("Not found", { status: 404 });

    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](/docs/guides/email/testing-sandbox) 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 it:

```bash
bun run server.ts
curl -X POST http://localhost:3000/
```

Bird accepts the email with a `202` and delivers it asynchronously; your handler returns the `em_` ID and status:

```json
{
  "id": "em_01ky7ma8y2es1s2akzk53tmjn0",
  "status": "accepted"
}
```

## Next steps

- [TypeScript email SDK](/docs/sdks/typescript): the full email surface (`send`, `get`, `list`) and the error model.
- [Send your first email](/docs/get-started/send-your-first-email): creating an API key and the full set of sandbox addresses.
- [Sending domains](/docs/guides/email/sending-domains): verify your own domain for production sending.
- [Email API reference](/docs/api/reference/create-email-message): the full request and response schema.