GitHub user dosubot[bot] added a comment to the discussion: REST API Endpoint 
for Downloading Superset Charts in CSV/Excel Format

<!-- Answer -->
To download a chart via the REST API in CSV or Excel format in Apache Superset 
(4.1.2), you should use the `/api/v1/chart/data` endpoint. This endpoint 
supports both formats by specifying the desired `result_format` in your JSON 
payload.

**API Endpoint:**
```
POST /api/v1/chart/data
```

**Required JSON Payload:**
- The payload must be a valid "query context" object.
- To export as CSV, set `"result_format": "csv"`.
- To export as Excel, set `"result_format": "xlsx"`.

**Example payload for CSV:**
```json
{
  "datasource": "1__table",
  "queries": [
    {
      "columns": ["col1", "col2"],
      "metrics": [],
      "filters": [],
      "orderby": [],
      "row_limit": 1000
    }
  ],
  "result_format": "csv",
  "result_type": "full"
}
```

**Example payload for Excel:**
```json
{
  "datasource": "1__table",
  "queries": [
    {
      "columns": ["col1", "col2"],
      "metrics": [],
      "filters": [],
      "orderby": [],
      "row_limit": 1000
    }
  ],
  "result_format": "xlsx",
  "result_type": "full"
}
```

- The structure of the `queries` array and other fields should match the 
chart's configuration. You can obtain the exact query context by inspecting 
network requests in the Superset UI when you run the chart.

**Response:**
- For single queries, the response will be a file in the requested format (CSV 
or Excel).
- For multi-query payloads, the response will be a ZIP file containing one file 
per query in the requested format.  
  
[Reference](https://github.com/apache/superset/blob/master/superset/charts/data/api.py)

---

**Regarding retrieving the full query context for a given chart:**

There is currently no official API endpoint that returns the full query context 
for a chart by its ID. The query context is constructed client-side in the 
Superset UI and sent to the backend for data requests. The backend code (e.g., 
`chart.get_query_context()`) can generate this context, but there is no exposed 
REST API for this purpose in Superset 4.1.2 or in the current codebase 
[[source](https://github.com/apache/superset/blob/master/superset/common/query_context_processor.py)].

If you need the query context, the typical approach is to retrieve it manually 
from the Superset UI (e.g., via browser dev tools when running the chart).

Let me know if you need an example tailored to a specific chart or further 
details!


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://go.dosu.dev/dosubot), mention @dosu-bot.*

---
<sup>Help Dosu learn! Give it feedback: [Great 
Response](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=great_response)
 | [Irrelevant 
Answer](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=irrelevant_answer)
 | [Incorrect 
Sources](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=incorrect_sources)
 | [Too 
Verbose](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=hallucination)
 | [Bug 
Report](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/0f4a5514-acb1-4fb5-8ecd-bf823912fa45?feedback_type=other)</sup>


GitHub link: 
https://github.com/apache/superset/discussions/33574#discussioncomment-13245285

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: 
notifications-unsubscr...@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to