Skip to content
Email API for Bun

Send email from Bun with an email API

Send email from Bun with the fetch it already ships: one POST, four headers, an id back, and delivery reported on a signed webhook.

Bun — send.ts
// Bun ships fetch: there is nothing to install.const apiKey = process.env.RASKET_API_KEY;if (apiKey === undefined) {  throw new Error("RASKET_API_KEY is not set");}
const response = await fetch(  "https://api.rasket.com/emails",  {    method: "POST",    headers: {      Authorization: `Bearer ${apiKey}`,      // Required: a request with no User-Agent is      // refused before it reaches your account.      "User-Agent": "acme-billing/1.0",      "Content-Type": "application/json",      "Idempotency-Key": "order-1042",    },    body: JSON.stringify({      from: "Acme <orders@send.acme.example>",      to: ["ronald.williams@example.com"],      subject: "Your order has shipped",      html: "<p>Order 1042 shipped today.</p>",    }),  },);
if (!response.ok) {  throw new Error(`Rasket answered ${response.status}`);}
const email = (await response.json()) as { id: string };console.log(email.id);

What sending from Bun actually involves

To send email from Bun you need nothing installed at all: fetch and process.env are both built in, and the send is one POST with four headers. The rasket package works under Bun too if you would rather have the typed client; the sample below is the version with no dependency, so you can read every header it sends.

Last updated 2026-09-16. Every field, header and limit below is the one the emails reference carries.

What you get with the key

Five things that arrive with the first request rather than with a later plan.

  • Idempotent sends

    Put an Idempotency-Key on a send and repeat it as often as you like. For 24 hours the same key and payload return the first response instead of a second email, and a different payload under that key is refused rather than sent.

  • Signed webhooks you can replay

    Delivery, bounce, complaint, open and click posted to your endpoint with a timestamp and a signature, retried on failure and replayable from the dashboard.

  • Inbound mail on your own domain

    Receive at your domain on every plan. Each message arrives on a signed webhook with its headers, text and HTML, and its attachments behind signed links.

  • Templates with typed variables

    Publish a template, then send it by ID or alias with the values it declares. Every email records the exact version it was rendered from.

  • Broadcasts and automations

    Contacts with typed properties, topics people subscribe to, and per-contact workflows that wait, branch and send — through the same pipeline as the rest of your mail.

How to send email from Bun

Six steps, in this order. Everything before the fifth is done once, and only the fifth is about the language you write in.

  1. 1

    Create an API key

    Open API keys in the dashboard and create one. It is shown once and stored hashed, so copy it then. Put it in your environment as RASKET_API_KEY and read it from there; a key committed to a repository is a key you have to rotate.

  2. 2

    Add a sending domain

    POST /domains with a domain you control. A subdomain such as send.acme.example is the usual choice, because it keeps this mail's reputation separate from the address your people write from. The response carries a records array.

  3. 3

    Publish the DNS records

    Every row of records goes into your DNS: the DKIM TXT record at rasket._domainkey, and the two records that give the domain its own return path. Nothing sends from the domain until they resolve.

  4. 4

    Verify the domain

    POST /domains/{domain_id}/verify. The domain comes back with a status on each record, and once it reads verified you can send from any address on it. An unverified sender is refused rather than quietly dropped.

  5. 5

    Send your first email from Bun

    Post the message with the sample above. Carry an Idempotency-Key — yours or your queue's retry cannot then turn into a second email — and a User-Agent, which every request has to have. A 200 with an id means we have taken responsibility for the message.

  6. 6

    Subscribe a webhook and check its signature

    POST /webhooks with an https endpoint and the events you care about. Delivery, bounce, complaint, open and click arrive there with svix-id, svix-timestamp and svix-signature; verify against the raw body before anything parses it.

Verify webhooks in Bun

In a Bun.serve handler, await request.text() is the raw body; take it before anything parses, then pass it with request.headers to verify() from @rasket/webhook-verify, which Bun installs and runs unchanged. Calling request.json() first destroys the exact string the signature covers.

The five steps a verifier performs, in seven languages, are on the webhooks reference.

Questions about Bun

Do I need an SMTP server to send email from Bun?

No. The send is an HTTPS request Bun can make with no dependency at all. There is no relay to run and no SMTP settings to carry between environments, just RASKET_API_KEY and a domain you have verified.

Do I need an SDK, or is fetch enough?

fetch is enough, and the sample above is the whole of it. The rasket package adds generated types, retries that only repeat what is safe to repeat, and the rate-limit headers parsed for you — worth having in a service, unnecessary in a script.

Where do I keep the API key in a Bun app?

In the environment. Bun reads .env on its own and exposes the value as process.env.RASKET_API_KEY, so check it once at startup and fail loudly if it is missing rather than sending a request with an empty bearer token.

How do I stop a retry from sending the same email twice?

Send an Idempotency-Key header derived from the thing the mail is about. For 24 hours the same key with the same payload returns the first response instead of sending again, and a different payload under that key is refused rather than sent.

How do I verify a Rasket webhook in Bun?

bun add @rasket/webhook-verify, read await request.text() as the first thing your handler does, and pass it with request.headers and your whsec_ secret to verify(). It requires the three svix headers, rejects a stale timestamp and compares in constant time.

Make your first send

Create a key, verify a domain, and post your first message from Bun. The free plan does not expire.