Send transactional email with Resend and Functions
Build a Hono endpoint on Azion Functions that calls the Resend API, store the API key as an environment variable, and deploy it with Azion CLI.
In this tutorial, you will build an HTTP endpoint on Azion Functions that sends a transactional email through Resend. You will create the project from a template, write the handler, store the API key, deploy the project, and send a live message.
The finished project is in the edge-functions-examples repository.
Prerequisites
- An Azion account. To create one, refer to How to create an account on Azion.
- A Resend account with an API key.
- Azion CLI installed. For the installation options, refer to Azion CLI.
- Node.js version 18 or higher.
1. Create the project from the Hono template
To authorize the CLI and create the project:
Run the login command:
With no credential flags, the CLI opens a browser-based flow. It stores the credentials locally, and every later command is authorized against your account.
Run the init command in the directory that holds your projects:
The command opens an interactive journey.
Enter a name, or press enter to accept the suggestion:
The CLI lists the presets it supports:
The CLI offers a local development server and a deployment. Answer N to both, because the handler is not written yet:
Go to the project directory and install its dependencies:
The project directory holds the Hono template and its dependencies.
2. Write the handler that calls the Resend API
The handler answers a GET request on the root path, posts the message to the Resend API, and returns the API response as JSON. The entry field of azion.config.js names the entry file of the project. Open that file and replace its contents with:
Set EMAIL_TO to the address that receives the message. The html field carries the message body as an HTML string, so a template literal substitutes the values that change per recipient.
The final line exports the app as an ES Modules handler, the pattern Azion recommends over the legacy Service Worker pattern. For both patterns and the move between them, refer to Migrate handler patterns in Functions.
3. Store the Resend API key as an environment variable
The handler reads RESEND_API_KEY at run time, so the key stays out of the project files and out of version control. To create the variable on your account:
A variable whose key contains password, pwd, secret, key, hash, encrypted, passcode, auth, or token is sent as a secret by default. The --secret true flag repeats that default here. Give the Resend API key the smallest permission set that the send call needs.
To replace the value later, run azion update variables and deploy the project again. A change to a variable does not reach a function that is already running.
The account holds the variable, and the handler reads it by key after the deployment.
4. Deploy the project
To build the project and publish it:
The deployment uploads the function code, configures the application, sets the routing rules, applies the environment variables and secrets, and returns a domain in the format https://xxxxxxx.map.azionedge.net. Azion opens a browser at the Azion Console page that carries the deployment logs.
The endpoint answers on the returned domain a few minutes after propagation.
5. Send the first email
To run the handler, request the root path of the domain the deployment returned:
The response body is the JSON that the Resend API returns for the request. The message arrives in the inbox of the address set in EMAIL_TO, with the subject hello world and the body it works!.
The endpoint now sends a transactional email on every request to its root path.