Accessing card variables through Metabase API in Python

Hi, I've tried to look through the documentation but there honestly isn't a great description as to what parameters does Metabase API documentation. (it just says I can pass parameters onto it really, no description/example as to how this would look.)

I've looked through links like this which tell me to go here but I'm still getting the same issue: that being that the full card data is returned regardless of what variables I'm trying to account for.

    data =  {'paramaters': [
        {
            'type': 'category',
            'target': [
                'variable', 
                [
                    'template-tag', 
                    'agent'
                ]
            ], 
            'value': 'test'
        }
    ]}
    url = self.api_endpoint + f"/api/card/{cardid}/query"
    resp = requests_retry_session().post(
        url, headers=self.api_headers, data=data)

The code above is what I'm using to try to post. I've tried every variation I can think of, I've urlencoded the data, put it in the params argument, etc. None work. Does anyone have a good example or description for doing this in Python? Regardless of how I format it, I'm still able to get the card response, but my request body is ignored entirely.

Hi @zsmith2
Your problem is paramaters should be parameters.
I would recommend having a look at this: https://github.com/vvaezian/metabase_api_python

1 Like

LOL, I feel a little dumb having misspelled that. However in testing I'm still getting the same result- that being that it's ignoring the request body and just giving me the full results (or whatever the default is, i.e if I set agent to equal x on the card it'll only return the row where agent = x and still ignore my request body). I glanced over that github link a bit, but it doesn't seem like they're accessing card variables when they're trying to run queries associated with a card.

Actually just got it to work - changing parameters to the correct spelling and switching from the form-encoded data parameter to the json-encoded json parameter in my post request fixed it. I guess when I was trying every variation I still had the misspelling (swear I spelled it right the first time :laughing: ).

Thanks @flamber for noticing my misspelling :slight_smile:

data = {'parameters': [

        {

            'type': 'category',

            'target': [

                'variable', 

                [

                    'template-tag', 

                    'agent'

                ]

            ], 

            'value': 'test'

        }

    ]}

    url = self.api_endpoint + f"/api/card/{cardid}/query"

    resp = requests_retry_session().post(

        url, headers=self.api_headers, json=data)

Hey there.
I'm trying to pass the payload source code with a bunch of filters into a POST card/query request but it seems to ignore the filters.

Also, my source code seems to be in a different order or scheme than @zsmith2 for exemple.

As for the Documentation, they also do not detail what parameters or scheme should I use in POST /api/card/:card-id/query

I'm using Python BTW
Can someone please assist?

So using exactly the same method as above this works perfectly for the card url
which is great so i get some json back however if i want to use the
`/api/card/:card-id/query/csv
it breaks again.
Has anyone used this successfully with /api/card/:card-id/query/json or /api/card/:card-id/query/csv

Blockquote
data = {'parameters': [
{
'type': 'date/single',
'target': [
'variable',
[
'template-tag',
'effective_date'
]
],
'value': yesterday
}
]}

@locose91 Have a look here: API POST /api/card/:card-id/query/:export-format with parameters

I have several times and looked at your reponses / tried to change the json for the /csv endpoint to what you suggested but nothing.
Also when i download the csv like you suggested near the end of that post nothing happens in network tab (that i can see) also the download link is stripped off all the parameters so can view the exact request it is sending

csv_data = parameters = [
{"type":  "date/single",
 "target":
     ["variable", ["template-tag", "effective_date"]],
 "value": yesterday}]

csv_query_url = "https://metabase.url/api/card/24/query/csv"
csv_response = requests.post(csv_query_url, json=csv_data, headers=headers)

@locose91
Post "Diagnostic Info" from Admin > Troubleshooting.
Make sure that your Site URL is actually correct in Admin > Settings > General.
Then copy the cURL request from the browser developer Network, which will show exactly what you need to send.

Have you tried using https://github.com/vvaezian/metabase_api_python instead?