Access to query data from API endpoints

Does this plugin present any new API endpoints, i.e. if we wanted to access the data from the query via the API?

Probably so. See https://meta.discourse.org/t/how-to-reverse-engineer-.the-discourse-api/20576

1 Like

Yes, to get the data from a query you need to know the query’s ID. You can see the ID in the browser’s address bar when you run a query. To make an API request for the data you can do a query like this with CURL, with $api_key and $api_username set to the API credentials from your site and $query_id set to the query’s id.

curl -X POST -d "api_key=$api_key&api_username=$username" http://forum.example.com/admin/plugins/explorer/queries/$query_id/run

It looks like it’s also possible to create queries through the API. I haven’t tried that yet though.

10 Likes

So this works - and is great! Thanks.

Continuing on this line of thinking, is it possible to pass along the params for the query in this? What about also passing in the api_key and api_username so that it is one link and not curl

1 Like

Yes, it can be done. In a curl request you can pass the parameters as Form Data. Something like this will work:

curl -X POST -F "api_key=$api_key" -F "api_username=simon" -F 'params={"months_ago":"1"}'  http://localhost:3000/admin/plugins/explorer/queries/33/run
5 Likes

perfect… what about as part of the link itself and not use curl? Similar to:

http://localhost:3000/admin/reports/visits?category_id=all&end_date=2018-03-15&start_date=2018-02-14

I don’t think that can work. It has to be a POST request. If you’re running the query from another application, the easiest thing to do would be to make the request from your application’s server. That way, you should be able to avoid using curl.

2 Likes