Skip to content

Send Emails with Bun

Learn how to send transactional emails using PostStack and Bun.

1. Install the SDK

bash
bun add @poststack/sdk

2. Initialize the client

typescript
import PostStack from '@poststack/sdk';

const poststack = new PostStack(process.env.POSTSTACK_API_KEY!);

3. Send an email

typescript
const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    if (req.method === 'POST' && new URL(req.url).pathname === '/send') {
      const { data, error } = await poststack.emails.send({
        from: 'hello@yourdomain.com',
        to: 'user@example.com',
        subject: 'Hello from Bun!',
        html: '<h1>Welcome!</h1>',
      });

      if (error) return Response.json({ error }, { status: 400 });
      return Response.json(data);
    }
    return new Response('Not found', { status: 404 });
  },
});

console.log(`Listening on ${server.url}`);

Ready to send emails with Bun?

Create a free account and get your API key in under a minute.