Airtable query metabase api

hey

Im trying to write a script in airtable that will query metabase. script is triggered directly in airtable and the goal is to have it dynamic, so ill have variables in the query that ill be passing from airtable, and i want to receive an output based on that.


const METABASE_API_URL = 'AAAAAAAAAAA/api/card/BBBBBBBB/query';
const METABASE_TOKEN = 'CCCCCCCCCCCCCCC';

async function fetchMetabaseData() {
try {
const queryPayload = JSON.stringify({
type: 'native',
native: {
query: "SELECT id FROM bookings WHERE client_id = '102827'"
}
});

    const headers = {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${METABASE_TOKEN}`
    };

    const response = await fetch(METABASE_API_URL, {
        method: 'POST',
        headers: headers,
        body: queryPayload  // The body must be a string
    });

    if (!response.ok) {
        console.error('HTTP Error:', response.status, await response.text());
        throw new Error('Failed to fetch data from Metabase: ' + response.statusText);
    }

    const data = await response.json();

    if (data && data.data.length > 0) {
        let bookingId = data.data[0].id;
        console.log('Booking ID:', bookingId);
    } else {
        console.log('No bookings found for the specified client_id.');
    }
} catch (error) {
    console.error('Error fetching data from Metabase:', error);
}

}

fetchMetabaseData();


AAAAAA- company name
BBBBBB-question id
CCCCCC-token (generated with postman)

i think im missing indicating which database i want to query, but where should i add it in the script?

the script itself is running successfully but the console is returning:

"HTTP Error:" 401 "Unauthenticated"
"Error fetching data from Metabase:" Error {name: "Error"}.

there is another way to do it, through make.com,
but the problem in there is that its asking me to add query in 'valid json string' , when i use the converters sql to json its not working

please help, im really stuck with this

You seem to have skipped the authentication phase: