Missing required parameter on export API call

I have a Python code that calls upon the export API.

endpoint = "https://url.metabase.comapi/card/123/query/csv"
headers = {
    "Content-Type": "application/json",
    "X-Metabase-Session": token
}
data = json.dumps({
    "parameters": 
        [
            {
                "type": "category", 
                "target": [
                    "variable", [
                        "template-tag", "date"
                    ]
                ], 
                "value": "17-08-2020"
            }
        ]
})
resp = requests.post(
    endpoint, 
    data=data,
    headers=headers
)

The code threw an "error_type":"missing-required-parameter" on me.

I’ve tried setting a default value on the query without changing anything on the code, and the API successfully returned a .csv file with the default value as the parameter.

I’ve also tried calling the non-export query using the exact same code and the exact same question, and it successfully worked without throwing any error.

Did I do something wrong when I called the export API, or is this a bug?

Full error message:

{
   "database_id":34,
   "started_at":"2020-08-24T19:38:35.752121+07:00",
   "error_type":"missing-required-parameter",
   "json_query":{
      "constraints":null,
      "type":"native",
      "middleware":{
         "skip-results-metadata?":true
      },
      "native":{
         "query":"SELECT data FROM db WHERE campaign_name = {{campaign_name}},
         "template-tags":{
            "campaign_name":{
               "id":"blablabla",
               "name":"campaign_name",
               "display-name":"campaign_name",
               "type":"text",
               "required":false
            }
         }
      },
      "database":34,
      "parameters":null,
      "async?":true,
      "cache-ttl":null
   },
   "status":"failed",
   "class":"class clojure.lang.ExceptionInfo",
   "stacktrace":[
      "--> driver.sql.parameters.substitute$substitute.invokeStatic(substitute.clj:82)",
      "driver.sql.parameters.substitute$substitute.invoke(substitute.clj:64)",
      "driver.sql$fn__73030.invokeStatic(sql.clj:46)",
      "driver.sql$fn__73030.invoke(sql.clj:42)",
      "query_processor.middleware.parameters.native$expand_inner.invokeStatic(native.clj:39)",
      "query_processor.middleware.parameters.native$expand_inner.invoke(native.clj:30)",
      "query_processor.middleware.parameters$expand_one.invokeStatic(parameters.clj:50)",
      "query_processor.middleware.parameters$expand_one.invoke(parameters.clj:41)",
      "query_processor.middleware.parameters$expand_all$replace_43758__43759.invoke(parameters.clj:59)",
      "mbql.util.match$replace_in_collection$iter__24736__24740$fn__24741.invoke(match.clj:132)",
      "mbql.util.match$replace_in_collection.invokeStatic(match.clj:131)",
      "mbql.util.match$replace_in_collection.invoke(match.clj:126)",
      "query_processor.middleware.parameters$expand_all$replace_43758__43759.invoke(parameters.clj:59)",
      "query_processor.middleware.parameters$expand_all.invokeStatic(parameters.clj:59)",
      "query_processor.middleware.parameters$expand_all.invoke(parameters.clj:53)",
      "query_processor.middleware.parameters$expand_all.invokeStatic(parameters.clj:56)",
      "query_processor.middleware.parameters$expand_all.invoke(parameters.clj:53)",
      "query_processor.middleware.parameters$expand_parameters.invokeStatic(parameters.clj:77)",
      "query_processor.middleware.parameters$expand_parameters.invoke(parameters.clj:73)",
      "query_processor.middleware.parameters$fn__43774$substitute_parameters_STAR___43779$fn__43780.invoke(parameters.clj:82)",
      "query_processor.middleware.parameters$fn__43774$substitute_parameters_STAR___43779.invoke(parameters.clj:79)",
      "query_processor.middleware.parameters$substitute_parameters$fn__43795.invoke(parameters.clj:97)",
      "query_processor.middleware.resolve_referenced$resolve_referenced_card_resources$fn__42243.invoke(resolve_referenced.clj:80)",
      "query_processor.middleware.expand_macros$expand_macros$fn__42858.invoke(expand_macros.clj:158)",
      "query_processor.middleware.add_timezone_info$add_timezone_info$fn__39451.invoke(add_timezone_info.clj:15)",
      "query_processor.middleware.splice_params_in_response$splice_params_in_response$fn__47166.invoke(splice_params_in_response.clj:32)",
      "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__44021$fn__44025.invoke(resolve_database_and_driver.clj:33)",
      "driver$do_with_driver.invokeStatic(driver.clj:61)",
      "driver$do_with_driver.invoke(driver.clj:57)",
      "query_processor.middleware.resolve_database_and_driver$resolve_database_and_driver$fn__44021.invoke(resolve_database_and_driver.clj:27)",
      "query_processor.middleware.fetch_source_query$resolve_card_id_source_tables$fn__43064.invoke(fetch_source_query.clj:243)",
      "query_processor.middleware.store$initialize_store$fn__47175$fn__47176.invoke(store.clj:11)",
      "query_processor.store$do_with_store.invokeStatic(store.clj:46)",
      "query_processor.store$do_with_store.invoke(store.clj:40)",
      "query_processor.middleware.store$initialize_store$fn__47175.invoke(store.clj:10)",
      "query_processor.middleware.cache$maybe_return_cached_results$fn__42120.invoke(cache.clj:209)",
      "query_processor.middleware.validate$validate_query$fn__47184.invoke(validate.clj:10)",
      "query_processor.middleware.normalize_query$normalize$fn__43162.invoke(normalize_query.clj:22)",
      "query_processor.middleware.add_rows_truncated$add_rows_truncated$fn__39289.invoke(add_rows_truncated.clj:36)",
      "query_processor.middleware.results_metadata$record_and_return_metadata_BANG_$fn__47151.invoke(results_metadata.clj:124)",
      "query_processor.middleware.constraints$add_default_userland_constraints$fn__42479.invoke(constraints.clj:42)",
      "query_processor.middleware.process_userland_query$process_userland_query$fn__43884.invoke(process_userland_query.clj:136)",
      "query_processor.middleware.catch_exceptions$catch_exceptions$fn__42422.invoke(catch_exceptions.clj:174)",
      "query_processor.reducible$async_qp$qp_STAR___38090$thunk__38091.invoke(reducible.clj:101)",
      "query_processor.reducible$async_qp$qp_STAR___38090$fn__38093.invoke(reducible.clj:106)"
   ],
   "context":"json-download",
   "error":"Cannot run the query: missing required parameters: #{\\""campaign_name\\""}",
   "row_count":0,
   "running_time":0,
   "ex-data":{
      "type":"missing-required-parameter",
      "missing":[
         "campaign_name"
      ]
   },
   "data":{
      "rows":[
         
      ],
      "cols":[
         
      ]
   }
}

Hi @sagungrp
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.

As the note says on https://www.metabase.com/docs/latest/api-documentation.html#post-apicardcard-idqueryexport-format

Note that this expects the parameters as serialized JSON in the ‘parameters’ parameter

So the correct data would be:

parameters=%5B%7B%22type%22%3A%22category%22%2C%22target%22%3A%5B%22variable%22%2C%5B%22template-tag%22%2C%22date%22%5D%5D%2C%22value%22%3A%2217-08-2020%22%7D%5D