The Coinbase Pro client library for Scala (coinbase4s-pro) allows crypto enthusiasts to start trading a variety of digital assets on Coinbase Pro.
- Provides simple, non-blocking interface for Coinbase Pro API
- Supports both RESTful API and Websocket feed
- Covers public as well as authenticated endpoints
- Takes care of nuances of the API like HMAC signatures and pagination
implicit val system = ActorSystem()
import system.dispatcher
implicit val materializer = ActorMaterializer()
Enable an API Key on your account in order to use private endpoints:
val auth = Auth("<key>", "<secret>", "<passphrase>")
Instantiate a client:
val baseUri = Uri("https://api.pro.coinbase.com")
// The second argument is optional and gives access to the private endpoints
val restClient = new RestClient(baseUri, Some(auth))
All methods, unless otherwise specified, are non-blocking and return an instance of scala.concurrent.Future
:
restClient
.getProducts
.onComplete {
case Success(result) =>
// process the data
case Failure(ex) =>
// handle the error
}
Some endpoints return paginated data. Corresponding methods return an iterator that allows to easily navigate the result set. The following example will fetch and print all orders by making multiple HTTP requests behind the scenes:
val resultSet = restClient.getOrders(Some(productId), List("done"))
val printAllOrders: (ResultSet[Order]) => Unit = (rs) => rs.next.onComplete {
case Success(orders) =>
orders.foreach(println)
printAllOrders(rs)
case Failure(ex: NoSuchElementException) =>
// No-op
case Failure(ex) =>
println(ex)
}
printAllOrders(resultSet)
-
Accounts
restClient.getAccounts()
restClient.getAccount("83a53e58-ff4d-4938-956a-dc84c17b818d")
restClient.getAccountActivity("83a53e58-ff4d-4938-956a-dc84c17b818d")
restClient.getAccountHolds("83a53e58-ff4d-4938-956a-dc84c17b818d")
-
Orders
val clientOid = UUID.randomUUID.toString val size = BigDecimal("0.1") val price = BigDecimal("3999.99") val order = LimitOrder(None, Some(clientOid), productId, OrderSide.Buy, size, price) restClient.placeOrder(order)
restClient.cancelOrder("5eb77d6c-0004-45e1-818f-2ac8593b0fae")
// Cancel all orders restClient.cancelOrders() // Cancel all orders for product restClient.cancelOrders(productId)
restClient.getOrders(Some(productId), List("pending", "open"))
restClient.getOrder("6f89e035-eac3-4389-99e7-6e20b0093354")
-
Fills
// Get fills by product restClient.getFillsByProduct(productId) // Get fills by order restClient.getFillsByOrder("4007468d-581f-43ce-989f-aed92e986720")
-
Products
restClient.getProducts()
val level = 1 restClient.getProductOrderBook(productId, level)
restClient.getProductTicker(productId)
restClient.getProductTrades(productId)
val start = ZonedDateTime.parse("2018-12-25T00:00:00+00:00") val end = ZonedDateTime.parse("2018-12-25T01:59:00+00:00") val granularity = Granularity.OneMinute restClient.getProductCandles(productId, start, end, granularity)
restClient.getProductStats(productId)
-
Currencies
restClient.getCurrencies()
-
Time
restClient.getTime()
The WebsocketClient allows you to connect and listen to the exchange websocket messages:
val webSocketUri = Uri("wss://ws-feed.pro.coinbase.com")
// The second argument is optional and gives access to the private endpoints
val wsClient = new WebSocketClient(webSocketUri, Some(auth))
// This will print all incoming messages for BTC-USD product in level2 channel
val productId = ProductId("BTC", "USD")
val channel = Channel(ChannelName.Level2)
val callback = (msg: WebSocketMessage) => println(msg)
wsClient.connect(List(productId), List(channel), callback)
wsClient.disconnect()