Quick setup examples

Astro

Send your first email from an Astro Action using the sendping SDK.

Send your first email from an Astro app using the sendping Node SDK. Install it with npm install sendping, then import SendPing and call mb.emails.send(). Because Astro runs server code on demand, an Astro Action is the natural place to make the call.

Prerequisites

1. Install the SDK

npm install sendping

2. Add your API key

Add your key to a .env file at the project root. Astro exposes it server-side via import.meta.env.

.env
SENDPING_API_KEY=mb_xxxxxxxxx

3. Send email from an Action

Create an Astro Action under src/actions/index.ts. Instantiate the client with the key from import.meta.env, call mb.emails.send(), and surface any error as an ActionError.

src/actions/index.ts
import { ActionError, defineAction } from 'astro:actions';
import { SendPing } from 'sendping';

const mb = new SendPing(import.meta.env.SENDPING_API_KEY);

export const server = {
  send: defineAction({
    accept: 'form',
    handler: async () => {
      const { data, error } = await mb.emails.send({
        from: 'Acme <onboarding@yourdomain.com>',
        to: ['delivered@test.sendping.co'],
        subject: 'Hello world',
        html: '<strong>It works!</strong>',
      });

      if (error) {
        throw new ActionError({ code: 'BAD_REQUEST', message: JSON.stringify(error) });
      }

      return data;
    },
  }),
};

Call the send action from any frontmatter route, script, or component.

A successful call returns the created email's id. See every supported field — cc, bcc, reply_to, attachments, scheduled_at — in the Send an email reference.