# Discourse Data Explorer SQL Query

**URL:** https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351
**Category:** Support
**Tags:** data-explorer
**Created:** [March 15, 2025, 1:26am UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351 "2025-03-15T01:26:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JohnMcDonald](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@JohnMcDonald](https://meta.discourse.org/u/JohnMcDonald)
#### Post date: [March 15, 2025, 1:26am UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351/1 "2025-03-15T01:26:40Z")

</div>

I have a question that is very similar to (but different from) the question posted here: [`DataExplorer::ValidationError: Missing parameter` when running Data Explorer queries with [params] via API](https://meta.discourse.org/t/dataexplorer-missing-parameter-when-running-data-explorer-queries-with-params-via-api/223812).

My query in [Data Explorer](https://meta.discourse.org/t/32566?silent=true) looks like this:

```plaintext
-- [params]
-- topic_id :topic_id

SELECT * FROM posts WHERE topic_id = :topic_id

```

This works fine in [Data Explorer](https://meta.discourse.org/t/32566?silent=true). Then, when I try and run it via the API I get:

```plaintext
{"success":false,"errors":["DiscourseDataExplorer::ValidationError: Missing parameter topic_id of type topic_id"]}'

```

Here is my python request:

```plaintext

headers = {
    'Content-Type': 'multipart/form-data;',
    'Api-Key': API_KEY,
    'Api-Username': USERNAME_SYSTEM
}

params = {"topic_id": 398}

response = requests.post(url="[REDACTED]/admin/plugins/explorer/queries/10/run", headers=headers, params=params)

```

I gather from the error than I cannot pass “topic\_id” as a string, but I don’t see how else I can pass it as a key. Any thoughts on this?

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [March 15, 2025, 3:23am UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351/2 "2025-03-15T03:23:39Z")

</div>

The parameters are not passed in the URL.

Maybe something like: `data={“params”: ‘{“topic_id”:"398"}’`?

---

<div class="post-metadata">

### Author: ![JohnMcDonald](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@JohnMcDonald](https://meta.discourse.org/u/JohnMcDonald)
#### Post date: [March 15, 2025, 4:39pm UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351/3 "2025-03-15T16:39:00Z")

</div>

I had to try a lot of different alternatives, but this worked:

````response
                         headers=headers, data={'params': '{\"topic_id\": "398"}'})```

So, the answer is a combination of that prior post (which said you have to escape the quotes when you are sending from a Windows machine and Arkshine's post which says it needs to be passed as data and not as a params kwarg. What I find odd though, is I thought the whole point of the params kwarg in the Python requests module was to pass parameter information.
````

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [March 15, 2025, 5:24pm UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351/4 "2025-03-15T17:24:53Z")

</div>

I’m glad it worked!

I tried sending Linux (wsl) or Windows; I don’t have the same issue, and it worked for both. 🤔

```python
data = {
    'params': '{"topic_id": "3"}'
}

response = requests.post(
    url="http://localhost:4200/admin/plugins/explorer/queries/4/run", 
    headers=headers, 
    data=data
)

```

![image](https://global.discourse-cdn.com/meta/original/4X/b/6/2/b62617e1e32a5c7a966ae201f05457d16ce7ee7d.png)

---

<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: [April 14, 2025, 5:25pm UTC](https://meta.discourse.org/t/discourse-data-explorer-sql-query/357351/5 "2025-04-14T17:25:22Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
