0

I am getting the following error when trying to fetch an API key from Spoonacular:

code: 401
message: "You are not authorized. Please read https://spoonacular.com/food-api/docs#Authentication"
status: "failure"

I have tried closing the browser and restarting it using npm start but still get the error. I have also tried a different API key from Spoonacular but still get the same error. I have also tried clearing my browser Cache but still get the same error.

Please advise on how I can fix this error.

My code:

import { useEffect } from 'react';

function Popular() {
  useEffect(() => {
    getPopular();
  }, []);

  const getPopular = async () => {
    const api = await fetch(
      'https://api.spoonacular.com/recipes/random?apiKey=${process.env.REACT_APP_API_KEY}&NUMBER=9'
    );
    const data = await api.json();                 
    console.log(data);
  };
  return <div>Popular</div>;
}

export default Popular;

6 Answers 6

1

You need to use template literals with backsticks ` .

Wrong:

const api = await fetch(
      'https://api.spoonacular.com/recipes/random?apiKey=${process.env.REACT_APP_API_KEY}&NUMBER=9'
    );

Correct

const api = await fetch(
      `https://api.spoonacular.com/recipes/random?apiKey=${process.env.REACT_APP_API_KEY}&NUMBER=9`
    );
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ali, thank you for your help. I have tried using literals with backsticks and even tried copying your code and using it but still get the same error
@Lizzy can you check in the Network tab in the console what is the url that you are calling?
I think it is this but not sure if I am sending you the right thing? api.spoonacular.com/recipes/…
0

check if your env file is in the root folder and not in src folder Also don't give any space or , while asigning the API key in env file.

for me that was the mistake.

Comments

0

You should always call the API in Try catch, so that you can log the error and check if there is an error. 401 Status Unauthorized you must pass a Authorization token in the API 'Headers'.

const getPopular = async () => {
    const api = await fetch(
      `https://api.spoonacular.com/recipes/random?apiKey=${process.env.REACT_APP_API_KEY}&NUMBER=9`, {
  headers: {
    'Authorization': // pass your token here
    }
     }
    );
    try {
      const data = await api.json();                 
      console.log(data);
    } catch (error) {
       console.log(error.response) 
    }
  };

3 Comments

Hi Ahmad, thank you for your help. Where do I get an authorization token from?
You get authorization token from login API. :)
I am very new to all this. Could you please list the steps to obtaining the authorization token? When I google spoonacular API login I can't find much on it.
0

Check if your env file is in root folder. I deleted the .env file and created it again and it worked.

Comments

0

Try this this; it works for me:

import { useEffect } from "react";

const Popular = () => {
  useEffect(() => {
    getpopular();
  }, []);

  const getpopular = async () => {
    const api = await fetch(
      `https://api.spoonacular.com/recipes/random?number=10&apiKey=YOUR API KEY`
    );
    const data = await api.json();
    console.log(data);
  };
  return <div>Popular</div>;
};

export default Popular;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

please add the "apiKey" parameter at the end of your API endpoint. Be sure to prefix it with "?".

2 Comments

Welcome to StackOverflow. Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem, make the additional insight more obvious which you contribute beyond existing answers, the relevant advantage which your solution achieves, especially if the difference is so small and non-obvious. Consider taking the tour.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.