Query Metabase data in Python. Metabase rest API and Python API | by Sophia Yang | Towards Data Science

Metabase is an open-source BI tool, where you can store data, connect to outside data sources, query, and visualizing data. This article talks about when we have data stored in Metabase, how do we query the data and get results in Python. The first part of this article talks about how to use the Metabase rest API to query data. The second part tries out a Python API. Let’s get started.
To use the Metabase rest API, we first need to get a session token. In the command line type the following:
The output will be your token. Then save this token in your Python script or Jupyter Notebooks.
token = "YOUR_TOKEN"
Alternatively, you can run the following in your Python script to get the token:
Query card
There are two ways to query Metabase data. The first option is to query data from a card, or a question you asked on the UI. In the UI, we can ask a question and query the data directly through the UI:
Once we have created the question, we can check if this question shows up in the api/card:
The output shows a list of dictionaries, each of which corresponds to a question user asked through the UI. Then we can find the ID number of the card and get the result of the card with the following code. This basically queries the database with the query you defined in the card (question).
Query database directly
Instead of defining the card first and querying the card, the second option is to query the database directly.
Let’s assume we want to query the table users from the database 1 and output all the rows that are created after 2020–10–15. Here we need to define this query in the SQL native query. And then added the query to the requests.post function. The resulting res.json() is a dictionary. To read in the data and the columns. We can get all the data from res.json()['data']['row'] , and then get the column names from res.json()['data']['results_metadata']['columns'] .
Now you should be able to see the results of your query in df.
I also tried out a Python API metabasepy. Similar to the Query card section we mentioned above, here we can query card 1 with the following code:
from metabasepy import Client, MetabaseTableParsercli = Client(username=username, password=password, base_url=endpoint)cli.authenticate()query_response = cli.cards.query(card_id="1")data_table = MetabaseTableParser.get_table(metabase_response=query_response)df = pd.DataFrame(data_table.__dict__['rows'])df
I feel like the Python API is less flexible. But you are certainly welcome to try it out.
Here I showed you three ways to query data from Metabase: query from the card using Metabase rest API, query directly from the database with a user-defined query using Metabase rest API, and query from the card using a Python API metabasepy.
I prefer querying directly from the database with a user-defined query using Metabase rest API. With this method, we can have all our queries in the Python script and we know exactly what we are querying.
There are also many other API calls available in Metabase we did not cover. The code in this article should be a good starting point for you to learn and explore other API functions. Hope you enjoy it! Thanks!