Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/knutkirkhorn/btc-value
💸 Get the current Bitcoin value
https://github.com/knutkirkhorn/btc-value
bitcoin btc cryptocurrency nodejs value
Last synced: 2 months ago
JSON representation
💸 Get the current Bitcoin value
- Host: GitHub
- URL: https://github.com/knutkirkhorn/btc-value
- Owner: knutkirkhorn
- License: mit
- Created: 2017-12-08T16:09:05.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2023-03-09T21:32:48.000Z (almost 2 years ago)
- Last Synced: 2024-11-29T04:12:00.249Z (2 months ago)
- Topics: bitcoin, btc, cryptocurrency, nodejs, value
- Language: JavaScript
- Homepage:
- Size: 349 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> Get the current Bitcoin value
[![Downloads](https://img.shields.io/npm/dm/btc-value.svg)](https://www.npmjs.com/package/btc-value) [![Coverage Status](https://codecov.io/gh/knutkirkhorn/btc-value/branch/main/graph/badge.svg)](https://codecov.io/gh/knutkirkhorn/btc-value)
## Installation
```
npm install btc-value
```## Usage
```js
import btcValue, {
setProvider,
setApiKey,
getPercentageChangeLastDay,
getSupportedCurrencies
} from 'btc-value';// Set the value provider
setProvider('coingecko');// Set the API key
setApiKey('example-cmc-API-key');// Print the current value of Bitcoin in USD
console.log(`$${await btcValue()}`);
// => e.g. $11048// Print the current value of Bitcoin in NOK (Norwegian krone)
console.log(`kr ${await btcValue('NOK')}`);
// => e.g. kr 86664// Print the current value of 2.2 BTC in USD
console.log(`$${await btcValue({quantity: 2.2})}`);
// => e.g. $24305.82// Print the percentage change in BTC value the last day
console.log(`${await getPercentageChangeLastDay()} %`);
// => e.g. 5%// Print all supported currencies for selected value provider
console.log(await getSupportedCurrencies());
// => cmc: [ ..., { name: 'Norwegian Krone', code: 'NOK', symbol: 'kr' }, ... ]
// => coingecko: [ ..., 'nok', ... ]
```## API
The Bitcoin value can be retrieved from [CoinMarketCap](https://coinmarketcap.com/) or [CoinGecko](https://www.coingecko.com). See the API used for CoinMarketCap [here](https://coinmarketcap.com/api/) and for CoinGecko [here](https://www.coingecko.com/en/api). If using the CoinMarketCap API to retrieve Bitcoin values, it is required to obtain and use an API key. This can be done [here](https://coinmarketcap.com/api/). Before using the functions for retrieving the Bitcoin value, one must then call `btcValue.setApiKey()` with your key. If using CoinGecko, this is not needed.
### btcValue(currencyCode?)
Returns the current Bitcoin value in USD ($).
#### currencyCode
Type: `string`
Default: `USD`Returns the current Bitcoin value in a different currency than `USD`. All valid currency codes can be retrieved for the selected value provider using the `getSupportedCurrencies` function.
### setProvider(provider)
Sets the selected provider to retrieve Bitcoin values from. Supported providers are: `cmc` (CoinMarketCap) and `coingecko`.
#### provider
Type: `string`
### setApiKey(apiKey)
Sets the API key for the selected value provider. Currently only CoinMarketCap supports using an API key. This is required to call the functions with the [CoinMarketCap API](https://coinmarketcap.com/api/).
#### apiKey
Type: `string`
### getPercentageChangeLastHour()
Returns the percentage change of BTC the last hour.
### getPercentageChangeLastDay()
Returns the percentage change of BTC the last day.
### getPercentageChangeLastWeek()
Returns the percentage change of BTC the last week.
### getSupportedCurrencies()
Returns an array with all the supported currencies for the selected value provider.
Example of the format for a single currency in the list using CoinMarketCap:```json
{
"name": "Norwegian Krone",
"code": "NOK",
"symbol": "kr"
}```
Example of a returned array using CoinGecko:
```js
['btc', 'eth']
```## Related
- [btc-value-cli](https://github.com/knutkirkhorn/btc-value-cli) - CLI for this module