In the file apiKeys.js I get all the apikeys from the .env file.
I will convert all those apikeys into an Array data.
Then I will export this array in order to get these apikeys in all the other JavaScript files.
You should also note that I get the first apikey in the .env file here at the end.
letAPI1=process.env.REACT_APP_FOOD_API_KEY;
I will say later why this is.
src / App.js
I have first imported ApiKeys from "./api/ApiKeys" in the image above.
Then I have written a few spoonacular api links.
Then I get apiKey in the variable "getApiKey" using useSelector inside the App function.
import{useSelector,useDispatch}from"react-redux";// import api key Arrayimport{ApiKeys}from'./api/ApiKeys'// some api urlconsthomeRecipeURL='https://api.spoonacular.com/recipes/random?number=21';constRecipeURL='https://api.spoonacular.com/recipes/';constSimilarRecipeURL='https://api.spoonacular.com/recipes/'constApp=()=>{// get api key id constgetApiKey=useSelector((state)=>state.apiKey_Data.apiKey)//some code...return(//some code...)}exportdefaultApp;
Then inside the App function, we write an important and simple function.
This is going to hack / break that api limit 😎
That called as changeAPiKey()🔥
import{UpdateApiKey}from"./store/action/updateApi";import{useSelector,useDispatch}from"react-redux";import{UpdateApiKey}from"./store/action/updateApi";constApp=()=>{//some code...letapiCallTime=0;constdispatch=useDispatch();constchangeAPiKey=()=>{// change api key when api limit hit 150 point, then it's throw 402 errorletCurrentApi=ApiKeys[apiCallTime];dispatch(UpdateApiKey(CurrentApi));console.log("api limit error & status code = 4️⃣0️⃣2️⃣, but you don't worry, api was changed. so happy cooking 😁")apiCallTime++;if (apiCallTime>8){apiCallTime=0;}};//some code...return(//some code...)}exportdefaultApp;
Here I first wrote
letapiCallTime=0;
Then I wrote
constdispatch=useDispatch ();
You can tell by looking at the changeAPiKey() function above.
When changeAPiKey() first runs, it stores the api key in the "apiCallTime" position in the array src / api / ApiKeys.js as "CurrentApi".
letCurrentApi=ApiKeys[apiCallTime];
It then dispatches it to UpdateApiKey using useDispatch().
Now the Api Key Data retrieved from the store will be updated by the "CurrentApi value".
So wherever we get Api Key Data in this entire react js app, it will be the updated new API KEY ✨.
another continues function that call / run ChangeApiKey()
// Home Pageconst[homeRecipeData,setHomeRecipeData]=React.useState([]);const[home,setHome]=React.useState(false);useEffect(()=>{asyncfunctionfetchData(){try{constapiResponse=awaitaxios.get(homeRecipeURL+`&apiKey=${getApiKey}`);consthomeRecipeData=awaitapiResponse.data;setHomeRecipeData(homeRecipeData);setHome(true)}catch (err){if (err.response){if(err.response.status===402){changeAPiKey()}}console.log(err.response.data.message)}}fetchData();},[getApiKey]);
Now in this fetchData() function I get 'api data' using 'api url' and 'api key' with the help of "axios".
In this I will use "initialApiKey" in the store as the 'api key' through "useSelector".
Probably 402 errors if "initialApiKey" runs out of 150 points per day.
If you get a 402 error like this, run the ChangeApiKey() function.
Then, as I said, the old 'api key' which gives 402 error will be replaced by the 'new api key' in the 'Array'.
I would have written the 'api key' at the beginning of the last .env in the array.
This is because the 'api key' used first in "initialApiKey" is the 'api key' at the beginning of the .env.
If "initialApiKey" gives 402 error ChangeApiKey() will again use the 'api key' which returns the old error at the beginning of .env,
So I would have written the 'api key' at the beginning of the .env at the end of the array.
The website will continue to run as usual.
Celebrate a victory🎉💃💃💃 You have now exceeded the api limit...
I am use 10 free account api.
one free api = 150 point/day;
10 x 150 = 1500
So, you get freely 1500 point / pre day, even you don't pay for that.
Now you save up to 29$ per month 🤑🤑🤑.
You can save more on that if you think💸.
If you use the API KEY only once on your processor, you can do this using redux just ChangeApiKey().
But I use redux because this application uses the updated "API KEY" in different javscript files in many places.
[DISCLAIMER: NO HARM INTENDED, THIS IS JUST TO CREATE A PUBLIC AWARENESS]