Query

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

Endpoints

POST
GET
DELETE
GET
POST
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

Path parameters

  • project_id
    string

Request parameters

  • async
    boolean
  • client_query_id
    string
  • filters_override
  • name
    string
  • query
  • refresh
    Default: blocking
  • variables_override
    object

Response


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
  • project_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": {
"complete": false,
"dashboard_id": null,
"end_time": null,
"error": false,
"error_message": null,
"expiration_time": null,
"id": "string",
"insight_id": null,
"labels": null,
"pickup_time": null,
"query_async": true,
"query_progress": {
"active_cpu_time": 0,
"bytes_read": 0,
"estimated_rows_total": 0,
"rows_read": 0,
"time_elapsed": 0
},
"results": null,
"start_time": null,
"task_id": null,
"team_id": 0
}
}

Delete query

(Experimental)

Required API key scopes

query:read

Path parameters

  • id
    string
  • project_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
  • project_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 check auth for async

Path parameters

  • project_id
    string

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 No response body

Retrieve query draft sql

Path parameters

  • project_id
    string

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 No response body

Create query upgrade

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

Path parameters

  • project_id
    string

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": 0,
"math_hogql": null,
"math_multiplier": null,
"math_property": null,
"math_property_revenue_currency": {
"property": null,
"static": "AED"
},
"math_property_type": null,
"name": null,
"optionalInFunnel": null,
"orderBy": null,
"properties": null,
"response": null,
"version": null
}
}

Community questions

Questions about this page? or post a community question.