Parameters are not taken into account using Metabase API

Hello,
I am using the Metabase API via python and I am trying to pass parameters to a question using tPOST /api/card/:card-id/query/:export-format. I am passing the parameters like this:

import requests

url = f'https://metabase-adress/api/card/2319/query/json'

headers = {'Content-Type': 'application/json', 'X-Metabase-Session': 'session-id'}

data = {"parameters": [{"type": "id", "target": ["dimension", ["template-tag", "org_id"]], "value": "12345"}]}

response = requests.post(url, headers=headers, data=data)

The parameters are not taken into account in the query. I used the data from the network tab in the developer tools to get the right serialized JSON for the parameters so that part should be correct. It does work using the curl command.

curl -X POST \
  -H "Content-Type: application/json" \
  -H "X-Metabase-Session: session_id" \
  -d '{"parameters": [{"type": "id","value": ["12345"],"target": ["dimension",["template-tag","master_org_id"]]}]}' \
  https://metabase-adress/api/card/2319/query/json

Any ideas?

Thanks
Marion

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

Hello @flamber
I have tried to use
data = "parameters=%5B%7B%22type%22%3A%22date%2Fall-options%22%2C%22value%22%3A%22past6months%22%2C%22target%22%3A%5B%22dimension%22%2C%5B%22template-tag%22%2C%22usage_date_range%22%5D%5D%7D%2C%7B%22type%22%3A%22id%22%2C%22value%22%3A%5B%2289351%22%5D%2C%22target%22%3A%5B%22dimension%22%2C%5B%22template-tag%22%2C%22org_id%22%5D%5D%7D%2C%7B%22type%22%3A%22string%2F%3D%22%2C%22value%22%3A%5B%22top99p%22%5D%2C%22target%22%3A%5B%22dimension%22%2C%5B%22template-tag%22%2C%22aggregation_function%22%5D%5D%7D%5D"

response = requests.post(url, headers=headers, data=json.dumps(data))

and
response = requests.post(url, headers=headers, data=data)
and
response = requests.post(url, headers=headers, json=json.dumps(data))

And it still doesn't work, any idea?

Thanks
Marion

@marionchan Use your browser. Then copy the cURL or Fetch request and compare it to what you're doing.

hello, I am running into a similar issue . I followed the adivce and used copy as cURL in browser. It looks like metabase is query the endpoint .../query whereas I am querying ../query/json. It turns out that changing the endpoint solve the problem. The value returned is accurate. But you lose the formatting of the json... Is this a behavior that someone else spotted ? or is it normal ?

To illustrate my point :

curl -X POST \
    -H "Content-Type: application/json" \
    -H "X-Metabase-Session: ${MB_TOKEN}" \
    --data-raw '{"ignore_cache":true,"parameters":[{"type":"date/single","value":"2022-01-01","target":["variable",["template-tag","start_date"]]}]}' \
    --compressed \
    https://metabase.***/api/card/${MB_QUESTION}/query/json

returns

[
{"count":19543}
]

whereas

curl -X POST \
    -H "Content-Type: application/json" \
    -H "X-Metabase-Session: ${MB_TOKEN}" \
    --data-raw '{"ignore_cache":true,"parameters":[{"type":"date/single","value":"2022-01-01","target":["variable",["template-tag","start_date"]]}]}' \
    --compressed \
    https://metabase.infra.crowdsec.net/api/card/${MB_QUESTION}/query

returns:

{"data":{"rows":[
[3037]
],
"cols": ....}

@mazzma12 Then you are doing something different. The Metabase UI uses the API, so if you are getting different results, when you're doing something via the API, then you're not doing the same thing as you're trying to do in the GUI. Please read everything here very carefully: API POST /api/card/:card-id/query/:export-format with parameters

/api/card/:card_id/query is not the same API endpoint as /api/card/:card_id/query/json
https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicardcard-idquery
https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicardcard-idqueryexport-format

Thank you @flamber, I eventually managed to do it on the query/json endpoint !!

curl -X POST \
    -H "Content-Type: application/json" \
    -H "X-Metabase-Session: ${MB_TOKEN}" \
    https://metabase*****/api/card/${MB_QUESTION}/query/json?parameters=%5B%7B%22type%22%3A%20%22date/single%22%2C%20%22value%22%3A%20%222022-01-01%22%2C%20%22target%22%3A%20%5B%22variable%22%2C%20%5B%22template-tag%22%2C%20%22start_date%22%5D%5D%7D%5D

where parameters is obtained with

import json
import urllib.parse

params = [{
    "type": "date/single",
    "value": "2022-01-01",
    "target": [
        "variable",
        [
            "template-tag",
            "start_date"
        ]
    ]
}]
tt = json.dumps({'parameters': params})
parameters = urllib.parse.quote(tt)
parameters