|
import { Tool } from 'openai-function-calling-tools'; |
|
import { z } from 'zod'; |
|
|
|
function createCoinMarketCapApi({ apiKey }: { apiKey: string }) { |
|
const paramsSchema = z.object({ |
|
input: z.string(), |
|
}); |
|
const name = 'coinmarketcap'; |
|
const description = 'A realtime cryptocurrency API. Useful for when you need to answer questions about latest crypto'; |
|
|
|
const execute = async ({ input }: z.infer<typeof paramsSchema>) => { |
|
try { |
|
|
|
|
|
const queryParams = new URLSearchParams({ |
|
start: '1', |
|
limit: '20', |
|
convert: 'USD' |
|
}); |
|
|
|
const response = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?${queryParams}`, { |
|
method: 'GET', |
|
headers: { |
|
'X-CMC_PRO_API_KEY': apiKey, |
|
'Accept': 'application/json', |
|
}, |
|
}); |
|
|
|
if (!response.ok) { |
|
throw new Error('Network response was not ok'); |
|
} |
|
|
|
const data = await response.json(); |
|
return JSON.stringify(data); |
|
} catch (error) { |
|
throw new Error(`Error in coinMarketCapApi: ${error}`); |
|
} |
|
}; |
|
|
|
return new Tool(paramsSchema, name, description, execute).tool; |
|
} |
|
|
|
export { createCoinMarketCapApi }; |
|
|