Create a Data Explorer query using the API

AI-generated summary

The official guide by Discourse outlines how to create Data Explorer queries via the API, requiring an administrator with a Global scope API key. The documentation specifies the endpoint /admin/plugins/discourse-data-explorer/queries and details necessary parameters like query[name] and query[sql].

However, users pfaffman and murphyke reported significant issues stemming from recent changes. pfaffman noted that a path change broke their existing GitHub actions, which previously relied on /admin/plugins/explorer/queries/. Additionally, murphyke highlighted that their tooling broke due to the new requirement for a Global API key, replacing the previously sufficient Read-only scope. pfaffman agreed that the path change was annoying and characterized the API key restriction as a potential bug.

Create a Data Explorer query with the API

:bookmark: This how-to guide explains how to create a Data Explorer query using the Discourse API.

:person_raising_hand: Required user level: Administrator

Prerequisites

Before you begin, ensure you have:

:information_source: Creating queries requires a global API key. The granular “run queries” scope only allows running existing queries, not creating new ones.

Create a query

To create a new Data Explorer query, make a POST request to /admin/plugins/discourse-data-explorer/queries.

Parameters

Parameter Required Description
query[name] Yes The name for your query
query[sql] Yes The SQL statement to execute
query[description] No A description of what the query does
query[group_ids] No Array of group IDs that can access this query

Example request

curl -X POST "https://your-site.com/admin/plugins/discourse-data-explorer/queries" \
  -H "Content-Type: multipart/form-data;" \
  -H "Api-Key: <api-key>" \
  -H "Api-Username: <username>" \
  -F 'query[name]=User count query' \
  -F 'query[sql]=SELECT COUNT(*) FROM users'

Example response

{
  "query": {
    "id": 49,
    "name": "User count query",
    "description": null,
    "username": "<username>",
    "group_ids": [],
    "last_run_at": "2025-03-13T18:41:44.226Z",
    "user_id": 1,
    "is_default": false,
    "sql": "SELECT COUNT(*) FROM users",
    "param_info": [],
    "created_at": "2025-03-13T18:41:44.226Z",
    "hidden": false
  }
}

Note the id value in the response, you’ll need this to run the query. For details on running queries via the API, see Run Data Explorer queries with the Discourse API.

Last edited by @MarkDoerr 2026-02-25T00:36:21Z

Last checked by @MarkDoerr 2026-01-26T23:29:25Z

Check documentPerform check on document:
6 Likes

You changed the path of the API call and broke my query!?! it used to be /admin/plugins/explorer/queries/3/run and all of a sudden the github action started failing. This doesn’t seem very nice.

3 Likes

Our tooling broke because of that path change and also the change to require a Global API key instead of a Read-only API key, which used to be sufficient.

That was annoying.

That sounds like a bug.