# 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 (e.g., `process.env.COINGECKO_PRO_API_KEY` or `dotenv`).
- **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 environment variables (e.g., `process.env` or `dotenv`).
3. Follows all other Critical Rules.
4. Does **NOT** contain any Deprecated Patterns.