Typescript AI Prompts

A comprehensive AI prompt to guide coding assistants in correctly implementing the official CoinGecko TypeScript SDK (@coingecko/coingecko-typescript).


How to Use Our Prompts

Integrating these prompts into your workflow is simple. Copy the entire markdown prompt for your chosen language and provide it as context to your AI assistant.

  1. For Chat Interfaces (Claude, ChatGPT, etc.): Paste the prompt at the beginning of your conversation before asking the AI to write code.
  2. For Cursor IDE: Add the prompt to your project's Rules to enforce the guidelines across all AI interactions.
  3. For GitHub Copilot: Save the prompt to a file (e.g. coingecko_rules.md) and reference it in your chat with @workspace #coingecko_rules.md.
# Goal: Generate TypeScript code for the CoinGecko API using ONLY the official `@coingecko/coingecko-typescript` package.

## 🚨 Critical Rules

1.  **SDK Exclusivity**:
    - **ALWAYS** use the official `@coingecko/coingecko-typescript` package.
    - **NEVER** use `axios`, `fetch`, or any other method to make direct HTTP requests.

2.  **Authentication**:
    - **ALWAYS** load the API key from environment variables (`process.env.COINGECKO_PRO_API_KEY`).
    - **NEVER** hardcode API keys or any other credentials in the source code.

3.  **Client Implementation**:
    - **ALWAYS** initialize a single, reusable `Coingecko` client instance for the application.
    - **ALWAYS** configure automatic retries by setting the `maxRetries` option on the client.
    - **NEVER** implement manual retry loops using `setTimeout`. The SDK handles this internally.

4.  **Error Handling & Types**:
    - **ALWAYS** use `async/await` for all API calls.
    - **ALWAYS** catch specific `APIError` subclasses using `instanceof` (e.g., `err instanceof Coingecko.RateLimitError`).
    - **ALWAYS** use the built-in request and response types (e.g., `Coingecko.Simple.PriceGetParams`).
    - **NEVER** use generic `catch (e)` blocks for known API errors.

## ✅ Correct Implementation Example

This is the standard for all generated code.

```typescript
// src/api/client.ts
import Coingecko from '@coingecko/coingecko-typescript';

// Initialize a single, reusable client. This should be imported and used application-wide.
export const client = new Coingecko({
  proAPIKey: process.env.COINGECKO_PRO_API_KEY,
  environment: 'pro',
  maxRetries: 3, // Rely on the SDK's built-in retry mechanism.
});

// src/main.ts
import { client } from './api/client';
import Coingecko from '@coingecko/coingecko-typescript'; // Import the namespace for types

async function getBitcoinPrice(): Promise<number | null> {
  try {
    const params: Coingecko.Simple.PriceGetParams = {
      ids: 'bitcoin',
      vs_currencies: 'usd',
    };
    const priceData = await client.simple.price.get(params);
    return priceData.bitcoin.usd;
  } catch (err) {
    if (err instanceof Coingecko.RateLimitError) {
      console.error('Rate limit exceeded. Please try again later.');
    } else if (err instanceof Coingecko.APIError) {
      console.error(
        `An API error occurred: ${err.name} (Status: ${err.status})`
      );
    } else {
      console.error('An unexpected error occurred:', err);
    }
    return null;
  }
}

async function main() {
  const price = await getBitcoinPrice();
  if (price !== null) {
    console.log(`The current price of Bitcoin is: $${price}`);
  }
}

main();
```

## ❌ Deprecated Patterns to AVOID

You **MUST NOT** generate code that includes any of the following outdated patterns.

```typescript
// ❌ NO direct HTTP requests with fetch or axios.
import axios from 'axios';
const response = await axios.get(
  '[https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd](https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd)'
);

// ❌ NO hardcoded API keys.
const client = new Coingecko({ proAPIKey: 'CG-abc123xyz789' });

// ❌ NO manual retry loops. The SDK's `maxRetries` handles this.
import { setTimeout } from 'timers/promises';
for (let i = 0; i < 3; i++) {
  try {
    const data = await client.simple.price.get({
      ids: 'bitcoin',
      vs_currencies: 'usd',
    });
    break;
  } catch (e) {
    await setTimeout(5000);
  }
}

// ❌ NO generic exception handling for API errors.
try {
  const data = await client.simple.price.get({
    ids: 'bitcoin',
    vs_currencies: 'usd',
  });
} catch (e) {
  console.log(`An error occurred: ${e}`); // Too broad. Use `instanceof` checks.
}
```

## 📝 Final Check

Before providing a response, you **MUST** verify that your generated code:

1.  Imports and uses `@coingecko/coingecko-typescript`.
2.  Loads the API key from `process.env`.
3.  Follows all other Critical Rules.
4.  Does **NOT** contain any Deprecated Patterns.

Resources

Notice something off or missing? Let us know by opening an Issue here.


Have feedback, a cool idea, or need help? Reach out to soonaik@coingecko[dot]com