Is there a Metabase REST API that takes a MBQL/SQL query and returns the raw data?
I can perform a MBQL query via the API in a 2-step process by doing the intermediate step of creating a Question via the Metabase web app UI and then querying the Question, but I haven't figured how how to combine MBQL with the REST API in a single step.
Some items I'd like to accomplish by having the MBQL in the API request instead of a UI-generated Question:
- better version management as the MBQL query can be checked into source control with the code
- better isolation as the API call won't be dependent on the question which can change
Here's some info on how to perform the 2-step process.
2-Step Process
The two step process is:
- Use web app to create a MBQL/SQL Metabase Question
- Use REST API to query existing Question created in web app using the Card API
Step 1) Creating Question via Web UI
Log into the web app and click the "New Question" button in the top menu.
Once your question has been created you will be directed to a URL like the following where :question-id
is an integer.
- Web UI endpoint:
GET /question/:question-id
Note this value and use it in the API in the next step.
Note: an alternative for creating the card is to use the
POST /api/card
API endpoint per YakovL. This can be useful in some scenarios were UI questions/cards are desirable, but I'm also trying to avoid creating creating cards / questions in the first place, since I'm not planning on using the Metabase UI to consume them. Reasons to avoid cards for me include needing to perform extra work to verify the card query definitions haven't changed but still having the SQL in the code to create the cards, and generate a lot of unneeded question cards in the UI.
Step 2) REST API for Question Data
The API uses the term "card" for the Web UI "question" object, so make an API call to the following Card API:
- API endpoint:
POST /api/card/:card-id/query/:export-format
In this URL:
:card-id
is the:question-id
from the Web UI URL:export-format
can bejson
or another format
More information on the API is available in the API Documentation:
https://github.com/metabase/metabase/blob/master/docs/api-documentation.md
Question
Is there a way to do this directly by sending the MBQL/SQL query in the API request in a single step without a pre-existing Question/Card?
GET /api/embed/card/:token/query
endpoint and had hard times figuring where to get the token from. Really appreciated! – KnowledgeablePOST /api/card/
endpoint? It is described to create a new card, so probably you'll be able to do this in 2 steps, but both via API – Knowledgeable