Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
Iterating IDs to send in an API call

I have an array of crypto coin IDs that I need to send in an API call to get data against each coin. How do I iterate it and send in API call?

The IDs array being stored:

const getIds = async () => {
  Axios.get(
    "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false"
  ).then((response) =>
    setCoinIds((coinIds) => [
      ...coinIds,
      ...response.data.map((res) => res.id),
    ])
  );

};

The coin details API. The {coin_id} is where I need to iterate and send api calls against each ID.

export const getCoinsData = async () => {
  try {
    const response = await Axios.get(
      "https://api.coingecko.com/api/v3/coins/${coin_id)"
    );
    return response.data;
  } catch (e) {
    console.log(e);
  }
};

In the end, I need to store all coin data in a collective object:

const fetchCoinData = async () => {
    setLoading(true);
    const fetchedCoinData = await getCoinsData();
    setData(fetchedCoinData);
    setLoading(false);
  }; 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Using CoinGecko’s API recursively when on a free version is bit tricky. If you go through this API documentation , you will see a statement saying they have rate limit for their API calls.

Our Free API* has a rate limit of 50 calls/minute.

I have rewritten a working code to fetch all coins and get their IDs then recursively call CoinGecko's API for those ids with an interval of 3 sec between each API call, which makes our rate of calling 20 calls/minute.

Here is the code for the same :

Note - You can modify this to fit in with react to set state and all. Currently this can be run with node directly.

import axios from "axios";
const instance = axios.create({
    baseURL: "https://api.coingecko.com/api/v3",
});

const delay = async (ms = 3000) => {
    return new Promise((resolve) => setTimeout(resolve, ms));
};

let coinIds = [];
const getIds = async () => {
    try {
        const response = await instance.get(
            "/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false"
        );
        coinIds = [...coinIds, ...response.data.map((coin) => coin.id)];
        console.log("🚀 ~ file: apiCall.js ~ line 14 ~ getIds ~ coinIds", coinIds);
    } catch (e) {
        console.log("🚀 ~ file: apiCall.js ~ line 17 ~ getIds ~ e", e.message);
    }
};

const getCoinsData = async (coin_id) => {
    console.log(
        "🚀 ~ file: apiCall.js ~ line 22 ~ getCoinsData ~ coin_id",
        coin_id
    );
    try {
        const response = await instance.get(`/coins/${coin_id}`);
        return response.data;
    } catch (e) {
        console.log(
            "🚀 ~ file: apiCall.js ~ line 26 ~ getCoinsData ~ e",
            e.message
        );
    }
};

const fetchCoinData = async () => {
    try {
        await getIds();
        coinIds = coinIds.slice(0, 4);
        // uncomment below to call API concurrently (if on paid version)
        // const fetchedCoinData = await Promise.all(coinIds.map((coinId) => getCoinsData(coinId)))

        // if on free version use this
        const fetchedCoinData = await coinIds.reduce(
            (acc, coinId) =>
                acc.then(async () => {
                    await delay();
                    const coinData = await getCoinsData(coinId);
                    return coinData;
                }),
            Promise.resolve()
        );
        console.log(
            "🚀 ~ file: apiCall.js ~ line 33 ~ fetchCoinData ~ fetchedCoinData",
            fetchedCoinData
        );
    } catch (e) {
        console.log(
            "🚀 ~ file: apiCall.js ~ line 36 ~ fetchCoinData ~ e",
            e.message
        );
    }
};

fetchCoinData();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda