paoloboni / binance-scala-client   1.6.1

MIT License GitHub

A functional Scala client for Binance

Scala versions: 3.x 2.13 2.12

Scala client for Binance API

All Contributors

Build Status Latest version License: MIT Join the chat at https://gitter.im/binance-scala-client/community Join the chat on Discord at https://discord.gg/7KrBehYs55 Support

A functional Scala client for Binance, powered by cats-effect and fs2.

This client is rate limited, based on Binance API specification.

Version compatibility table

JDK LTS version Scala version
11 2.12.x, 2.13.x, 3.0.x, 3.1.x
17 2.12.x, 2.13.x, 3.0.x, 3.1.x

Getting started

If you use sbt add the following dependency to your build file:

libraryDependencies += "io.github.paoloboni" %% "binance-scala-client" % "<version>"

APIs supported

Spot API

Rest

WebSocket

Future API

Rest

WebSocket

Initialise the client

The client initialisation returns an API object as a Cats Resource. There are two factories available at the moment, one for Spot API and the other one for Future API.

Spot client

import cats.effect.{IO, Resource}
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.SpotConfig
import io.github.paoloboni.binance.spot.SpotApi

val config = SpotConfig.Default(
  apiKey = "***",     // your api key
  apiSecret = "***"   // your api secret
)
val client: Resource[IO, SpotApi[IO]] = BinanceClient.createSpotClient[IO](config)

Future client

import cats.effect.{IO, Resource}
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.FapiConfig
import io.github.paoloboni.binance.fapi.FutureApi

val config = FapiConfig.Default(
  apiKey = "***",     // your api key
  apiSecret = "***"   // your api secret
)
val client: Resource[IO, FutureApi[IO]] = BinanceClient.createFutureClient[IO](config)

Documentation

The API documentation is available here.

Example app

This is a sample app to monitor the exchange prices (fetch every 5 seconds).

import cats.effect.std.Console
import cats.effect.{IO, IOApp}
import fs2.Stream
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.SpotConfig

import scala.concurrent.duration.DurationInt

object PriceMonitor extends IOApp.Simple {

  val config = SpotConfig.Default(
    apiKey = "***",
    apiSecret = "***"
  )

  override def run: IO[Unit] =
    BinanceClient
      .createSpotClient[IO](config)
      .use { client =>
        Stream
          .awakeEvery[IO](5.seconds)
          .repeat
          .evalMap(_ => client.V3.getPrices())
          .evalMap(prices => Console[IO].println("Current prices: " + prices))
          .compile
          .drain
      }
}

Contributing

How to run unit and integration tests

sbt test

How to run end-to-end tests

FAPI_API_KEY=<your-fapi-api-key> \
    FAPI_SECRET_KEY=<your-fapi-secret-key> \
    SPOT_API_KEY=<your-spot-api-key> \
    SPOT_SECRET_KEY=<your-spot-secret-key> \
    sbt e2e:test

How to get Spot API and secret keys

  • Navigate to https://testnet.binance.vision
  • Login with your Github account
  • Click on "Generate HMAC_SHA256 Key"
  • Enter a description and press "Generate"
  • Take note of the generated values

How to get Future API and secret keys

  • Navigate to https://testnet.binancefuture.com
  • Register a new account or login with an existing one
  • Click on "API key" link in the lower part of the screen
  • Take note of the values

Contributors ✨

Thanks goes to these wonderful people (emoji key):


DarkWingMcQuack

💻

Swoorup Joshi

💻

This project follows the all-contributors specification. Contributions of any kind welcome!