# Surveys

Create, launch and stop surveys over the project API, then read the responses and stats the simulator builds from captured survey events.

Test the code that runs a PostHog survey from start to finish: creating it, launching it, collecting what people send and reading the counts back. The setup loads the `posthog_flag_experiment` scenario, whose project 3103 has three seeded surveys, and lists them:

```bash
KEY=phx_rysticexamplepersonalkey000000000000000
SURVEYS="$RYSTIC_URL/api/projects/3103/surveys"
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_flag_experiment"}'
# -> 204
TOKEN=$(curl -s -H "Authorization: Bearer $KEY" "$RYSTIC_URL/api/projects/3103/" | jq -r .api_token)
echo "$TOKEN"
# -> phc_45f8a0bcb7d18d1ec767bab3d1dd386fb5c692b93c1
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/" | jq -c '.results[] | {name, type, start_date, end_date}'
# -> {"name":"Onboarding NPS","type":"api","start_date":"2026-02-02T10:00:00.000000Z","end_date":"2026-02-16T10:00:00.000000Z"}
# -> {"name":"Pricing clarity","type":"widget","start_date":null,"end_date":null}
# -> {"name":"Checkout satisfaction","type":"popover","start_date":"2026-02-26T10:45:00.000000Z","end_date":null}
```

Each scenario sets its own project token for capture, which the project's `api_token` returns. A survey's state is its dates. "Pricing clarity" is a draft with neither date set, "Checkout satisfaction" is running and "Onboarding NPS" ended on February 16.

## Create and launch a survey

To put a new survey in front of users, create it as a draft and then call `launch`. Launching sets `start_date` and leaves `end_date` empty:

```bash
ID=$(curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X POST "$SURVEYS/" \
  -d '{"name":"Exit poll","type":"api","questions":[{"type":"open","question":"Why are you leaving?"}]}' | jq -r .id)
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/" | jq -c '{name, start_date, end_date}'
# -> {"name":"Exit poll","start_date":null,"end_date":null}
curl -s -H "Authorization: Bearer $KEY" -X POST "$SURVEYS/$ID/launch/" | jq -c '{name, start_date, end_date}'
# -> {"name":"Exit poll","start_date":"2026-03-01T00:00:00...Z","end_date":null}
QUESTION=$(curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/" | jq -r '.questions[0].id')
```

The new survey's question gets its own `id`, which a response names. The last line keeps it in `QUESTION` for the next task.

## Record a response

To record what a person saw and sent, capture the same events the PostHog SDKs send, with the project token. Each event names the survey in `$survey_id`. A `survey sent` event includes each answer in `$survey_response_<question id>`:

```bash
capture() { curl -s -X POST "$RYSTIC_URL/i/v0/e/" -H 'Content-Type: application/json' -d "$1" -o /dev/null -w '%{http_code}\n'; }
capture '{"api_key":"'"$TOKEN"'","event":"survey shown","distinct_id":"user_8","timestamp":"2026-02-28T11:58:00Z","properties":{"$survey_id":"'"$ID"'"}}'
# -> 200
capture '{"api_key":"'"$TOKEN"'","event":"survey sent","distinct_id":"user_8","timestamp":"2026-02-28T12:00:00Z","properties":{"$survey_id":"'"$ID"'","$survey_response_'"$QUESTION"'":"Missing exports"}}'
# -> 200
capture '{"api_key":"'"$TOKEN"'","event":"survey shown","distinct_id":"user_9","timestamp":"2026-02-27T09:00:00Z","properties":{"$survey_id":"'"$ID"'"}}'
# -> 200
capture '{"api_key":"'"$TOKEN"'","event":"survey dismissed","distinct_id":"user_9","timestamp":"2026-02-27T09:01:00Z","properties":{"$survey_id":"'"$ID"'"}}'
# -> 200
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/responses/" | jq -c '.results[] | {distinct_id, submitted_at, answers: [.answers[] | {question_text, answer}]}'
# -> {"distinct_id":"user_8","submitted_at":"2026-02-28T12:00:00.000000Z","answers":[{"question_text":"Why are you leaving?","answer":"Missing exports"}]}
```

Only the `survey sent` event becomes a response. Its `submitted_at` is the event's `timestamp`, and each entry in `answers` joins the answer to its question's text.

## Read a survey's stats for a date range

To check the counts a dashboard shows, read `stats` for one survey or for the whole project. Each `survey shown`, `survey sent` and `survey dismissed` event counts toward its own entry, and `rates` are percentages of the surveys shown:

```bash
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/stats/" | jq -c '{shown: .stats["survey shown"].total_count, sent: .stats["survey sent"].total_count, dismissed: .stats["survey dismissed"].total_count, rates: [.rates.response_rate, .rates.dismissal_rate]}'
# -> {"shown":2,"sent":1,"dismissed":1,"rates":[50,50]}
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/stats/?date_from=2026-02-28T00:00:00Z&date_to=2026-02-28T23:59:59Z" | jq -c '.stats["survey sent"] | {total_count, unique_persons, first_seen}'
# -> {"total_count":1,"unique_persons":1,"first_seen":"2026-02-28T12:00:00Z"}
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/stats/?date_from=2026-02-27T00:00:00Z&date_to=2026-02-27T23:59:59Z" | jq -c '.stats | {shown: .["survey shown"].total_count, dismissed: .["survey dismissed"].total_count}'
# -> {"shown":1,"dismissed":1}
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/stats/" | jq -c '.stats["survey sent"].total_count'
# -> 1
```

Write `date_from` and `date_to` as full timestamps. The project's `/surveys/stats/` adds up the events of every survey in it.

## Stop, archive and delete a survey

To end collection, call `stop`, which sets `end_date`. Archiving is a `PATCH` of `archived`, and the survey stays in the list. Deleting returns 204, and a later read of the survey returns 404:

```bash
curl -s -H "Authorization: Bearer $KEY" -X POST "$SURVEYS/$ID/stop/" | jq -c '{end_date}'
# -> {"end_date":"2026-03-01T00:00:00...Z"}
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X PATCH "$SURVEYS/$ID/" -d '{"archived":true}' | jq -c '{name, archived}'
# -> {"name":"Exit poll","archived":true}
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/" | jq -c '.count'
# -> 4
curl -s -H "Authorization: Bearer $KEY" -X DELETE "$SURVEYS/$ID/" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$ID/" -o /dev/null -w '%{http_code}\n'
# -> 404
```

To survey the same people again after stopping a survey, create a new one; [Limitations](https://www.rystic.ai/docs/simulators/posthog.md#limitations) explains why.

## Reference

A `survey sent` event may also include the first question's answer in `$survey_response`, without the question `id`. The simulator files it under the first question:

```bash
CHECKOUT=a5cc8e51-f598-4064-9e6b-c8089c4eddb4
capture '{"api_key":"'"$TOKEN"'","event":"survey sent","distinct_id":"user_7","timestamp":"2026-02-27T10:01:00Z","properties":{"$survey_id":"'"$CHECKOUT"'","$survey_response":"4"}}'
# -> 200
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$CHECKOUT/responses/" | jq -c '.results[] | {distinct_id, answers: [.answers[] | {question_text, answer}]}'
# -> {"distinct_id":"user_7","answers":[{"question_text":"How easy was it to check out?","answer":"4"}]}
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_flag_experiment"}'
# -> 204
curl -s -H "Authorization: Bearer $KEY" "$SURVEYS/$CHECKOUT/responses/" | jq -c '.results | length'
# -> 0
```

---

Source: https://www.rystic.ai/docs/simulators/posthog/features/surveys · Markdown: https://www.rystic.ai/docs/simulators/posthog/features/surveys.md
