> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pnta.io/llms.txt
> Use this file to discover all available pages before exploring further.

# PostHog

> Send PNTA push notifications from a PostHog workflow

## Prerequisites

* PostHog project with a workflow (or a new one)
* API key created in [PNTA Dashboard](https://app.pnta.io) (project **API Keys** page)
* Platform keys configured in [PNTA Dashboard](https://app.pnta.io) (see [Platform Keys Setup](https://docs.pnta.io/resources/platform-keys.md))
* Devices registered via a PNTA client SDK, with the user ID stored in device metadata (e.g. `user_id`)

> [!TIP]
>
> To see the metadata of a device, open **Devices** in the PNTA Dashboard,
> then open one device.

## Setup Steps

### 1. Add an HTTP Webhook Step

1. In PostHog, open your workflow
2. Add an **HTTP Webhook** step
3. Set the URL to `https://app.pnta.io/api/v1/notifications`
4. Set the method to `POST`

![PostHog HTTP Webhook step with the PNTA URL and POST method](https://mintcdn.com/pnta/--qt4orKbam50ZUB/images/posthog-webhook-step.png?fit=max\&auto=format\&n=--qt4orKbam50ZUB\&q=85\&s=789aca3aa92af34ad7adb0d2ad84397a)

### 2. Set the Headers

1. In **Headers**, add `Content-Type: application/json`
2. In **Secret headers**, add `Authorization: Bearer <PNTA API key>`

> [!WARNING]
>
> Put the API key in **Secret headers**, not in **Headers**. PostHog encrypts
> secret headers and hides them after you save.

### 3. Set the JSON Body

```json theme={null}
{
    "notification": {
        "title": "Hello",
        "body": "Your message here",
        "link_to": "https://example.com",
        "targets": [{ "metadata": { "user_id": "{event.distinct_id}" } }]
    }
}
```

Replace `user_id` with the metadata key your app sets on devices. PostHog fills `{event.distinct_id}` from the event that triggered the workflow. `link_to` is optional. It sets what opens when the user taps the notification: an in-app route such as `/orders/123` (a deep link), or a URL such as `https://example.com`. Your app handles the tap, see [Flutter](https://docs.pnta.io/guides/flutter/quick-start.md#deep-linking-setup) or [React Native](https://docs.pnta.io/guides/react-native/api-reference.md#link-handling). For all body fields, see [Send Notification](https://docs.pnta.io/api-reference/notifications/send-notification.md).

Each entry in `targets` can be:

* A metadata match, such as `{ "metadata": { "user_id": "123" } }`. PNTA sends to every active device whose metadata contains all the given keys and values.
* A device ID, such as `"dev_V7pNZf2B"`
* A topic ID, such as `"top_..."`
* A raw device push token

If some targets match no device, PNTA skips them and sends to the rest.

### 4. Test and Enable

1. In the HTTP Webhook step, turn on **Log responses**. The step logs then show the request body and the PNTA response.
2. In the test panel, turn off mocking. If mocking is on, PostHog does not send the request.
3. Run a test with an event whose `distinct_id` is the user ID on your test device. Check that the push arrives on the device.
4. Turn off **Log responses**, then publish and enable the workflow. A draft workflow does not run.

> [!NOTE]
>
> Before PostHog identifies a user, `event.distinct_id` is an anonymous ID and
> matches no device. The metadata value on the device must match the
> identified `distinct_id`. If your user ID is in a person property, use
> `{person.properties.<property>}` instead.

## Troubleshooting

**The step fails with `401`.** The API key is wrong. Check the `Authorization` value in **Secret headers**.

**The step fails with `422`.** The body is not valid, or no target matched a device or topic. The step logs show the PNTA error. "Delivery targets did not match any devices or topics" usually means the metadata key or value does not match the device.

PostHog does not retry a failed step. By default, the workflow continues to the next step.

## Resources

> [card] [PostHog Workflows](https://posthog.com/docs/workflows)
>
> Learn more about PostHog workflows

> [card] [API Reference](https://docs.pnta.io/api-reference/notifications/send-notification.md)
>
> All body fields for sending a notification


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.