How to fetch API data in React?

How to fetch API data in React?
0

I am wanting to console.log data from this API when I hit the search button. I am just starting out with React and so I’m not entirely sure how to this.

import React, {useEffect, useState} from'react';
import Recipes from './Recipes.js';
import './App.css';



function App() {
const API_KEY = process.env.REACT_APP_API_KEY;
const API_ID = process.env.REACT_APP_API_ID;


const getRecipes = async() => {
  const response = await fetch(
    `https://api.edamam.com/search?q=chicken&app_id=${API_ID}&app_key=${API_KEY}`
  )
  const data = await response.json();
  console.log(data)
}


  return(
    <>
    <Recipes />
    <div className="container">
      <input className="input" type="text"></input>
      <button onClick={getRecipes} className="search-button">Search</button>
    </div>
    </>
  )
}

export default App;

What are you getting when you run this? I assume there’s some sort of render error, as there’s no render() in your component?

@snowmonkey I assume render is in an index.js or something.

@fkhan698 what are you getting back? There doesn’t seem to be anything wrong with what you have, so you might have to check the API docs if it isn’t giving you what you want.

I am getting this error

I also have the render in my index.js

You can try using a cors proxy, also the API key and APP id seems to not be coming through. Are you setting them in an .env file?

Yeah I already had my .env file set up like this

REACT_APP_API_KEY = #API Key
REACT_APP_API_ID = #API ID

Does it work if you hardcode the key and id into the string you are using in fetch?

Thats actually what I just tried to do. I get an error message saying (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Is that with or without the cors proxy?

Edit: If you just put the URL into the browser do you at least get the json back?

https://api.edamam.com/search?q=chicken&app_id=ApplicationID&app_key=ApplicationKeys

@fkhan698 I just tested the API locally and you do not need the proxy. Make sure the .env file is in the app root folder, not the src folder.

I got it to work again after I took out the .env and just left the API keys in the App.js. But whenever I try to use the env I get the same errors. And the .env file is in the root

Not sure what the issue is then. Did you restart the app after adding the .env file? Maybe also double check that you are using the long number for the API_KEY and the short one for the API_ID. Other than that I can’t really think of anything.

Have you tried adding a second argument to the fetch with an object like:

{
      method: "GET",
      mode: "cors",
      headers: {
        "Content-Type": "application/json"
      },
2 Likes

What about using Axios in react?

You should be able to use dotenv to load env files from the root of your directory, so outside the src folder. Then just import dotenv into the component and set the API key to REACT_APP_API_KEY={whatever your API key is} inside of the env file.

Set the path of the env file to dotenv.config({ path: "./config.env" }); inside the component then you can pull your API key using process.env.REACT_APP_API_KEY.

so your url for the response should look like this:

`https://api.edamam.com/search?q=chicken&app_id=${process.env.REACT_APP_API_ID}&app_key=${process.env.REACT_APP_API_KEY}`

Hopefully that is explained well enough.

I think that might be what I am missing. I forgot to import the .env file. So do I put the dotenv.config({ path: "./config.env" }); line at the top of my App.js which is the code I had above?

You don’t need to do anything. As long as the .env file is in the root and you are using create-react-app.

As said, I tested this locally using the code you posted with my own API id/key and it works just fine. You can make a private Github repo and PM the link if you want.

1 Like

Reminder:
All API keys will become public once the React App is built. They will no longer be secret even if using an .env file.

@fkhan698 If you are still looking for a solution, perhaps this lesson on the venerable Scrimba platform could help steer you in the right direction…? https://scrimba.com/p/p7P5Hd/c79Jask