list.ts
1 import { env } from "@/env.mjs"; 2 import type { NextApiRequest, NextApiResponse } from "next"; 3 4 interface RequestBody { 5 currency: string; 6 sort: string; 7 order: string; 8 offset: number; 9 limit: number; 10 meta: boolean; 11 } 12 13 const requestBody: RequestBody = { 14 currency: "USD", 15 sort: "rank", 16 order: "ascending", 17 offset: 0, 18 limit: 100, 19 meta: false, 20 }; 21 22 export default async function handler( 23 req: NextApiRequest, 24 res: NextApiResponse, 25 ) { 26 try { 27 const response = await fetch("https://api.livecoinwatch.com/coins/list", { 28 method: "POST", 29 headers: { 30 "Content-Type": "application/json", 31 "x-api-key": env.LIVECOINWATCH_API_KEY, 32 }, 33 body: JSON.stringify(requestBody), 34 }); 35 36 if (!response.ok) { 37 throw new Error(`HTTP error ${response.status}`); 38 } 39 40 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment 41 const data = await response.json(); 42 res.status(200).json(data); 43 } catch (error: unknown) { 44 // Catch the error as unknown 45 if (error instanceof Error) { 46 // Check if the error is an instance of Error 47 res.status(500).json({ error: error.message }); // Access the message property 48 } else { 49 res.status(500).json({ error: "An unknown error occurred" }); 50 } 51 } 52 }