Prerequisites
Use a verified sending domain, your account ID, and a domain-scoped send-only API v2 key. Use sandbox mode for every test. Management tasks need a separate full API key. Use Node.js 22 and the Node.js Functions Framework. A Google Cloud account is only needed when you deploy.Install the Dependencies
Try a Sandbox Send
Run this short SDK check locally before adding the longer server integration below. Use a verified sender, a send-only API key, and your account ID. It sends to a reserved test address withsandbox: true, so no email is delivered.
In the server project directory, add .env to .gitignore, then create .env with AHASEND_API_KEY, AHASEND_ACCOUNT_ID, and AHASEND_FROM (an address on your verified domain). Keep these values out of browser code and AI prompts. Use Node.js 22 or newer. Save this as quick-send.mjs:
quick-send.mjs
node --env-file=.env quick-send.mjs from that directory. The output lists each recipient status; queued or scheduled means accepted for sandbox processing. The process exits with a failure if the request or any recipient fails. Continue below for the full integration.
Add the Shared Sending Code
Save this assend-email.cjs beside the function file. Configure AHASEND_API_KEY, AHASEND_ACCOUNT_ID, AHASEND_FROM, AHASEND_TO and a long random SEND_TOKEN on the server. Set AHASEND_TO=recipient@example.com and AHASEND_DELIVERY_MODE=sandbox for local testing. Keep a sandbox credential until you are ready for real delivery; changing the mode to live alone cannot override a sandbox credential.
send-email.cjs
Create the HTTP Function
Save this asindex.cjs:
index.cjs
Run with the Functions Framework
If port 8080 is busy, setPORT to an unused local port in both shells. The commands below default to 8080.
Export the shared code’s environment variables before starting the local server:
SEND_TOKEN set, run:
Prepare the Hosted Function
Choose the Node.js 22 runtime and entry pointsendEmail. Follow Google’s function deployment steps. Put secrets in Secret Manager, allow only trusted invokers, and cap requests before they create mail jobs. When Cloud Run IAM and the app token are both in use, put the Google identity token in X-Serverless-Authorization and keep the app token in Authorization, as described in Google’s service authentication guide.
Use webhooks to track final delivery. Also see Google functions in Python, Go and Node.js SMTP.
Related Guides
- Before sending: verify a domain and create a send-only key.
- Other ways to send: REST API, SMTP, CLI quickstart, Node.js SDK and Go SDK.
- Request rules: API authentication, scopes, idempotency, errors and rate limits.
- Testing and events: sandbox mode, CLI webhook testing, event payloads, signature verification and delivery retries.
- Data and limits: retention, tracking and plans and feature availability.

