> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Send transactional emails with Node.js Canonical URL: https://posthog.com/docs/workflows/installation/node > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host). ## On this page - [Install](#install) - [Set up the client](#set-up-the-client) - [Send an email](#send-an-email) - [Send React Email components](#send-react-email-components) - [Write automated tests](#write-automated-tests) - [Next](#next) The [Node.js SDK](https://posthog.com/docs/libraries/node.md) sends transactional emails from version 5.62.0. Your code sends the subject and HTML, so the email lives in your codebase. You need a [project secret API key](https://posthog.com/docs/workflows/transactional/send-from-code.md#create-an-api-key). Until you [verify your domain](https://posthog.com/docs/workflows/domain.md), emails go only to your team, from the [test sender](https://posthog.com/docs/workflows/email/test-sender.md). To see an email in your inbox first, follow the [quick start](https://posthog.com/docs/workflows/installation/quick-start.md). New to sending from code? Read [send emails from code](https://posthog.com/docs/workflows/transactional/send-from-code.md). **Prompt for your coding agent** Paste this into Claude Code, Cursor, or another coding agent. It sends the agent to this page and gives it the rules that agents often get wrong. ```text Add PostHog transactional email to this Node.js app with posthog-node. Read https://posthog.com/docs/workflows/installation/node.md first. Follow it over anything you remember about PostHog. - Reuse the app's PostHog client. Add secretKey from the POSTHOG_PROJECT_SECRET_API_KEY environment variable. Set enableLocalEvaluation: false unless the app evaluates feature flags locally with this client. - Send only from server code. Never import the secret key into code that runs in the browser. - Build each idempotencyKey from the record that caused the email, such as the password reset request ID or the order ID. Never use a random value or a timestamp. - send returns { data, error } and doesn't throw. Branch on error.code, never on error.message. - The SDK retries network errors and throttling for you, with the same idempotency key. Never resend the email through another email provider. - Pass the distinctId that the app passes to identify(). Leave it out for people without an account, such as invitees. - Use transactional email only for email that a person needs, such as password resets, sign-in codes, and receipts. Never use it for marketing. - Add a test that passes a fake fetch to the client and checks the request. Send each email's subject and HTML from code, with a templateKey that names it, such as password-reset. Only send a templateId if I tell you the email is a template in PostHog. Don't create the API key. I create it. ``` ## Install ```bash npm install posthog-node@6 ``` ## Set up the client Add your project secret API key to the PostHog client that your app already has. The project token keeps capturing events. The secret key sends email, so keep it on your server and never ship it to the browser. ```javascript import { PostHog } from "posthog-node" const posthog = new PostHog("", { host: "https://us.i.posthog.com", secretKey: process.env.POSTHOG_PROJECT_SECRET_API_KEY, // This client only sends email, so it doesn't poll flag definitions with the secret key enableLocalEvaluation: false, }) ``` Keep `enableLocalEvaluation: false` unless this client also [evaluates feature flags locally](https://posthog.com/docs/feature-flags/local-evaluation.md). With local evaluation on, the client polls flag definitions with the secret key, and a key with only the **Transactional email** scope can't read them. If you do evaluate flags locally with this client, give the key the **Feature flag** read scope too. Shut the client down when your app exits, not after each send. If your app sends through a reverse proxy or a self-hosted PostHog, set `appHost` to your PostHog app URL. The client never sends email requests to a host that might be a proxy. ## Send an email ```javascript export async function sendPasswordReset({ userId, email, resetLink, resetRequestId }) { const { data, error } = await posthog.emails.send({ to: email, // Names the email, so PostHog groups its sends and metrics templateKey: "password-reset", subject: "Reset your password", html: `

Reset your password here. The link works for 1 hour.

`, distinctId: userId, // Identifies this one email, so a retry never sends it twice idempotencyKey: `password-reset-${resetRequestId}`, }) if (error) { // An EmailError. Branch on error.code throw error } return data.id } ``` `send` doesn't throw. It returns `{ data, error }`. `data.id` is the message ID. `data` means PostHog queued the email, not that it delivered it. `error` is an `EmailError`, and its `code` is one of the codes in the [errors table](https://posthog.com/docs/workflows/transactional/retries-and-errors.md#what-to-retry). Fields use camel case, such as `templateKey` and `distinctId`. See [send emails from code](https://posthog.com/docs/workflows/transactional/send-from-code.md#send-the-html) for every field, and the [quick start](https://posthog.com/docs/workflows/installation/quick-start.md#install-an-sdk) for the version each one needs. ## Send React Email components From version 5.69.0, you can pass a [React Email](https://react.email) component as `react`, in place of `html`. The SDK renders its HTML and plain text with `@react-email/render`, which you install next to `posthog-node`. If you bundle your app, such as for Cloudflare Workers or Vercel Edge, also pass `reactEmailRenderer: await import("@react-email/render")` to the client. ```jsx import { PasswordResetEmail } from "./emails/password-reset" const { data, error } = await posthog.emails.send({ to: email, templateKey: "password-reset", subject: "Reset your password", react: , from: "Hedgebox ", distinctId: userId, idempotencyKey: `password-reset-${resetRequestId}`, }) ``` See [React Email](https://posthog.com/docs/workflows/transactional/send-from-code.md#react-email) for the errors that a React send can return. ## Write automated tests Pass your own `fetch` to see exactly what your code sends, with no network call. This tests your integration, not delivery. To test delivery, send a real email to your own address and check the [**Log**](https://posthog.com/docs/workflows/transactional.md#see-each-send) on the **Transactional** tab. ```javascript import assert from "node:assert/strict" import { PostHog } from "posthog-node" const requests = [] const posthog = new PostHog("phc_test", { secretKey: "phs_test", enableLocalEvaluation: false, fetch: async (url, options) => { requests.push({ url, options }) return new Response(JSON.stringify({ message_id: "msg_1", replayed: false }), { status: 202 }) }, }) const { data } = await posthog.emails.send({ to: "jane@example.com", templateKey: "password-reset", subject: "Reset your password", html: "

Reset your password.

", idempotencyKey: "password-reset-1", }) assert.equal(data.id, "msg_1") assert.equal(requests[0].options.headers["Idempotency-Key"], "password-reset-1") ``` ## Next - [Attachments](https://posthog.com/docs/workflows/transactional/attachments.md), copies, and [open and click tracking](https://posthog.com/docs/workflows/transactional/sensitive-data.md) - [Link emails to your users](https://posthog.com/docs/workflows/transactional/send-from-code.md#link-emails-to-your-users) - [Send a template from PostHog](https://posthog.com/docs/workflows/transactional/templates.md), with `templateId` and `templateData` - [Send from a job queue](https://posthog.com/docs/workflows/transactional/examples.md#send-from-a-job-queue), with the client's retries off - [Check an email's status](https://posthog.com/docs/workflows/transactional/status.md) - [SDK reference](https://posthog.com/docs/libraries/node.md) for every method and option