Messaging

For instructions on how to authenticate to use this endpoint, see API overview.

Endpoints

POST
GET
GET
POST
POST

Create messaging attachments uploads

Before sending an email with files, ask where to upload each one. Name each file by the SHA-256 of its bytes and its size. A file this project already uploaded answers present and needs no upload. Any other file answers upload: PUT its bytes to url with exactly the headers given and without your PostHog API key, before url_expires_at. Once every upload succeeded, send the email with the files' hashes in attachments. Needs the transactional_email:write scope.

Required API key scopes

transactional_email:write

Request parameters

  • files
    Click to open
    array

Response


Example request

POST /api/projects/:project_id/messaging/attachments/uploads
export POSTHOG_PERSONAL_API_KEY=[your personal api key]
curl
-H 'Content-Type: application/json'\
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/messaging/attachments/uploads/\
-d files="array"

Example response

Status 200 Each file is present, or comes with where to upload it
RESPONSE
{
"files": [
{
"sha256": "string",
"status": "present",
"expires_at": "2019-08-24T14:15:22Z",
"url": "http://example.com",
"method": "string",
"headers": {
"property1": "string",
"property2": "string"
},
"url_expires_at": "2019-08-24T14:15:22Z"
}
]
}
Status 400 A file is malformed, there are more than 10, they're too large for one email, or the project can't send files yet
Status 429 Rate limited, or the project uploaded its daily limit of file bytes
Status 503 File storage didn't answer. Send the same request again

Retrieve messaging emails

Get the status of one transactional email by the message_id that the send returned, for 30 days after the send. Needs a project secret API key with the transactional_email:read scope. A key with hog_flow:read also works, but that scope reaches the rest of Workflows too. The first status is recorded a few seconds after the send, so a read right after a send can answer 404. That 404 doesn't say whether the email went out yet, so read again a few seconds later.

Required API key scopes

transactional_email:read

Path parameters

  • id
    string

Response


Example request

GET /api/projects/:project_id/messaging/emails/:id
export POSTHOG_PERSONAL_API_KEY=[your personal api key]
curl \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/messaging/emails/:id/

Example response

Status 200
RESPONSE
{
"id": "string",
"status": "scheduled",
"created_at": "2019-08-24T14:15:22Z",
"status_at": "2019-08-24T14:15:22Z",
"reason": "string"
}
Status 401 The request has no project secret API key.
Status 404 No email of this project has this ID, or it is older than 30 days. In the first seconds after a send, it means PostHog accepted the email but hasn't recorded its first status yet.

Retrieve messaging emails status

Get the status of one transactional email by its message_id, or by the idempotency_key it was sent with when the send's response was lost. Name exactly one of them. A key gives the latest send with it that PostHog has a status for. Compare the response's created_at with when you sent: an earlier time means an earlier send with the same key, not the one whose response was lost. It answers like the read by message_id, for 30 days after the send, and needs the same transactional_email:read scope. A 404 doesn't mean the email wasn't sent: its first status can take a few seconds to arrive. To make sure it goes out, send it again with the same key within 24 hours.

Required API key scopes

transactional_email:read

Query parameters

  • idempotency_key
    string
  • message_id
    string

Response


Example request

GET /api/projects/:project_id/messaging/emails/status
export POSTHOG_PERSONAL_API_KEY=[your personal api key]
curl \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/messaging/emails/status/

Example response

Status 200
RESPONSE
{
"id": "string",
"status": "scheduled",
"created_at": "2019-08-24T14:15:22Z",
"status_at": "2019-08-24T14:15:22Z",
"reason": "string"
}
Status 400 The request names neither or both of `message_id` and `idempotency_key`, names one of them twice, or gives one in the wrong format: a `message_id` that isn't a UUID, or an `idempotency_key` that isn't 1 to 255 visible ASCII characters.
Status 401 The request has no project secret API key.
Status 404 No email of this project has this ID or key, or it is older than 30 days. In the first seconds after a send, it means PostHog accepted the email but hasn't recorded its first status yet.

Create messaging preferences

Opt one recipient out of topics, or back in, in one call. all_marketing applies first, then each topic in order, and the whole change is saved at once. The same change twice ends the same, but a retry that lands after a newer change undoes that change. Workflows sends skip a marketing topic the recipient opted out of. Needs a project secret API key with the messaging_preference:write scope, which cannot send email. A key with hog_flow:write also works, but that scope can send transactional email too.

Required API key scopes

messaging_preference:write

Request parameters

  • identifier
    string
  • all_marketing
    boolean
  • topics
    object

Response


Example request

POST /api/projects/:project_id/messaging/preferences
export POSTHOG_PERSONAL_API_KEY=[your personal api key]
curl
-H 'Content-Type: application/json'\
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/messaging/preferences/\
-d identifier="string"

Example response

Status 200
RESPONSE
{
"identifier": "string",
"all_marketing": true,
"topics": {
"property1": true,
"property2": true
}
}
Status 401 The request has no project secret API key.
Status 404 A topic key names no topic of this project, with the code `unknown_topic`. Nothing changed.

Create messaging send email

Send one transactional email to one recipient. Name a template made transactional in template_id, and message_data fills its {{ variables.* }} placeholders. Or send content your code rendered with a template_key, a subject and html or text: PostHog sends it as is, and its metrics and run history group under the key. Transactional emails skip opt-outs and frequency caps. Send an Idempotency-Key header to retry safely without sending the email twice. Needs the transactional_email:write scope. A key with hog_flow:write also works, but that scope reaches the rest of Workflows too.

Required API key scopes

transactional_email:write

Request parameters

  • to
    string
  • message_data
    object
  • template_id
    string
  • template_key
    string
  • distinct_id
    string | null
  • subject
    string
  • html
    string
  • text
    string
  • reply_to
    array
  • cc
  • bcc
  • attachments
    Click to open
    array | null
  • capture_engagement
    boolean
  • tracking
  • scheduled_at
    string | null
  • sensitive
    boolean
  • from
    string

Response


Example request

POST /api/projects/:project_id/messaging/send/email
export POSTHOG_PERSONAL_API_KEY=[your personal api key]
curl
-H 'Content-Type: application/json'\
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/messaging/send/email/\
-d to="string"

Example response

Status 202 Email queued
RESPONSE
{
"message_id": "string",
"replayed": true,
"warnings": [
{
"code": "sandbox_sender",
"message": "string",
"setup_url": "string",
"agent_action": "string"
}
],
"status": "queued",
"scheduled_at": "2019-08-24T14:15:22Z"
}
Status 400 A field is invalid, the template isn't transactional or its email is paused, the sender isn't verified, the recipient can't be used, the Idempotency-Key is invalid, the email is too large to send, or the project has reached its limit of template keys and the `template_key` is new. When a code send from a project with no verified sending domain would go out from the PostHog test sender, that sender refuses `cc`, `bcc` and `attachments`
Status 403 The project has no verified sending domain, and the PostHog test sender isn't available for its organization
Status 404 Template not found
Status 409 A request with the same Idempotency-Key is still in progress
Status 422 The email can't go out: the recipient is suppressed or can't receive email, sending is suspended for the project or paused for this transactional email, or the Idempotency-Key was already used for a different request, or the email would go out from the PostHog test sender to someone outside the organization
Status 429 Rate limited, or the project is over its email quota
Status 503 The email pipeline did not confirm the send