> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Send your first transactional email Canonical URL: https://posthog.com/docs/workflows/tutorials/first-transactional-email > **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 - [Before you start](#before-you-start) - [Send the email](#send-the-email) - [What you learned](#what-you-learned) - [Next steps](#next-steps) In this tutorial, you send one transactional email from your code, read it in your inbox, and find it on your own person in PostHog. You learn what the secret API key, the test sender, the idempotency key, and the distinct ID each do. ## Before you start - **A PostHog account** – you send the first email to the address you use to log in. - **A server project or a terminal** – any Node.js or Python app works, or `curl`. - **No domain yet** – until you verify a domain, PostHog sends from the [PostHog test sender](https://posthog.com/docs/workflows/email/test-sender.md), which only emails members of your PostHog organization. You can't email a real user by mistake. ## Send the email Pick how you want to learn. Both paths reach the same checkpoints. ## With your agent ### Start the agent Pick one way to start: **Option A: run the wizard.** From your server project, run the wizard. It creates the key, installs the SDK, and writes the first send for you. ### Send your first email ``` npx @posthog/wizard email ``` **Option B: connect the PostHog MCP and learn step by step.** Run `npx @posthog/wizard mcp add` to connect the [PostHog MCP](https://posthog.com/docs/workflows/mcp.md) to your agent. Then copy this prompt into your agent: Guided tutorial promptwith the [PostHog MCP](https://posthog.com/docs/workflows/mcp.md)[sending-transactional-emails](https://posthog.com/docs/workflows/skills.md) “Walk me step by step through sending my first transactional email in this repo. Before each step, briefly explain what it does (project secret API key, test sender vs. verified domain, idempotencyKey, and distinctId). Start with the PostHog test sender to my own PostHog account email, stop after the first send so I can verify it in my inbox and in PostHog's Log tab, and do not switch to a live user list until I confirm.” Copy The agent stops at each checkpoint below. Check what you see before you tell it to continue. ### Checkpoint 1: The secret key and the SDK **What the agent explains:** a project secret API key starts with `phs_`. With the `transactional_email:write` scope, it can send email, upload attachments, and read the status of a send, and nothing else. It stays on your server, in an environment variable such as `POSTHOG_PROJECT_SECRET_API_KEY`. **What you should see:** - A new key in [**Project secret API keys**](https://app.posthog.com/settings/project-secret-api-keys), with **Transactional email** set to **Write**. - The key in your environment, not in your code or in a commit. - The PostHog SDK in your dependencies, such as `posthog-node` or `posthog`. ### Checkpoint 2: The first send **What the agent explains:** - **`templateKey`** – names the email, such as `welcome`. Each key shows as one email in PostHog, with its own sends and metrics. - **`idempotencyKey`** – if your code retries the same send, PostHog sends the email only once. - **`distinctId`** – links the email to a person, so its events land next to everything else they did. **What you should see:** the agent runs the send to your PostHog account email, and PostHog returns `202` with a message ID. `202` means queued, not delivered yet. ### Checkpoint 3: Verify the delivery **What the agent explains:** the test sender only emails members of your PostHog organization, and adds a footer that says so. **What you should see:** - The email in your inbox, from "your organization name via PostHog". - In PostHog, go to [**Transactional**](https://app.posthog.com/transactional) and open the **Log** tab. Your send shows with its timeline: sent, then delivered. - Open the send. It links to your person, with what you did before and after it. ### Checkpoint 4: Get ready for real users **What the agent explains:** to email real users, [verify your sending domain](https://posthog.com/docs/workflows/domain.md). Your code doesn't change: once the domain is verified, sends move from the test sender to your domain. **What you should see:** the agent stops here. It doesn't send to anyone else until you verify your domain and tell it to continue. ## By hand ### 1\. Create a secret API key 1. Go to [**Project secret API keys**](https://app.posthog.com/settings/project-secret-api-keys), and click **Create project secret API key**. 2. Set **Transactional email** to **Write**. This is the `transactional_email:write` scope. 3. Copy the key. It starts with `phs_`, and you only see it once. 4. Save it on your server as `POSTHOG_PROJECT_SECRET_API_KEY`. Never ship it to the browser. **What you should see:** the new key in the list, with the **Transactional email** scope. ### 2\. Install the SDK For Node.js: ```bash npm install posthog-node@6 ``` For Python: ```bash pip install "posthog>=8,<9" ``` With cURL, you don't install anything. ### 3\. Send to yourself Replace `you@example.com` with the email you use to log in to PostHog, and `user_123` with your own distinct ID. Until you verify a domain, the email goes out from the test sender. ### Node.js ```javascript import { PostHog } from "posthog-node" const posthog = new PostHog("", { host: "https://us.i.posthog.com", secretKey: process.env.POSTHOG_PROJECT_SECRET_API_KEY, enableLocalEvaluation: false, }) const { data, error } = await posthog.emails.send({ to: "you@example.com", templateKey: "welcome", subject: "My first PostHog email", html: "

It works.

", distinctId: "user_123", idempotencyKey: "first-email-user_123", }) console.log(data ?? error) ``` ### Python ```python import os from posthog import Posthog posthog = Posthog( "", host="https://us.i.posthog.com", secret_key=os.environ["POSTHOG_PROJECT_SECRET_API_KEY"], ) result = posthog.emails.send( to="you@example.com", template_key="welcome", subject="My first PostHog email", html="

It works.

", distinct_id="user_123", idempotency_key="first-email-user_123", ) print(result) ``` ### cURL ```bash curl -X POST "https://us.posthog.com/api/messaging/send/email/" \ -H "Authorization: Bearer $POSTHOG_PROJECT_SECRET_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: first-email-user_123" \ -d '{ "template_key": "welcome", "to": "you@example.com", "distinct_id": "user_123", "subject": "My first PostHog email", "html": "

It works.

" }' ``` With cURL, use `https://eu.posthog.com` if you log in to PostHog in the EU. **What you should see:** a `202` response with a message ID. Send it again with the same idempotency key, and PostHog doesn't send a second email. ### 4\. Verify the delivery 1. Open your inbox. The email comes from "your organization name via PostHog", with a test sender footer. 2. In PostHog, go to [**Transactional**](https://app.posthog.com/transactional) and open the **Log** tab. 3. Open your send. **What you should see:** the timeline of the send, from sent to delivered, and a link to your person, with what you did before and after the email. ### 5\. Verify your domain The test sender only emails members of your PostHog organization. To email your users, [set up your domain](https://posthog.com/docs/workflows/domain.md). **What you should see:** once your domain is verified, your next send comes from your own address. You don't change your code. ## What you learned - A project secret API key with the `transactional_email:write` scope sends email from your server. - The test sender lets you test safely before you verify a domain. - An idempotency key stops a retry from sending the same email twice. - A distinct ID links each send to a person, and the **Log** tab shows its timeline. ## Next steps - [Send emails from code](https://posthog.com/docs/workflows/transactional/send-from-code.md) – templates, attachments, and React Email. - [Reliable delivery](https://posthog.com/docs/workflows/concepts/reliable-delivery.md) – how retries, idempotency, and statuses work together. - [Marketing or transactional](https://posthog.com/docs/workflows/concepts/marketing-vs-transactional.md) – which kind of email to send, and why it matters. - [Build your first automation](https://posthog.com/docs/workflows/automations/first-automation.md) – send emails that react to what people do.