Sign inGet Started

Node.js SDK

Send email with Astro

Use Parcel Wing from Astro server endpoints and form submissions while keeping your API key server-only.

Install the SDK

The official SDK is published as @parcelwing/node.

Terminal

npm install @parcelwing/node

Server endpoint

Create an Astro endpoint that validates input, sends email, and returns a compact JSON response.

src/pages/api/send-welcome.ts

import type { APIRoute } from "astro";
import { ParcelWing, ParcelWingError } from "@parcelwing/node";
 
const parcelWing = new ParcelWing({
apiKey: import.meta.env.PARCEL_WING_API_KEY,
});
 
export const POST: APIRoute = async ({ request }) => {
const body = await request.json() as { email: string; firstName?: string };
 
try {
const [message] = await parcelWing.emails.send({
from: "Acme <[email protected]>",
to: body.email,
template_alias: "welcome_email",
template_params: {
first_name: body.firstName ?? "friend",
},
});
 
return new Response(JSON.stringify({ id: message?.id }), {
status: 200,
headers: { "content-type": "application/json" },
});
} catch (error) {
if (error instanceof ParcelWingError) {
return new Response(
JSON.stringify({ code: error.code, requestId: error.requestId }),
{
status: error.status,
headers: { "content-type": "application/json" },
},
);
}
 
return new Response(JSON.stringify({ error: "Unexpected error" }), {
status: 500,
headers: { "content-type": "application/json" },
});
}
};

Form

Submit to your server endpoint from an Astro page or component. Keep the Parcel Wing API key out of browser JavaScript.

src/pages/contact.astro

---
// src/pages/contact.astro
---
 
<form method="post" action="/api/send-welcome">
<input name="email" type="email" required placeholder="[email protected]" />
<input name="firstName" placeholder="Ada" />
<button type="submit">Send welcome email</button>
</form>

Production notes

  • Use SSR/server output or endpoint routes for trusted sending logic.
  • Store PARCEL_WING_API_KEY in server-only environment variables.
  • Validate and rate limit public forms before sending email.