Skip to main content

Overview

The cryptoService module provides functions to interact with the CoinLore API for retrieving cryptocurrency market data. It offers paginated list fetching and individual cryptocurrency lookup capabilities. Base API: https://api.coinlore.net/api/tickers/ Key Features:
  • Paginated cryptocurrency list retrieval
  • Individual cryptocurrency lookup by ID
  • Built-in error handling
  • TypeScript type safety with CryptoApiResponse

getCryptoList

Retrieves a paginated list of cryptocurrencies from the CoinLore API.
getCryptoList(start?: number, limit?: number): Promise<CryptoApiResponse[]>

Parameters

start
number
default:"0"
Starting index for pagination (e.g., 0, 100, 200). Used to skip the first N cryptocurrencies.
limit
number
default:"100"
Maximum number of results to retrieve. The API supports up to 100 results per request.

Returns

Promise<CryptoApiResponse[]>
array
Returns a promise that resolves to an array of cryptocurrency objects.

Error Handling

The function throws an error if the API request fails:
throw new Error(`Error al obtener lista de criptos: ${response.status}`);

Usage Examples

import { getCryptoList } from '../services/cryptoService';

// Get first 100 cryptocurrencies (default)
const cryptos = await getCryptoList();
console.log(cryptos); // Array of 100 CryptoApiResponse objects

getCryptoDetails

Searches for a specific cryptocurrency by ID by iterating through paginated results until found.
This function makes multiple API calls (up to 20 requests) to search through paginated results. Consider caching the result or using a more efficient lookup method for production applications.
getCryptoDetails(id: string): Promise<CryptoApiResponse>

Parameters

id
string
required
The unique identifier of the cryptocurrency to retrieve. This is the same id field returned in CryptoApiResponse objects.

Returns

Promise<CryptoApiResponse>
object
Returns a promise that resolves to a single cryptocurrency object matching the provided ID. See getCryptoList for the complete CryptoApiResponse structure.

Algorithm

The function uses a search algorithm that:
  1. Requests cryptocurrencies in pages of 100 (using getCryptoList)
  2. Searches each page for a matching ID
  3. Returns immediately when found
  4. Continues pagination up to 2,000 cryptocurrencies
  5. Throws an error if not found

Error Handling

The function throws an error if the cryptocurrency is not found after searching all pages:
throw new Error(`Criptomoneda con id ${id} no encontrada`);
It may also throw errors from the underlying getCryptoList function if API requests fail.

Usage Examples

import { getCryptoDetails } from '../services/cryptoService';

// Get Bitcoin details (id: "90")
const bitcoin = await getCryptoDetails('90');
console.log(bitcoin.name); // "Bitcoin"
console.log(bitcoin.symbol); // "BTC"
console.log(bitcoin.price_usd); // Current price

API Endpoint Reference

CoinLore Tickers Endpoint

URL: https://api.coinlore.net/api/tickers/ Method: GET Query Parameters:
  • start - Starting position for pagination
  • limit - Number of results (max 100)
Example Request:
https://api.coinlore.net/api/tickers/?start=0&limit=100
Response Format:
{
  "data": [
    {
      "id": "90",
      "symbol": "BTC",
      "name": "Bitcoin",
      "nameid": "bitcoin",
      "rank": 1,
      "price_usd": "45123.45",
      "percent_change_24h": "2.34",
      "percent_change_1h": "0.12",
      "percent_change_7d": "5.67",
      "price_btc": "1.00",
      "market_cap_usd": "850000000000",
      "volume24": 28000000000,
      "volume24a": 27500000000,
      "csupply": "19000000",
      "tsupply": "21000000",
      "msupply": "21000000"
    }
  ]
}

Type Definitions

The service uses the CryptoApiResponse interface from src/models/Crypto.ts:
export interface CryptoApiResponse {
  id: string;
  symbol: string;
  name: string;
  nameid: string;
  rank: number;
  price_usd: string;
  percent_change_24h: string;
  percent_change_1h: string;
  percent_change_7d: string;
  price_btc: string;
  market_cap_usd: string;
  volume24: number;
  volume24a: number;
  csupply: string;
  tsupply: string;
  msupply: string;
}

Best Practices

Caching

Consider implementing caching for getCryptoDetails to avoid multiple API requests for the same cryptocurrency.

Error Handling

Always wrap service calls in try-catch blocks to handle network failures and API errors gracefully.

Rate Limiting

Be mindful of API rate limits when making frequent requests. The CoinLore API is free but may have usage restrictions.

Type Safety

Use TypeScript types (CryptoApiResponse) to ensure type safety throughout your application.

Build docs developers (and LLMs) love