Spaces:
Sleeping
Sleeping
import { Tool } from 'openai-function-calling-tools'; | |
import { z } from 'zod'; | |
function createCoinMarketCapApi({ apiKey }: { apiKey: string }) { | |
const paramsSchema = z.object({ | |
input: z.string(), // Assuming this is used to pass query parameters, though you might want to define these more specifically | |
}); | |
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 { | |
// Parse the input to get parameters, or define them directly if not using 'input' for this | |
// For demonstration, I'm using static parameters that you might want to customize | |
const queryParams = new URLSearchParams({ | |
start: '1', | |
limit: '10', | |
convert: 'USD' | |
}); | |
const response = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?${queryParams}`, { | |
method: 'GET', // Method is GET by default for fetch, specified here for clarity | |
headers: { | |
'X-CMC_PRO_API_KEY': apiKey, // Use the passed apiKey for authentication | |
'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 }; | |