get_token_price
Retrieve current token prices by contract address on supported networks, with optional market data like capitalization and volume for trading analysis.
Instructions
Get token prices by contract addresses using CoinGecko API
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| network | Yes | Network ID (e.g., 'eth', 'bsc', 'polygon_pos') | |
| addresses | Yes | Token contract addresses, comma-separated for multiple tokens | |
| include_market_cap | No | Include market capitalization (optional) | |
| mcap_fdv_fallback | No | Return FDV if market cap is not available (optional) | |
| include_24hr_vol | No | Include 24hr volume (optional) | |
| include_24hr_price_change | No | Include 24hr price change (optional) | |
| include_total_reserve_in_usd | No | Include total reserve in USD (optional) |
Implementation Reference
- Core handler function that fetches token prices from CoinGecko API by constructing the URL with network, addresses, and optional parameters, then makes HTTP request.
async getTokenPrice(network, addresses, options = {}) { try { const queryParams = new URLSearchParams(); // Add optional parameters if (options.include_market_cap) queryParams.append('include_market_cap', options.include_market_cap); if (options.mcap_fdv_fallback) queryParams.append('mcap_fdv_fallback', options.mcap_fdv_fallback); if (options.include_24hr_vol) queryParams.append('include_24hr_vol', options.include_24hr_vol); if (options.include_24hr_price_change) queryParams.append('include_24hr_price_change', options.include_24hr_price_change); if (options.include_total_reserve_in_usd) queryParams.append('include_total_reserve_in_usd', options.include_total_reserve_in_usd); const url = `${this.baseUrl}/simple/networks/${network}/token_price/${addresses}${queryParams.toString() ? '?' + queryParams.toString() : ''}`; const response = await fetch(url, { headers: { 'x-cg-demo-api-key': this.apiKey } }); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } return await response.json(); } catch (error) { throw new Error(`Failed to get token price: ${error.message}`); } }