skip to Main Content

i am trying to query the MongoDB Rest API using Custom HTTP Endpoints and i am getting the ‘404’ Error.

I created the following endpoint for ‘GET’ requests:

fetch('https://eu-west-2.aws.data.mongodb-api.com/app/application-0-dhdvm/endpoint/mydatabase/', {
method: 'GET',
mode: 'no-cors',
cache: 'no-cache',
headers: {
    "Content-Type": "application/json",
},

Do i have to use an API key to have access to the API ? What is missing to have access to my database ?

2

Answers


  1. Chosen as BEST ANSWER

    I am getting the 401 status code.

    I created the API key:

    mongoDB API key

    I inserted the APIkey on the Headers:

    headers: { 'Content-Type': 'application/json', 'Authorization':Bearer ${apiKey}, Authorization header },

    I am still getting the unauthorized error.


  2. If you’re trying to access a MongoDB REST API, using an API key for authentication is a common practice.

    This is a general example of how you might use an API key in your fetch request

    const apiKey = 'YOUR_API_KEY';
    
    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
        },
    })
    .then(response => {
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        return response.json();
    })
    .then(data => {
        console.log(data);
    })
    .catch(error => {
        console.error('Fetch error:', error);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search