Quick setup examples

Encore.ts

Send your first email from an Encore.ts API endpoint using the mailblastr SDK.

Encore is an open-source TypeScript framework that provisions infrastructure directly from your application code. Use the mailblastr Node SDK — install it with npm install mailblastr — and read your key from Encore's secrets management.

Prerequisites

1. Create an Encore app

encore app create --lang=ts my-app

2. Install the SDK

npm install mailblastr

3. Set your API key

Encore has built-in secrets management. Store your MailBlastr API key as a secret — no .env files needed.

encore secret set --type dev,local,pr,production MailBlastrAPIKey

4. Define a service

Every Encore.ts service needs a service definition file.

email/encore.service.ts
import { Service } from 'encore.dev/service';

export default new Service('email');

5. Send email from an API endpoint

Create a type-safe API endpoint. Read the secret with secret() from encore.dev/config, then call mb.emails.send().

email/send.ts
import { api } from 'encore.dev/api';
import { secret } from 'encore.dev/config';
import { MailBlastr } from 'mailblastr';

const mailblastrApiKey = secret('MailBlastrAPIKey');

interface SendRequest {
  to: string;
  subject: string;
  html: string;
}

interface SendResponse {
  id: string;
}

export const sendEmail = api(
  { expose: true, method: 'POST', path: '/email/send' },
  async (req: SendRequest): Promise<SendResponse> => {
    const mb = new MailBlastr(mailblastrApiKey());
    const { data, error } = await mb.emails.send({
      from: 'Acme <onboarding@yourdomain.com>',
      to: req.to,
      subject: req.subject,
      html: req.html,
    });

    if (error) {
      throw new Error(`Failed to send email: ${JSON.stringify(error)}`);
    }

    return { id: data.id };
  },
);

6. Run the app

encore run

Your API is running at http://localhost:4000. Send a test email:

curl -X POST http://localhost:4000/email/send \
  -H "Content-Type: application/json" \
  -d '{"to":"delivered@example.com","subject":"Hello World","html":"<strong>It works!</strong>"}'
See Send an email for every body field, including cc, bcc, reply_to, tags, and scheduled_at.