Overview
ThecryptoService 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.Parameters
Starting index for pagination (e.g., 0, 100, 200). Used to skip the first N cryptocurrencies.
Maximum number of results to retrieve. The API supports up to 100 results per request.
Returns
Returns a promise that resolves to an array of cryptocurrency objects.
Error Handling
The function throws an error if the API request fails:Usage Examples
getCryptoDetails
Searches for a specific cryptocurrency by ID by iterating through paginated results until found.Parameters
The unique identifier of the cryptocurrency to retrieve. This is the same
id field returned in CryptoApiResponse objects.Returns
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:- Requests cryptocurrencies in pages of 100 (using
getCryptoList) - Searches each page for a matching ID
- Returns immediately when found
- Continues pagination up to 2,000 cryptocurrencies
- Throws an error if not found
Error Handling
The function throws an error if the cryptocurrency is not found after searching all pages:getCryptoList function if API requests fail.
Usage Examples
API Endpoint Reference
CoinLore Tickers Endpoint
URL:https://api.coinlore.net/api/tickers/
Method: GET
Query Parameters:
start- Starting position for paginationlimit- Number of results (max 100)
Type Definitions
The service uses theCryptoApiResponse interface from src/models/Crypto.ts:
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.Related
- Crypto Model - Data models and type definitions
- useCryptoData Hook - React hook for managing crypto data
- Crypto Details Feature - Feature using getCryptoDetails