Skip to content

nyg/kraken-api-java

Repository files navigation

A Kraken API Client in Java

Maven Central

Query the Kraken REST API in Java.

Maven

<dependency>
    <groupId>dev.andstuff.kraken</groupId>
    <artifactId>kraken-api</artifactId>
    <version>2.0.0</version>
</dependency>

Usage

Public endpoints

Public endpoints that have been implemented by the library, can be queried in the following way:

KrakenAPI api = new KrakenAPI();

Map<String, AssetInfo> assets = api.assetInfo(List.of("BTC", "ETH"));
// {BTC=AssetInfo[assetClass=currency, alternateName=XBT, maxDecimals=10, …

Map<String, AssetPair> 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 deserialization libary:

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:

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:

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:

// 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:

public class MyRestTemplateRestRequester implements KrakenRestRequester {
    public <T> T execute(PublicEndpoint<T> endpoint) { /* your implementation */ }
    public <T> T execute(PrivateEndpoint<T> 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:

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.

# 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