Problems trying to export dataset via API

Hi, I have been trying to get the /dataset/csv endpoint to work.
I've tried the following:

curl --location --request POST 'https://metbase-domain.example.com/api/dataset/csv' \
--header 'X-Metabase-Session: GENERATED_TOKEN_HERE' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": "{\"database\":1,\"type\":\"native\",\"native\":{\"query\":\"SELECT * FROM users LIMIT 10\"}}"
}'

Which returned:

"errors": {
    "query": "value must be a valid JSON string."
}

and

curl --location --request POST 'https://metbase-domain.example.com/api/dataset/csv' \
--header 'X-Metabase-Session: GENERATED_TOKEN_HERE' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'query={"database":1,"type":"native","native":{"query":"SELECT * FROM users LIMIT 10"}}'

Which returned 403 "You don't have permissions to do that."

What am I missing here?

Hi @apiissues
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 copy the request as cURL and look at the difference between that and your example.

1 Like

That's amazing! I wasn't aware of the fact that the API is used by the frontend client.
For anyone looking at this in the future, this final curl call ended up working:

curl --location --request POST 'https://metbase-domain.example.com/api/dataset/csv' \
--header 'X-Metabase-Session: GENERATED_TOKEN_HERE' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'query={"database":1,"type":"native","native":{"query":"SELECT * FROM `users` LIMIT 10"}}'