The Coingecko TypeScript API Library provides convenient access to the Coingecko REST API from server-side TypeScript or JavaScript applications. This client library makes it easy to interact with cryptocurrency data from your applications.
To install the library, use npm:
npm install @coingecko/coingecko-typescript
Here's a simple example of how to use the Coingecko client to fetch Bitcoin's price in USD:
import Coingecko from '@coingecko/coingecko-typescript';
const client = new Coingecko({
proAPIKey: process.env['COINGECKO_PRO_API_KEY'],
// demoAPIKey: process.env['COINGECKO_DEMO_API_KEY'], // Optional, for Demo API access
environment: 'pro', // 'demo' to initialize the client with Demo API access
});
async function main() {
const price = await client.simple.price.get({ vs_currencies: 'usd', ids: 'bitcoin' });
console.log(price);
}
main();
The library includes TypeScript definitions for all request params and response fields:
import Coingecko from '@coingecko/coingecko-typescript';
const client = new Coingecko({
proAPIKey: process.env['COINGECKO_PRO_API_KEY'],
environment: 'pro',
});
const params: Coingecko.Simple.PriceGetParams = { vs_currencies: 'usd', ids: 'bitcoin' };
const price: Coingecko.Simple.PriceGetResponse = await client.simple.price.get(params);
When the API returns a non-success status code, the library throws a subclass of APIError
:
const price = await client.simple.price.get({ vs_currencies: 'usd', ids: 'bitcoin' }).catch(async (err) => {
if (err instanceof Coingecko.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});
Status Code | Error Type |
---|---|
400 | BadRequestError |
401 | AuthenticationError |
403 | PermissionDeniedError |
404 | NotFoundError |
422 | UnprocessableEntityError |
429 | RateLimitError |
>=500 | InternalServerError |
N/A | APIConnectionError |
Certain errors are automatically retried 2 times by default with exponential backoff:
// Configure the default for all requests:
const client = new Coingecko({
maxRetries: 0, // default is 2
});
// Or, configure per-request:
await client.simple.price.get({ vs_currencies: 'usd', ids: 'bitcoin' }, {
maxRetries: 5,
});
Requests time out after 1 minute by default:
// Configure the default for all requests:
const client = new Coingecko({
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
});
// Override per-request:
await client.simple.price.get({ vs_currencies: 'usd', ids: 'bitcoin' }, {
timeout: 5 * 1000,
});
const client = new Coingecko();
// Method 1: Get only the raw response
const response = await client.simple.price.get({ vs_currencies: 'usd', ids: 'bitcoin' }).asResponse();
console.log(response.headers.get('X-My-Header'));
// Method 2: Get both data and raw response
const { data: price, response: raw } = await client.simple.price
.get({ vs_currencies: 'usd', ids: 'bitcoin' })
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(price);
import Coingecko from '@coingecko/coingecko-typescript';
// Set log level in client options
const client = new Coingecko({
logLevel: 'debug', // Show all log messages
});
Available log levels (from most to least verbose):
'debug'
- Show debug messages, info, warnings, and errors'info'
- Show info messages, warnings, and errors'warn'
- Show warnings and errors (default)'error'
- Show only errors'off'
- Disable all loggingimport Coingecko from '@coingecko/coingecko-typescript';
import pino from 'pino';
const logger = pino();
const client = new Coingecko({
logger: logger.child({ name: 'Coingecko' }),
logLevel: 'debug',
});
import Coingecko from '@coingecko/coingecko-typescript';
import fetch from 'my-fetch';
const client = new Coingecko({ fetch });
For Node.js:
import Coingecko from '@coingecko/coingecko-typescript';
import * as undici from 'undici';
const proxyAgent = new undici.ProxyAgent('http://localhost:8888');
const client = new Coingecko({
fetchOptions: {
dispatcher: proxyAgent,
},
});
For Bun:
import Coingecko from '@coingecko/coingecko-typescript';
const client = new Coingecko({
fetchOptions: {
proxy: 'http://localhost:8888',
},
});
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "coingecko" '{"command":"npx","args":["-y","@coingecko/coingecko-typescript"]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"coingecko": {
"command": "npx",
"args": [
"-y",
"@coingecko/coingecko-typescript"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"coingecko": {
"command": "npx",
"args": [
"-y",
"@coingecko/coingecko-typescript"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect