Skip to content
A Go SDK for Binance API
Go
Branch: master
Clone or download
Latest commit b280588 Aug 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
vendor/github.com update dependencies (#20) Feb 26, 2018
.codecov.yml Add .codecov.yml Jan 19, 2018
.gitignore Init repo Oct 25, 2017
.travis.yml update golint Oct 11, 2018
Gopkg.lock update dependencies (#20) Feb 26, 2018
Gopkg.toml update dependencies (#20) Feb 26, 2018
LICENSE Initial commit Oct 25, 2017
README.md Fix typo Aug 13, 2019
account_service.go code quality improvements (#21) Feb 26, 2018
account_service_test.go Fix typos Jan 19, 2018
client.go Add marge order Aug 8, 2019
client_test.go Add marge order Aug 8, 2019
deposit_service.go Add returned transaction id to Deposit struct (#42) Apr 3, 2018
deposit_service_test.go Add returned transaction id to Deposit struct (#42) Apr 3, 2018
depth_service.go code quality improvements (#21) Feb 26, 2018
depth_service_test.go Unexport SetParam Oct 30, 2017
doc.go Go doc support Oct 25, 2017
errors.go Init repo Oct 25, 2017
exchange_info_service.go Add symbol filters Jul 31, 2019
exchange_info_service_test.go Add symbol filters Jul 31, 2019
helpers.go Update with v3 APIs Jul 29, 2019
helpers_test.go added helper function for converting amounts to lot sized amount Mar 20, 2018
kline_service.go code quality improvements (#21) Feb 26, 2018
kline_service_test.go Unexport SetParam Oct 30, 2017
margin_order_service.go Add marge order Aug 8, 2019
margin_order_service_test.go Add marge order Aug 8, 2019
margin_service.go Add marge order Aug 8, 2019
margin_service_test.go Add marge order Aug 8, 2019
order_service.go Update with v3 APIs Jul 29, 2019
order_service_test.go Add margin service Aug 3, 2019
request.go Unexport SetParam Oct 30, 2017
server_service.go code quality improvements (#21) Feb 26, 2018
server_service_test.go Init repo Oct 25, 2017
ticker_service.go Update with v3 APIs Jul 29, 2019
ticker_service_test.go Update with v3 APIs Jul 29, 2019
trade_service.go Update with v3 APIs Jul 29, 2019
trade_service_test.go Add marge order Aug 8, 2019
user_stream_service.go code quality improvements (#21) Feb 26, 2018
user_stream_service_test.go Unexport SetParam Oct 30, 2017
websocket.go dont use goroutine for every ws frame (#65) Oct 12, 2018
websocket_service.go add CombinedPartialDepthServe (#60) Sep 28, 2018
websocket_service_test.go add CombinedPartialDepthServe (#60) Sep 28, 2018
withdraw_service.go fix withdraw api (#82) Apr 26, 2019
withdraw_service_test.go fix withdraw api (#82) Apr 26, 2019

README.md

go-binance

A Golang SDK for binance API.

Build Status GoDoc Go Report Card codecov

All the REST APIs listed in binance API document are implemented, as well as the websocket APIs.

For best compatibility, please use Go >= 1.8.

Make sure you have read binance API document before continuing.

API List

Name Description Status
rest-api.md Details on the Rest API (/api) Implemented
web-socket-streams.md Details on available streams and payloads Implemented
user-data-stream.md Details on the dedicated account stream Implemented
wapi-api.md Details on the Withdrawal API (/wapi) Partially Implemented
margin-api.md Details on the Margin API (/sapi) Implemented

Installation

go get github.com/adshao/go-binance

Importing

import (
    "github.com/adshao/go-binance"
)

Documentation

GoDoc

REST API

Setup

Init client for API services. Get APIKey/SecretKey from your binance account.

var (
    apiKey = "your api key"
    secretKey = "your secret key"
)
client := binance.NewClient(apiKey, secretKey)

A service instance stands for a REST API endpoint and is initialized by client.NewXXXService function.

Simply call API in chain style. Call Do() in the end to send HTTP request.

Following are some simple examples, please refer to godoc for full references.

Create Order

order, err := client.NewCreateOrderService().Symbol("BNBETH").
        Side(binance.SideTypeBuy).Type(binance.OrderTypeLimit).
        TimeInForce(binance.TimeInForceTypeGTC).Quantity("5").
        Price("0.0030000").Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(order)

// Use Test() instead of Do() for testing.

Get Order

order, err := client.NewGetOrderService().Symbol("BNBETH").
    OrderID(4432844).Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(order)

Cancel Order

_, err := client.NewCancelOrderService().Symbol("BNBETH").
    OrderID(4432844).Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}

List Open Orders

openOrders, err := client.NewListOpenOrdersService().Symbol("BNBETH").
    Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, o := range openOrders {
    fmt.Println(o)
}

List Orders

orders, err := client.NewListOrdersService().Symbol("BNBETH").
    Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, o := range orders {
    fmt.Println(o)
}

List Ticker Prices

prices, err := client.NewListPricesService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, p := range prices {
    fmt.Println(p)
}

Show Depth

res, err := client.NewDepthService().Symbol("LTCBTC").
    Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(res)

List Klines

klines, err := client.NewKlinesService().Symbol("LTCBTC").
    Interval("15m").Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, k := range klines {
    fmt.Println(k)
}

List Aggregate Trades

trades, err := client.NewAggTradesService().
    Symbol("LTCBTC").StartTime(1508673256594).EndTime(1508673256595).
    Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, t := range trades {
    fmt.Println(t)
}

Get Account

res, err := client.NewGetAccountService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(res)

Start User Stream

res, err := client.NewStartUserStreamService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(res)

Websocket

You don't need Client in websocket API. Just call binance.WsXxxServe(args, handler, errHandler).

Depth

wsDepthHandler := func(event *binance.WsDepthEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, stopC, err := binance.WsDepthServe("LTCBTC", wsDepthHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
// use stopC to exit
go func() {
    time.Sleep(5 * time.Second)
    stopC <- struct{}{}
}()
// remove this if you do not want to be blocked here
<-doneC

Kline

wsKlineHandler := func(event *binance.WsKlineEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, _, err := binance.WsKlineServe("LTCBTC", "1m", wsKlineHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
<-doneC

Aggregate

wsAggTradeHandler := func(event *binance.WsAggTradeEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, _, err := binance.WsAggTradeServe("LTCBTC", wsAggTradeHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
<-doneC

User Data

wsHandler := func(message []byte) {
    fmt.Println(string(message))
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, _, err := binance.WsUserDataServe(listenKey, wsHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
<-doneC
You can’t perform that action at this time.