# Access to query data from API endpoints

**URL:** https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [February 9, 2018, 11:39pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614 "2018-02-09T23:39:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [February 9, 2018, 11:39pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/1 "2018-02-09T23:39:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 9, 2018, 11:49pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/2 "2018-02-09T23:49:40Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/111):
>
> Does this plugin present any new API endpoints

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

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 9, 2018, 11:50pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/3 "2018-02-09T23:50:48Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/111):
>
> Does this plugin present any new API endpoints

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.

```plaintext
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.

---

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [March 16, 2018, 7:37pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/4 "2018-03-16T19:37:46Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/113):
>
> ```plaintext
> 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.

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

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 16, 2018, 8:30pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/5 "2018-03-16T20:30:51Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/122):
>
> is it possible to pass along the params for the query in this?

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

```plaintext
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

```

---

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [March 16, 2018, 8:44pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/6 "2018-03-16T20:44:24Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/123):
>
> Yes, it can be done. In a curl request you can pass the parameters as Form Data. Something like this will work:
> 
> ```plaintext
> 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
> 
> ```

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`

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 16, 2018, 8:52pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/7 "2018-03-16T20:52:54Z")

</div>

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566/124):
>
> what about as part of the link itself and not use curl?

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.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [September 28, 2020, 1:21pm UTC](https://meta.discourse.org/t/access-to-query-data-from-api-endpoints/90614/8 "2020-09-28T13:21:49Z")

</div>


