Cannot Query MongoDB API using HTTP's requests

Hello, i am trying to query the MongoDB API using the API key as the authentication method, but i keep getting the 401 status code.

Here is the code:

fetch('https://eu-west-2.aws.data.mongodb-api.com/app/application-0-dhdvm/endpoint/mydatabase', {
    method: 'GET',
    mode: 'cors',
    cache: 'no-cache',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`,  // Include your API key in the Authorization header
    },
})

I generated the API KEY:

I created the following EndPoint: https://eu-west-2.aws.data.mongodb-api.com/app/application-0-dhdvm/endpoint/mydatabase

Hi @Andre_Silveira,

Welcome to the MongoDB Community :sparkles:

The 401 error might be due to an invalid API key or if the IP hasn’t been added to the access list. Please refer to the documentation to read more about it.

You can further refer to the Atlas API Error codes to identify the specific error being thrown in your case.

Regards,
Kushagra