How to access Metabase rest API using NodeJS

Hi Team,

I am using axios to connect to Metabase API to I can get JSON response for question id. I am using following code but it doesn’t work while getting JSON response. I am able to get the token but not able to JSON response for particular question id.

const axios = require(‘axios’);

// Call Metabase rest API to get token
axios.post(‘http://localhost:3000/api/session’,
{
username: ‘uname’,
password: ‘pwd’
})
.then(function (response) {
console.log('The token is : ’ + response.data.id);
axios.defaults.headers.post[‘Content-Type’] = ‘application/json’;
axios.defaults.headers.post[‘X-Metabase-Session’] = response.data.id;
axios.post(‘http://localhost:3000/api/card/39/query/json’)
.then((res) => {
console.log("The Metabase API to get JSON : “, res);
})
.catch((err) => {
console.log(” The error occurred while get Metabase JSON response : ", err);
})
})
.catch(function (error) {
console.log(error);
});

I either get 401 unauthorized or now I am getting status code 500

04-22 18:10:24 ERROR middleware.log :: POST /api/card/39/query/json 500 3.1 ms (3 DB calls)
{:message nil, :type java.lang.NullPointerException, :stacktrace [nil]}

Hi @roshanyadav

Just for reference: https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicardcard-idqueryexport-format

Have you tried to make sure you’re sending the exact same parameters, when using the browser? It’s the best way to learn the API.
Also, using CURL might be a simpler way to debug something.

The 401 is something to do with a wrong token used I guess. Have a look at this topic: How to access metabase api using vue js

Not sure what’s going on, when you’re seeing 500, so make sure you’re using 0.35.3 (released yesterday), otherwise you need to enable debug logging:
https://www.metabase.com/docs/latest/operations-guide/log-configuration.html