apilayer-coinlayer
v1.0.0
Published
Coinlayer API SDK
Downloads
1
Readme
Coinlayer JavaScript SDK
A lightweight and easy-to-use SDK for interacting with the Coinlayer API. This SDK covers all major endpoints, enabling developers to fetch cryptocurrency data seamlessly.
Installation
Install the SDK using npm:
npm install apilayer-coinlayeror via yarn
yarn add apilayer-coinlayerUsage
First, you need to create an account on Coinlayer API and obtain your API key.
Initialization
This SDK supports both CommonJS (require) and ES Modules (import).
CommonJS (require)
const Coinlayer = require("apilayer-coinlayer");'
// Initialize with your API key
const coinlayer = new Coinlayer(YOUR_API_KEY);ES Modules (import)
import Coinlayer from "apilayer-coinlayer";
// Initialize with your API key
const coinlayer = new Coinlayer(YOUR_API_KEY);API Methods
list(params = {})
Fetches a list of all available cryptocurrencies and their symbols.
- Parameters: Optional query parameters.
- Example:
const list = await coinLayer.list();live(params = {})
Fetches the latest exchange rates.
- Parameters: Optional query parameters (e.g.,
symbols). - Example:
const live = await coinLayer.live({ symbols: "BTC,ETH" });historical(date, params = {})
Fetches historical data for a specific date.
- Parameters:
date(required): The date inYYYY-MM-DDformat.params: Optional query parameters.
- Example:
const historical = await coinLayer.historical("2018-04-30");convert(from, to, amount, params = {})
Converts an amount from one cryptocurrency to another.
- Parameters:
from(required): The base currency.to(required): The target currency.amount(required): The amount to convert.params: Optional query parameters.
- Example:
const conversion = await coinLayer.convert("BTC", "ETH", 10);timeframe(start_date, end_date, params = {})
Fetches data for a specific timeframe.
- Parameters:
start_date(required): The start date inYYYY-MM-DDformat.end_date(required): The end date inYYYY-MM-DDformat.params: Optional query parameters.
- Example:
const timeframe = await coinLayer.timeframe("2018-04-01", "2018-04-30", { symbols: "BTC,ETH" });change(params = {})
Fetches change data for a specific timeframe or defaults to yesterday and today if no dates are provided.
- Parameters: Optional query parameters (
start_date,end_date,symbols). - Example:
const change = await coinLayer.change({ symbols: "BTC,ETH,XRP" });Error Handling
All methods throw an error if required parameters are missing or if the API call fails. Use try-catch blocks to handle errors gracefully.
try {
const live = await coinLayer.live();
} catch (error) {
console.error("Error fetching live data:", error);
}License
This SDK is licensed under the MIT License. See the LICENSE file for more details.
