pummmp.fun/app/api/sol-price/route.ts
2026-07-04 12:49:09 -07:00

30 lines
766 B
TypeScript

import { NextResponse } from 'next/server'
export async function GET() {
try {
const response = await fetch(
'https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd&include_24hr_change=true',
{ next: { revalidate: 60 } } //? cache for 60 seconds
)
if (!response.ok) {
throw new Error('Failed to fetch SOL price')
}
const data = await response.json()
return NextResponse.json({
price: data.solana.usd,
change24h: data.solana.usd_24h_change,
lastUpdated: new Date(),
})
} catch (error) {
console.error('Error fetching SOL price:', error)
//! fallback
return NextResponse.json({
price: 120,
change24h: 0,
lastUpdated: new Date(),
})
}
}