Query

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

Endpoints

POST
GET
DELETE
GET
POST
POST
GET
GET
POST

Create query

This is the main endpoint for querying data from PostHog. You can find all the details on what it does and how you can use it in our API queries doc.

If you don't want to read that, here's an example of how to use it to get events where the $current_url contains blog using the HogQLQuery query type and SQL.

Tip: Always include a descriptive name parameter in your queries. This makes them easier to identify and analyze in the query_log table.

curl \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
<ph_app_host>/api/projects/:project_id/query/ \
-d '{
"query": {
"kind": "HogQLQuery",
"query": "select properties.$current_url from events where properties.$current_url like '\''%/blog%'\'' limit 100"
},
"name": "get 100 blog urls"
}'

Again, more details about this endpoint are available in our API queries doc.

Required API key scopes

query:read

Request parameters

  • async
  • client_query_id
  • filters_override
  • limit_context
  • name
  • query
  • refresh
    Default: blocking
  • variables_override

Example request

POST /api/projects/:project_id/query
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/query/\
-d query=undefined

Example response

Status 200
RESPONSE
{}

Retrieve query

(Experimental)

Required API key scopes

query:read

Path parameters

  • id
    string

Response


Example request

GET /api/projects/:project_id/query/: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/query/:id/

Example response

Status 200
RESPONSE
{
"query_status": {
"budget_remaining_bytes": null,
"bytes_read": null,
"cache_key": null,
"complete": false,
"dashboard_id": null,
"end_time": null,
"error": false,
"error_code": null,
"error_message": null,
"expiration_time": null,
"id": "string",
"insight_id": null,
"labels": null,
"pickup_time": null,
"query_async": true,
"query_progress": null,
"query_scan": null,
"results": null,
"start_time": null,
"task_id": null,
"team_id": 0
}
}

Delete query

(Experimental)

Required API key scopes

query:read

Path parameters

  • id
    string

Example request

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

Example response

Status 204 Query cancelled

Retrieve query log

Get query log details from query_log_archive table for a specific query_id, the query must have been issued in last 24 hours.

Path parameters

  • id
    string

Example request

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

Example response

Status 200

Create query create with

Path parameters

  • query_kind
    string

Example request

POST /api/projects/:project_id/query/:query_kind
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/query/:query_kind/

Example response

Status 200 No response body

Create query check auth for async

Example request

POST /api/projects/:project_id/query/check_auth_for_async
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/query/check_auth_for_async/

Example response

Status 200

Retrieve query draft sql

Example request

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

Example response

Status 200

Retrieve query scan

Get the query scan for a cache key: the analysis of the last slow run of that query. The body is empty while the analysis runs. 404 when the query has not been analyzed.

Required API key scopes

query:read

Path parameters

  • cache_key
    string

Response


Example request

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

Example response

Status 200
RESPONSE
{
"analysis": null
}
Status 404 No query scan exists for this cache key.

Create query upgrade

Upgrades a query without executing it. Returns a query with all nodes migrated to the latest version.

Request parameters

  • query

Response


Example request

POST /api/projects/:project_id/query/upgrade
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/query/upgrade/\
-d query=undefined

Example response

Status 200
RESPONSE
{
"query": {
"custom_name": null,
"event": null,
"fixedProperties": null,
"kind": "EventsNode",
"limit": null,
"math": null,
"math_group_type_index": null,
"math_hogql": null,
"math_multiplier": null,
"math_property": null,
"math_property_revenue_currency": null,
"math_property_type": null,
"name": null,
"optionalInFunnel": null,
"orderBy": null,
"properties": null,
"response": null,
"version": null
}
}