# Astro

An API endpoint on a server-rendered route, and the one config line that makes it possible.

Astro is static by default, and a static build has no server to hold a key. Sending needs an adapter and a route that runs on the server.

### Install

Astro 4 or 5:
```bash
npm install @emails.sh/sdk
npx astro add node        # or: npx astro add vercel / cloudflare / netlify
```

.env:
```bash
# .env. The key comes from https://emails.sh/dashboard/api-keys.
EMAILSSH_API_KEY=esh_your_key_here
```

### The endpoint

src/pages/api/send.ts:
```ts
// src/pages/api/send.ts
import type { APIRoute } from 'astro';
import { Emailssh } from '@emails.sh/sdk';

// This route must run on the server, so opt it out of prerendering.
export const prerender = false;

export const POST: APIRoute = async ({ request }) => {
  const { to, subject, html } = await request.json();
  const mail = new Emailssh({ apiKey: import.meta.env.EMAILSSH_API_KEY });

  try {
    const { id } = await mail.send({ from: 'Acme <hello@acme.com>', to: [to], subject, html });
    return new Response(JSON.stringify({ id }), {
      status: 200,
      headers: { 'content-type': 'application/json' }
    });
  } catch (err) {
    console.error('emails.sh refused the send', err);
    return new Response(JSON.stringify({ error: 'Could not send that email.' }), { status: 502 });
  }
};
```

import.meta.env.EMAILSSH_API_KEY without a PUBLIC_ prefix stays server side. A variable named PUBLIC_EMAILSSH_API_KEY would be inlined into the client bundle, which is why the name matters here.

---

Base URL: https://emails.sh/v1. Auth: `Authorization: Bearer esh_...`.
Whole API in one file: https://emails.sh/llms.txt. All documentation: https://emails.sh/docs.md.
