Skip to main content
Glama

fetchTokenPriceHistoryBySymbol

Retrieve historical price data for a specific cryptocurrency by symbol, date range, and interval using this Alchemy MCP Server tool. Ideal for analyzing token price trends over time.

Input Schema

NameRequiredDescriptionDefault
endTimeYesThe end time date to query. e.g. "2021-01-01"
intervalYesThe interval to query. e.g. "1d" or "1h"
startTimeYesThe start time date to query. e.g. "2021-01-01"
symbolYesThe token symbol to query. e.g. "BTC" or "ETH"

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "endTime": { "description": "The end time date to query. e.g. \"2021-01-01\"", "type": "string" }, "interval": { "description": "The interval to query. e.g. \"1d\" or \"1h\"", "type": "string" }, "startTime": { "description": "The start time date to query. e.g. \"2021-01-01\"", "type": "string" }, "symbol": { "description": "The token symbol to query. e.g. \"BTC\" or \"ETH\"", "type": "string" } }, "required": [ "symbol", "startTime", "endTime", "interval" ], "type": "object" }

Implementation Reference

  • Core implementation of the tool logic: makes HTTP POST request to Alchemy's /prices/v1/{key}/tokens/historical endpoint with the provided parameters.
    async getTokenPriceHistoryBySymbol(params: TokenPriceHistoryBySymbol) { console.log('Fetching token price history for symbol:', params.symbol); try { const client = createPricesClient(); const response = await client.post('/historical', { ...params }); console.log('Successfully fetched token price history:', response.data); return response.data; } catch (error) { console.error('Error fetching token price history:', error); throw error; } },
  • index.ts:68-96 (registration)
    MCP server tool registration, including Zod input schema validation and inline error-handling handler that preprocesses dates using toISO8601 and delegates to alchemyApi.getTokenPriceHistoryBySymbol
    server.tool('fetchTokenPriceHistoryBySymbol', { symbol: z.string().describe('The token symbol to query. e.g. "BTC" or "ETH"'), startTime: z.string().describe('The start time date to query. e.g. "2021-01-01"'), endTime: z.string().describe('The end time date to query. e.g. "2021-01-01"'), interval: z.string().describe('The interval to query. e.g. "1d" or "1h"') }, async (params) => { try { const result = await alchemyApi.getTokenPriceHistoryBySymbol({ ...params, startTime: toISO8601(params.startTime), endTime: toISO8601(params.endTime) }); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (error) { if (error instanceof Error) { console.error('Error in getTokenPriceHistoryBySymbol:', error); return { content: [{ type: "text", text: `Error: ${error.message}` }], isError: true }; } return { content: [{ type: "text", text: 'Unknown error occurred' }], isError: true }; } });
  • TypeScript interface defining the input parameters for getTokenPriceHistoryBySymbol, matching the Zod schema.
    export interface TokenPriceHistoryBySymbol { symbol: string; startTime: string; endTime: string; interval: string; }
  • Factory function creating Axios HTTP client configured for Alchemy Prices API, used in the handler to make the historical price request.
    export const createPricesClient = () => axios.create({ baseURL: `https://api.g.alchemy.com/prices/v1/${API_KEY}/tokens`, headers: { 'accept': 'application/json', 'content-type': 'application/json', 'x-alchemy-client-breadcrumb': BREADCRUMB_HEADER }, });
  • Utility function toISO8601 used in the tool handler to convert startTime and endTime parameters to ISO 8601 format required by the API.
    export function toISO8601(dateStr: string): string { // Handle keywords first const normalizedStr = dateStr.toLowerCase(); const now = new Date(); switch (normalizedStr) { case 'today': { const date = new Date(now); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'yesterday': { const date = new Date(now); date.setDate(date.getDate() - 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'last-week': { const date = new Date(now); date.setDate(date.getDate() - 7); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'last-month': { const date = new Date(now); date.setMonth(date.getMonth() - 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'start-of-year': { const date = new Date(now); date.setMonth(0, 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'now': { return now.toISOString(); } } // If already in ISO format, return as is if (dateStr.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/)) { return dateStr; } // For any other date string, parse it normally try { const date = new Date(dateStr); return date.toISOString(); } catch (error) { console.error('Error parsing date:', error); // Return current time if parsing fails return now.toISOString(); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/alchemyplatform/alchemy-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server