> ## Documentation Index
> Fetch the complete documentation index at: https://docs.autosend.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Send emails with Neon Functions

> Send transactional emails and AutoSend events from a Neon Function with the Send email with AutoSend template.

![AutoSend x Neon](https://mintcdn.com/autosend-13920f5c/elz97IrcT4EqDdvp/images/guides/autosendxneon.jpg?fit=max\&auto=format\&n=elz97IrcT4EqDdvp\&q=85\&s=305b28a3494206bc5bd32540321c0f93)

## Overview

This guide shows you how to send transactional emails and events from [Neon Functions](https://neon.com/docs/compute/functions/overview) with AutoSend. Neon Functions run your backend code on a Neon branch, next to your Postgres database. This guide uses the [Send email with AutoSend](https://neon.com/functions/templates/autosend-email) template to add a function with two routes:

* `/send-email` sends an email from an AutoSend template or from HTML and text, with the [Send Email API](https://docs.autosend.com/api-reference/mails/send.md).
* `/send-event` records an AutoSend [event](https://docs.autosend.com/automations/events.md) for a contact, with the [Send Event API](https://docs.autosend.com/api-reference/events/send-event.md). Events can start [automations](https://docs.autosend.com/automations.md).

Neon Function URLs are public, so both routes require a bearer token.

> [!TIP]
>
> To send only Managed Better Auth emails, without code, use [AutoSend SMTP](https://docs.autosend.com/guides/smtp/neon.md) instead.

## Prerequisites

Before you begin, make sure you have:

* An [AutoSend account](https://autosend.com/) with a [verified domain](https://autosend.com/settings/domains) and a project [API key](https://autosend.com/account/api-key), which starts with `AS_`
* A Neon project in a region where [Neon Functions are available](https://neon.com/docs/compute/functions/overview)
* Node.js 24+ installed locally

## Quickstart

### 1. Add the Function to Your Project

Install the Neon CLI, log in, and link your directory to your Neon project. Then add the template:

```bash theme={null}
npm install -g neon@latest
neon login
neon link
neon functions new autosend-email
```

The CLI writes the function to `functions/autosendemail/`, declares it in `neon.ts`, and lists its environment variables in `.env.example`.

### 2. Set Environment Variables

Add these variables to `.env.local`, next to the Neon values that are already there:

```bash .env.local theme={null}
AUTOSEND_API_KEY=your_autosend_api_key_here
AUTOSEND_FROM="MyApp <hello@yourdomain.com>"
AUTOSEND_FUNCTION_TOKEN=your_function_token_here
```

| Variable | Description |
| - | - |
| `AUTOSEND_API_KEY` | Your AutoSend project API key. Keep it on the server. |
| `AUTOSEND_FROM` | The sender to use when a request doesn't set `from`. Must be on a verified domain. |
| `AUTOSEND_FUNCTION_TOKEN` | A random token of at least 32 characters, for example from `openssl rand -hex 32`. Callers send it as `Authorization: Bearer <token>`. |

> [!NOTE]
>
> `AUTOSEND_FROM` is optional if every request sets `from`. The Neon CLI declares each variable in `neon.ts` as `process.env.<NAME>!`, so `neon.ts` doesn't load while one is unset. If you leave `AUTOSEND_FROM` out, change its declaration to `{ value: process.env.AUTOSEND_FROM, required: false }`.

### 3. Create an Email Template

Create a template on the [Email Templates](https://autosend.com/dynamic-templates) page. Open the template's menu and select **Template ID** to copy its ID, which starts with `A-`.

Learn more about [email templates](https://docs.autosend.com/transactional-emails/email-templates.md) and [template variables](https://docs.autosend.com/transactional-emails/variables.md).

### 4. Deploy the Function

```bash theme={null}
neon deploy --env .env.local
neon functions get autosendemail
```

`neon functions get` prints the function URL. You can also run the function locally with `neon dev`, which serves it at `http://localhost:8787/`.

### 5. Send an Email

The keys in `dynamicData` fill the template's variables:

```bash theme={null}
curl -s https://<function-url>/send-email \
  -H "Authorization: Bearer YOUR_FUNCTION_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "to": { "email": "jane@example.com", "name": "Jane" },
    "templateId": "A-your_template_id",
    "dynamicData": { "firstName": "Jane" }
  }'
```

The function returns the `emailId`:

```json theme={null}
{ "emailId": "698afb75ff4bc5466e3a797a" }
```

Check the [Email Activity](https://autosend.com/email-activities) page to see the email.

## Send Your Own Content

To send your own content, pass `subject` with `html`, `text`, or both instead of `templateId`:

```bash theme={null}
curl -s https://<function-url>/send-email \
  -H "Authorization: Bearer YOUR_FUNCTION_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "to": "jane@example.com",
    "subject": "Your receipt",
    "text": "Thanks for your order."
  }'
```

`to`, `from`, and `replyTo` each accept an address (`jane@example.com`), a name and address (`Jane <jane@example.com>`), or an object (`{ "email": "jane@example.com", "name": "Jane" }`).

> [!NOTE]
>
> When you send with a template, AutoSend uses the template's subject. If `dynamicData` leaves out a variable, the email still sends, with that variable empty.

## Start Automations with Events

### 1. Create the Event

On the [Events](https://autosend.com/events) page, click **New Event**, enter the event name, and add each property with its type, such as `plan` as a string. The Send Event API rejects event names and properties the event doesn't declare.

### 2. Send the Event

```bash theme={null}
curl -s https://<function-url>/send-event \
  -H "Authorization: Bearer YOUR_FUNCTION_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "email": "jane@example.com",
    "eventName": "signup_completed",
    "eventProperties": { "plan": "pro" }
  }'
```

```json theme={null}
{ "eventId": "60d5ec49f1b2c72d9c8b9999", "contactId": "60d5ec49f1b2c72d9c8b8888" }
```

If no contact has that email yet, AutoSend creates one. `eventProperties` is a flat object of strings, numbers, and booleans.

### 3. Create the Automation

On the [Automations](https://autosend.com/automations) page, click **New Automation**. Select the **Event received** trigger, choose your event, and add the email to send. Select an [unsubscribe group](https://docs.autosend.com/others/unsubscribe-groups.md) in the automation's settings, then activate it. Only events that arrive after you activate the automation start it.

## Error Handling

| Status | Cause |
| - | - |
| `400` | The body is not a JSON object, or a field is missing or invalid |
| `401` | The function token is missing or wrong |
| `413` | The body is over 500,000 bytes for `/send-email` or 100,000 bytes for `/send-event` |
| `422` | AutoSend refused the request, for example with `TEMPLATE_NOT_FOUND`, `INVALID_EMAIL_SENDER`, `EVENT_NOT_FOUND`, or `UNKNOWN_PROPERTY`. The response includes AutoSend's `error` and `code`. |
| `429` | AutoSend rate-limited the request. The response passes on `Retry-After`. |
| `502` | AutoSend rejected the API key, returned another error, or could not be reached |
| `503` | `AUTOSEND_API_KEY` is unset, or `AUTOSEND_FUNCTION_TOKEN` is unset or shorter than 32 characters |

> [!WARNING]
>
> The function doesn't retry, and the Send Email API doesn't deduplicate requests. If you retry a failed call, the recipient can get the email twice.

The [template README](https://github.com/neondatabase/examples/tree/main/registry/autosend-email) lists every field and error.

## Store Email Events in Neon

To store delivery, bounce, spam, and unsubscribe events in Postgres, point an AutoSend [webhook](https://docs.autosend.com/others/webhooks/introduction.md) at another Neon Function.

> [card] [Store AutoSend webhook events in Neon](https://neon.com/guides/autosend-webhooks)
>
> Verify each webhook, store it in Postgres without duplicates, and test on a branch.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.