Bun
-
Install the SDK.
Terminal window bun add @spacepost/node -
Put your key in
.env. Bun loads it automatically..env SPACEPOST_API_KEY=sp_live_YOUR_API_KEY -
Send an email.
send.ts import { SpacePost } from "@spacepost/node";const spacepost = new SpacePost(Bun.env.SPACEPOST_API_KEY);const { data, error } = await spacepost.emails.send({from: "Acme <hello@acme.com>",to: "user@example.com",subject: "Hello from Bun",html: "<p>Sent with Bun and SpacePost.</p>",});console.log(error ?? data);Terminal window bun run send.ts
A tiny HTTP server that sends mail on request:
import { SpacePost } from "@spacepost/node";
const spacepost = new SpacePost();
Bun.serve({ port: 3000, async fetch(req) { if (req.method !== "POST") return new Response("POST only", { status: 405 }); const { email } = (await req.json()) as { email: string }; const { data, error } = await spacepost.emails.send( { from: "Acme <hello@acme.com>", to: email, subject: "Thanks for signing up", text: "We will be in touch." }, { idempotencyKey: `signup-${email}` }, ); return Response.json(error ?? data, { status: error?.statusCode ?? 200 }); },});