TypeScript · Astro
Send an email from an Astro API route 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
Exemple de code
npm create astro@latest my-app -- --template minimal --install --no-git --no-ai --yes
cd my-app
npm install @messagebird/sdk2. Send
Create src/pages/api/send.ts. prerender = false makes this route render on the server, on demand; Astro exposes .env values on import.meta.env, and BIRD_API_KEY has no PUBLIC_ prefix, so it stays server-only:
Exemple de code
import type { APIRoute } from "astro";
import { BirdClient } from "@messagebird/sdk";
export const prerender = false;
const bird = new BirdClient({ apiKey: import.meta.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 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 route:
Exemple de code
npm run dev
curl -X POST http://localhost:4321/api/sendBird accepts the email with a 202 and delivers it asynchronously; your route returns the em_ ID and status:
Exemple de code
{
"id": "em_01ky7ma8y2es1s2akzk53tmjn0",
"status": "accepted"
}To deploy on-demand routes to production, add a server adapter such as @astrojs/node or @astrojs/vercel. Static-only builds cannot run them.
Next steps
- TypeScript email SDK: the full email surface (send, get, list) and the error model.
- Send your first email: creating an API key and the full set of sandbox addresses.
- Sending domains: verify your own domain for production sending.
- Email API reference: the full request and response schema.
Ressources associées
Poursuivez avec la documentation, les guides et les exemples sur ce sujet. Les ressources sont en anglais.
Regarder le guideGetting started with emailComprendre le conceptShould I use a Bird SDK or call the API directly?Explorer la fonctionnalitéEmailSuivre le parcours d'apprentissageBuild your first integration
Essayez la pratique et obtenez un guide d'implémentation