This guide explains how to use the Discourse API to create, run, and manage queries with the Data Explorer plugin.
Required user level: Administrator
Virtually any action that can be performed through the Discourse user interface can also be triggered with the Discourse API.
This document provides a comprehensive overview for utilizing the API specifically in conjunction with the Data Explorer plugin.
For a general overview of how to find the correct API request for an action, see: Reverse engineer the Discourse API.
Running a Data Explorer query
To run a Data Explorer query via the API, make a POST
request to /admin/plugins/explorer/queries/<query-id>/run
. You can find the query ID by visiting it through your Discourse site and checking the id
parameter in the address bar.
Below is an example query with an ID of 20
that returns topics by views on a specified date:
--[params]
-- date :viewed_at
SELECT
topic_id,
COUNT(1) AS views_for_date
FROM topic_views
WHERE viewed_at = :viewed_at
GROUP BY topic_id
ORDER BY views_for_date DESC
This query can be run from a terminal with:
curl -X POST "https://your-site-url/admin/plugins/explorer/queries/20/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"viewed_at":"2019-06-10"}'
Note that youâll need to replace the <api-key>
, <your-site-url>
with your API key and domain.
Creating a query via the API
To create a Data Explorer query via the API, youâll need to make a POST
request to /admin/plugins/explorer/queries
.
You will also need to specify the query name
and sql
to use for the new query in the API call.
Below is an example of how to create a new query using the API:
curl -X POST "https://your-site-url/admin/plugins/explorer/queries" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: <username>" \
-F 'query[name]=Example Query' \
-F 'query[sql]=SELECT COUNT(*) FROM users'
This API call will return a response like:
{"query":{"id":49,"name":"Example Query","description":null,"username":"<username>","group_ids":[],"last_run_at":null,"user_id":1,"sql":"SELECT COUNT(*) FROM users","param_info":[],"created_at":"2025-03-13T18:41:44.226Z","hidden":false}}%
You can then run the query by using the query ID from the response (in this case, 49
).
Returned results will be structured within the rows field.
Handling large datasets
The Data Explorer plugin limits results to 1000 rows by default. To paginate through larger datasets, you can use the example query below:
--[params]
-- integer :limit = 100
-- integer :page = 0
SELECT *
FROM generate_series(1, 10000)
OFFSET :page * :limit
LIMIT :limit
To fetch the results page-by-page, increment the page
parameter in the request:
curl -X POST "https://your-site-url/admin/plugins/explorer/queries/27/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"page":"0"}'
Stop when result_count
is zero.
For additional information about handling large datasets, see: Result Limits and Exporting Queries
Removing relations
data from the results
When Data Explorer queries are run through the user interface, a relations
object is added to the results. This data is used for rendering the user in UI results, but you are unlikely to need it when running queries via the API.
To remove that data from the results, add a download=true
parameter with your request:
curl -X POST "https://your-site-url/admin/plugins/explorer/queries/27/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"page":"0"}' \
-F "download=true"
API authentication
Details about generating an API key for the requests can be found here: Create and configure an API key.
If the API key is only going to be used to run Data Explorer queries, you can select âGranularâ from the Scope drop down menu, then select the ârun queriesâ scope.
FAQs
Is there any api endpoint I can use to get the list of reports and the ID numbers? I want to build a dropdown with the list in it?
Yes, you can make an authenticated GET request to /admin/plugins/explorer/queries.json
to get a list of all queries on the site.
Is it possible to send parameters with the post request?
Yes, include SQL parameters using the -F
option, as shown in the examples.
Is CSV export for queries supported by the API?
While JSON output is standard, you can manually convert results to CSV. Native CSV export is no longer supported.