
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:/send-emailsends an email from an AutoSend template or from HTML and text, with the Send Email API./send-eventrecords an AutoSend event for a contact, with the Send Event API. Events can start automations.
Prerequisites
Before you begin, make sure you have:- An AutoSend account with a verified domain and a project API key, which starts with
AS_ - A Neon project in a region where Neon Functions are available
- Node.js 24+ installed locally
Quickstart
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.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 }.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.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/.Send an Email
The keys in The function returns the Check the Email Activity page to see the email.
dynamicData fill the template’s variables:emailId:Send Your Own Content
To send your own content, passsubject 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
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.Send the Event
eventProperties is a flat object of strings, numbers, and booleans.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 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.