Can't pass parameters to api/card/query

Hi!

I'm working with the Metabase API and run into some issues passing parameters to different endpoints.

This works properly using the api/:card-id/query/:export-format endpoint:

params = [
        {
            "type":"category",
            "target":[
                "variable",
                [
                    "template-tag",
                    "id"
                ]
            ],
            "value":"1234"
        },
        {
            "type":"date/single",
            "target":[
                "variable",
                [
                    "template-tag",
                    "start_date"
                ]
            ],
            "value":"2021-04-01"
        }
    ]

encoded_params = str(urllib.parse.quote_plus(json.dumps(params)))
    

response = requests.post(f'https://metabase.mycompany.ai/api/card/cardId/query/json?parameters={encoded_params}',
headers=headers)

But I would rather use api/card/:card-id/query without the export-format so I can access the response with response.json()['data']['rows'] (Basically the request above returns a list of dicts while the one I want to use returns a list of lists each one representing a row)

The thing is: I understand api/car/:card-id/query receives parameters in the body of the request, but I don't seem to get it right. I've tried:

params = {"parameters": [
        {
            "type":"category",
            "target":[
                "variable",
                [
                    "template-tag",
                    "id"
                ]
            ],
            "value":"1234"
        },
        {
            "type":"date/single",
            "target":[
                "variable",
                [
                    "template-tag",
                    "start_date"
                ]
            ],
            "value":"2021-04-01"
        }
    ]}

json_params = json.dumps(params)
    

response = requests.post(https://metabase.mycompany.ai/api/card/cardId/query',
headers=headers,
json=json_params
)

And I've also tried passing the parameters as a dict in the data argument of the requests.post function.

Both ways I get a response but as if the parameters were completely ignored (basically it runs the query with its default parameters).

Please help, thanks in advance!

Edit: for clarity

Hi @marinomar
Just for reference:
https://www.metabase.com/docs/latest/api-documentation.html#post-apicardcard-idquery
What does json.dumps do? The params should be part of the body. It's different for export-format.

The best way to learn the API, is to just use Metabase while having your browser developer Network-tab open and looking at the request, and what data is being send/received.
Then you can right-click requests and copy as Fetch or cURL command, which should make it easy for you to spot the difference.

Thanks @flamber!

@marinomar Related function: get_card_data
Feel free to create a PR to achieve exactly what you are looking for.

1 Like