Passing parameters to the API

Hi,

I’m trying to use this api method: https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicardcard-idqueryexport-format , but I’m serious trouble on how to format the parameters, since my question has a required template-tag.

My attempt was:

$parameters = array('parameters' => array ('municipio' => '3122306') );

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://.../api/card/" . $card_id . "/query/" . $export_format);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept: application/json', 'X-Metabase-Session: ' . $_COOKIE['x_metabase_session']));				
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($ch, CURLOPT_POST, 1);
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode($res);

Hi,

Looking at the code here https://github.com/metabase/metabase/blob/8f77cb519eb825a9a24851b2ce03062911d938d3/test/metabase/api/card_test.clj#L727 , and helped by the very helpful @jornh, I found out that parameters show be formated in a specific way.

In my case, the code that works is:

$parameters = array(
    "type" => "number", 
    "target" => array(
        "variable", 
        array("template-tag",
        "municipio")
     ),
    "value"=>"4313409"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://metabase.eita.org.br/api/card/" . $card_id . "/query/" .     $export_format . "?parameters=" . urlencode("[".json_encode($parameters) . "]"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept: application/json', 'X-Metabase-Session: ' . $_COOKIE['x_metabase_session']));				
curl_setopt($ch, CURLOPT_POST, 1);
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode($res);

I’m still feeling that I’m not doing it on the most correct way.

The question remains: is this described in the docs somewhere? If not, how can I help?

best,
alan

:tada: Great to hear you got it working!

As the API documentation is generated from the comments to functions in the /api/ folder in GitHub the best way to contribute would probably be a Pull Request adding to these comments.

And BTW it seems this specific API here does not work like many others as stated in the above wiki documentation page.

Other listed parameters (except for URL parameters like :id) should go inside the JSON in the HTTP request body.

... I wonder if that’s simply a bug?

Well, it is difficult to know if this is a bug, because I don't know how this was supposed to work. What we definitely have is documentation problem.

This quote is not wrong, but is vague an not so helpful...

Should I post this on github as an issue?

Hi, I am trying call /api/card/433/query/json with [{“type”:“category”,“target”:[“variable”,[“template-tag”,“session_id”]],“value”:“4c9188fe-b79e-416f-b432-cdff8466ce88”}],
like this one
curl -X POST
-H “Content-Type: application/json”
-H “X-Metabase-Session: <SESSION_ID>”
“https:///api/card/433/query/json?parameters=%5B%7B%22type%22%3A%22category%22%2C%22target%22%3A%5B%22variable%22%2C%5B%22template-tag%22%2C%22session_id%22%5D%5D%2C%22value%22%3A%224c9188fe-b79e-416f-b432-cdff8466ce88%22%7D%5D”. But I receive an error Illegal clause (no matching fn found): :template-tag
Can you help me?

[SOLVED]: In my case the json stayed look like this [{“type”:“category”,“target”:[“variable”,[“field-id”,6939]],“value”:"<FIRST_FILTER_VALUE>"},{“type”:“category”,“target”:[“variable”,[“field-id”,6935]],“value”:"<SECOND_FILTER_VALUE>"}]

1 Like

Hi Dimas,

Have you tried including the parameters in the URL itself?

Step 1: Get your card id. You can find this by making any question shareable. The shareable link will end with:

/public/question/8fe71c3c-b204-45a3-8eb7-946c17ef82c7

That’s your card id after ‘/question/’

Step 2: Add both the card-id, and the parameters into the URL (You can try this out by pasting the URL into any browser).

Try: "mymetabasesite.herokuapp.com/api/public/card/your-card-id/query/json?parameters=[{“type”:“category”,“target”:[“variable”,[“template-tag”,“session_id”]],“value”:“4c9188fe-b79e-416f-b432-cdff8466ce88”}]

Keep me posted if it helps

2 Likes

Hi. I’m trying to do exactly what you describe and include the parameters in the URL. Do the special characters (like {) need to be encoded? And do you know if parameters will work at all for CSV? I’m trying to ultimately have these data show up in a spreadsheet.

Thanks for any help you may be able to provide!
Brandon

That’s not how it works. Ignore the earlier response. you need to pass it using the proper integration, URL isn’t supported.

Thanks for letting me know. Can you link me to info on how to do this properly?

This worked perfectly, you use the same parameters as the query , but in the "query/json" you put them in the url, and in the plain query, you put them in the data.