Skip to main content
AutoSend x Neon

Overview

This guide shows you how to send transactional emails and events from Neon Functions 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 template to add a function with two routes: Neon Function URLs are public, so both routes require a bearer token.
To send only Managed Better Auth emails, without code, use AutoSend SMTP instead.

Prerequisites

Before you begin, make sure you have:

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:
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:
.env.local
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 page. Open the template’s menu and select Template ID to copy its ID, which starts with A-.Learn more about email templates and template variables.
4

Deploy the Function

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:
The function returns the emailId:
Check the Email Activity page to see the email.

Send Your Own Content

To send your own content, pass subject with html, text, or both instead of templateId:
to, from, and replyTo each accept an address ([email protected]), a name and address (Jane <[email protected]>), or an object ({ "email": "[email protected]", "name": "Jane" }).
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 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

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 page, click New Automation. Select the Event received trigger, choose your event, and add the email to send. Select an unsubscribe group in the automation’s settings, then activate it. Only events that arrive after you activate the automation start it.

Error Handling

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 lists every field and error.

Store Email Events in Neon

To store delivery, bounce, spam, and unsubscribe events in Postgres, point an AutoSend webhook at another Neon Function.

Store AutoSend webhook events in Neon

Verify each webhook, store it in Postgres without duplicates, and test on a branch.