Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nyg/kraken-api-java
Java library to query the Kraken REST API.
https://github.com/nyg/kraken-api-java
bitcoin kraken kraken-api kraken-exchange-api
Last synced: 2 days ago
JSON representation
Java library to query the Kraken REST API.
- Host: GitHub
- URL: https://github.com/nyg/kraken-api-java
- Owner: nyg
- License: isc
- Created: 2015-10-16T22:34:02.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-10-31T23:34:39.000Z (13 days ago)
- Last Synced: 2024-11-01T00:24:18.454Z (13 days ago)
- Topics: bitcoin, kraken, kraken-api, kraken-exchange-api
- Language: Java
- Homepage: https://central.sonatype.com/artifact/dev.andstuff.kraken/kraken-api
- Size: 105 KB
- Stars: 53
- Watchers: 8
- Forks: 28
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# A Kraken API Client in Java
[![Maven Central](https://img.shields.io/maven-central/v/dev.andstuff.kraken/kraken-api)](https://central.sonatype.com/artifact/dev.andstuff.kraken/kraken-api)
Query the [Kraken REST API][1] in Java.
## Maven
```xml
dev.andstuff.kraken
kraken-api
2.0.0```
## Usage
### Public endpoints
Public endpoints that have been implemented by the library, can be queried in the following way:
```java
KrakenAPI api = new KrakenAPI();Map assets = api.assetInfo(List.of("BTC", "ETH"));
// {BTC=AssetInfo[assetClass=currency, alternateName=XBT, maxDecimals=10, …Map pairs = api.assetPairs(List.of("ETH/BTC", "ETH/USD"));
// {ETH/BTC=AssetPair[alternateName=ETHXBT, webSocketName=ETH/XBT, …
```If the endpoint has not yet been implemented (feel free to submit a PR!), the generic `query` method can be used, which will return a `JsonNode` of the [Jackson][2] deserialization libary:
```java
JsonNode ticker = api.query(KrakenAPI.Public.TICKER, Map.of("pair", "XBTEUR"));
// {"XXBTZEUR":{"a":["62650.00000","1","1.000"],"b":["62649.90000","6","6.000"], …
```It's also possible to specify the path directly, in case a new endpoint has been added by Kraken and not yet added in the library:
```java
JsonNode trades = api.queryPublic("Trades", Map.of("pair", "XBTUSD", "count", "1"));
// {"XXBTZUSD":[["68515.60000","0.00029628",1.7100231295628998E9,"s","m","",68007835]], …
```### Private endpoints
Private endpoints can be queried in the same way as the public ones, but an API key and secret must be provided to the `KrakenAPI` instance:
```java
KrakenAPI api = new KrakenAPI("my key", "my secret");JsonNode balance = api.query(KrakenAPI.Private.BALANCE);
// {"XXBT":"1234.8396278900", … :)
```If the Kraken API call returns an error, an unchecked exception of type `KrakenException` is thrown:
```java
// API key doesn't have the permission to create orders
JsonNode order = api.query(KrakenAPI.Private.ADD_ORDER, Map.of(
"ordertype", "limit", "type", "sell",
"volume", "1", "pair", "XLTCZUSD",
"price", "1000", "oflags", "post,fciq",
"validate", "true"));
// Exception in thread "main" KrakenException(errors=[EGeneral:Permission denied])
```### Custom REST requester
The current implementation of the library uses the JDK's HttpsURLConnection to make HTTP request. If that doesn't suit your needs and which to use something else (e.g. Spring RestTemplate, Apache HttpComponents, OkHttp), you can implement the KrakenRestRequester interface and pass it to the KrakenAPI constructor:
```java
public class MyRestTemplateRestRequester implements KrakenRestRequester {
public T execute(PublicEndpoint endpoint) { /* your implementation */ }
public T execute(PrivateEndpoint endpoint) { /* your implementation */ }
}KrakenAPI api = new KrakenAPI(MyRestTemplateRestRequest(apiKey, apiSecret));
```See `DefaultKrakenRestRequester` for the default implementation.
### Custom nonce generator
For private endpoint requests, the nonce value is set to `System.currentTimeMillis()`. If you wish to use another value, you can specify a custom nonce generator when creating the `KrakenAPI` instance:
```java
KrakenAPI api = new KrakenAPI(
new KrakenCredentials(key, secret),
() -> Long.toString(System.currentTimeMillis() / 1000));
```The second parameter is of type `KrakenNonceGenerator`, an interface containing a single `generate()` method returning a string.
## Examples
The `examples` Maven module contains some examples that might be worth checking (e.g. total staking rewards summary). The examples can be run directly from your IDE, or from the command line.
For private endpoints, you need to rename `api-keys.properties.example` (located in `examples/src/main/resources/`) to `api-keys.properties` and fill in your API keys.
```shell
# build project
mvn clean install# run example classes
mvn -q -pl examples exec:java -Dexec.mainClass=dev.andstuff.kraken.example.SimpleExamples
mvn -q -pl examples exec:java -Dexec.mainClass=dev.andstuff.kraken.example.StakingRewardsSummaryExample
```[1]: https://docs.kraken.com/rest/
[2]: https://github.com/FasterXML/jackson
[3]: https://github.com/nyg/kraken-api-java/blob/v1.0.0/examples/src/main/java/dev/andstuff/kraken/example/Examples.java