Retrieve cryptocurrencies current prices https://github.com/ethauvin/cryptoprice
Find a file
2022-01-18 17:16:43 -08:00
.circleci Testing with JDK 17. 2021-10-01 22:18:57 -07:00
.github/workflows Testing with JDK 17. 2021-10-01 22:18:57 -07:00
.idea Updated dependencies and copyright. 2022-01-03 11:52:09 -08:00
examples Initial 0.9.0 release cleanup. 2022-01-18 17:16:43 -08:00
gradle/wrapper Updated dependencies and copyright. 2022-01-03 11:52:09 -08:00
src Updated dependencies and copyright. 2022-01-03 11:52:09 -08:00
.gitattributes Initial commit. 2021-05-08 01:35:55 -07:00
.gitignore Updated dependencies. 2021-07-08 10:38:46 -07:00
.gitlab-ci.yml Using system wide Gradle. 2021-05-21 13:10:31 -07:00
baseline.xml Improved exceptions. 2021-05-09 17:54:48 -07:00
bitbucket-pipelines.yml Testing with JDK 17. 2021-10-01 22:18:57 -07:00
build.gradle.kts Initial 0.9.0 release cleanup. 2022-01-18 17:16:43 -08:00
cryptoprice.iml Improved response parsing. 2022-01-03 11:51:31 -08:00
detekt-baseline.xml Added wrapper parameter to toJson/toPrice. 2021-05-27 23:54:23 -07:00
gradlew Updated to Kotlin 1.5.30. 2021-08-24 22:18:48 -07:00
gradlew.bat Initial commit. 2021-05-08 01:35:55 -07:00
LICENSE.txt Updated dependencies and copyright. 2022-01-03 11:52:09 -08:00
pom.xml Initial 0.9.0 release cleanup. 2022-01-18 17:16:43 -08:00
README.md Initial 0.9.0 release cleanup. 2022-01-18 17:16:43 -08:00
settings.gradle.kts Upgraded to Kotlin 1.5.21 and JDK 16. 2021-07-27 09:43:34 -07:00

License (3-Clause BSD) Release Maven Central

Known Vulnerabilities Quality Gate Status GitHub CI CircleCI

Retrieve cryptocurrencies current prices

A simple Kotlin/Java/Android implementation of the spot price Coinbase Public API.

Examples (TL;DR)

import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice

// ...

val btc = spotPrice("BTC") // Bitcoin
println(btc.amount)

val eth = spotPrice("ETH", "EUR") // Ethereum in Euros
println(eth.amount)

Gradle, Maven, etc.

To use with Gradle, include the following dependency in your build file:

dependencies {
    implementation("net.thauvin.erik:cryptoprice:0.9.0")
}

Instructions for using with Maven, Ivy, etc. can be found on Maven Central.

Spot Price

The spotPrice function defines the following parameters:

spotPrice(
    base: String, // Required 
    currency: String = "USD",
    date: LocalDate? = null,
)
Parameters Description
base The cryptocurrency ticker symbol (BTC, ETH, LTC, etc.)
currency The fiat currency ISO 4217 code. (USD, GBP, EUR, etc.)
date The LocalDate for historical price data.

A CryptoPrice object is returned defined as follows:

CryptoPrice(val base: String, val currency: String, val amount: BigDecimal)

The parameter names match the Coinbase API.

Format

To display the amount as a formatted currency, use the toCurrency function:

val euro = CryptoPrice("BTC", "EUR", 23456.78.toBigDecimal())
println(euro.toCurrency()) // €23,456.78

val krone = CryptoPrice("BTC", "DKK", 123456.78.toBigDecimal())
println(krone.toCurrency(Locale("da", "DK"))) // 123.456,78 kr.
JSON

To convert a CryptoPrice object back to JSON, use the toJson function:

val price = CryptoPrice("BTC", "USD", 34567.89.toBigDecimal())
println(price.toJson())

output:

{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}

The data object matches the Coinbase API. To specify a different (or no) key, use:

println(price.toJson("bitcoin"))
println(price.toJson("")) // or price.toString()

output:

{"bitcoin":{"base":"BTC","currency":"USD","amount":"34567.89"}}
{"base":"BTC","currency":"USD","amount":"34567.89"}

Similarly, to create a CryptoPrice object from JSON, use the toPrice function:

val btc = """{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}""".toPrice()
val eth = """{"ether":{"base":"ETH","currency":"USD","amount":"2345.67"}}""".toPrice("ether")

Extending

A generic apiCall() function is available to access other API data endpoints. For example to retrieve the current buy price of a cryptocurrency:

apiCall(paths = listOf("prices", "BTC-USD", "buy"), params = emptyMap())

will return something like:

{"data":{"base":"BTC","currency":"USD","amount":"34554.32"}}

See the examples for more details.