Next.js Discord

Discord Forum

Getting EAI_AGAIN error code after calling coingecko api on Next api

Answered
Sirex woodwasp posted this in #help-forum
Open in Discord
Sirex woodwaspOP
I am trying to get fetch token price using coingecko api on next api and it's giving me such error while it's working pretty well on client side
{
"errno": -3001,
"code": "EAI_AGAIN",
"syscall": "getaddrinfo",
"hostname": "pro-api.coingecko.com"
}

fyi, here's the api handler function:
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  const { tokenName } = req.query;

  if (!tokenName || typeof tokenName !== "string") {
    return res.status(400).json({ message: "Token name is required" });
  }

  try {
    const isEcosystemToken = isEcosystemTokenName(tokenName);
    const response = await fetchPrice(tokenName, isEcosystemToken);

    return res.status(200).json(
      isEcosystemToken ? [{ current_price: response }] : response
    );
  } catch (error) {
    return res.status(500).json({ message: error });
  }
}


const fetchPrice = async (tokenName: string) => {
  const API_URL = `https://pro-api.coingecko.com/api/v3/coins/markets?ids=${tokenName}&vs_currency=usd&x_cg_pro_api_key=${process.env.NEXT_PUBLIC_COINGECKO_API_KEY}`;
  const response = await fetch(API_URL);
  return response;
};

any idea on this?
Answered by James4u
Maybe network connection issue? are you behind proxy or are you using any VPN services?
View full answer

9 Replies

Sirex woodwaspOP
bump
your code looks good to me
EAI_AGAIN is a DNS lookup timed out error, means it is a network connectivity error or proxy related error.
Sirex woodwaspOP
yeah, i also checked that out but have no idea actually why it shows up..
Maybe network connection issue? are you behind proxy or are you using any VPN services?
Answer
Sirex woodwaspOP
hmm.. sounds reasonable, lemme check it real quick
Sirex woodwaspOP
you really hit the point, i was using vpn service and that was the reason!
thanks man 🙂