Files
zoo/app/pages/api/usdCoinConversion.ts
2026-04-01 15:53:20 -07:00

41 lines
1.1 KiB
TypeScript

import supportedChains from 'utils/chains'
import { NextResponse } from 'next/server'
import { fetchWithTimeout } from 'utils/fetcher'
export const config = {
runtime: 'experimental-edge',
}
let ids = supportedChains.map((chain) => chain.coingeckoId).join(',')
export default async function handler() {
let prices
let cacheSettings = 'maxage=0, s-maxage=86400 stale-while-revalidate' // Default cache settings
try {
const response = await fetchWithTimeout(
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=${ids}`,
{},
5000 // Timeout in milliseconds (5 seconds)
)
prices = await response.json()
} catch (error) {
console.error('Error fetching Coingecko data:', error)
prices = null
cacheSettings = 'maxage=0, s-maxage=300 stale-while-revalidate' // Reduce cache time if Coingecko API fails
}
return new NextResponse(
JSON.stringify({
prices,
}),
{
status: 200,
headers: {
'content-type': 'application/json',
'Cache-Control': cacheSettings,
},
}
)
}