Kelvin Fumo
Kelvin Fumo

Reputation: 1

Limiting API calls with Alpha vantage on React

I am trying to limit the number of API calls being made when fetching data from Alpha Vantage. So far I've tried using throttling and debouncing, I also tried using the setInterval() method inside a useEffect, which were to no use as when running the react app it makes a lot of API calls. How can I change these simple lines so that it only makes one API call per a set amount of time:

const fetchData = async (symbol) => {
  const API_KEY = "YOUR_API_KEY";
  var URL = `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${symbol}&interval=5min&apikey=${API_KEY}`;
  const response = await fetch(URL);
  const data = await response.json();
  return data;
};

I tried a different approach to getting the data, not sure if its relevant.

 let alphavantage = async () => {
    try {
      fetch(URL)
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          setNum(num + 1);
          console.log("Number of API calls made: ", num);
          setData(data);
        });
    } catch (error) {
      console.log("Error fetching data: ", error);
    }
  };

Thank you!

Upvotes: 0

Views: 123

Answers (1)

wes
wes

Reputation: 1

this is how I did it

Wait for 0 minutes before the next request to avoid API limits

            time.sleep(300)

Upvotes: 0

Related Questions