A Golang SDK for binance API.
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.
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 |
margin-api.md | Details on the Margin API (/sapi) | Implemented |
futures-api.md | Details on the Futures API (/fapi) | Partially Implemented |
delivery-api.md | Details on the Coin-M Futures API (/dapi) | Partially Implemented |
go get github.com/sfwtoms2030/go-binance/v2
For v1 API, it has been moved to v1
branch, please use:
go get github.com/sfwtoms2030/go-binance/v1
import (
"github.com/sfwtoms2030/go-binance/v2"
)
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)
futuresClient := binance.NewFuturesClient(apiKey, secretKey) // USDT-M Futures
deliveryClient := binance.NewDeliveryClient(apiKey, secretKey) // Coin-M Futures
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.
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.
order, err := client.NewGetOrderService().Symbol("BNBETH").
OrderID(4432844).Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(order)
_, err := client.NewCancelOrderService().Symbol("BNBETH").
OrderID(4432844).Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
openOrders, err := client.NewListOpenOrdersService().Symbol("BNBETH").
Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
for _, o := range openOrders {
fmt.Println(o)
}
orders, err := client.NewListOrdersService().Symbol("BNBETH").
Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
for _, o := range orders {
fmt.Println(o)
}
prices, err := client.NewListPricesService().Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
for _, p := range prices {
fmt.Println(p)
}
res, err := client.NewDepthService().Symbol("LTCBTC").
Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
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)
}
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)
}
res, err := client.NewGetAccountService().Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
res, err := client.NewStartUserStreamService().Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
You don't need Client in websocket API. Just call binance.WsXxxServe(args, handler, errHandler).
For delivery API you can use
delivery.WsXxxServe(args, handler, errHandler)
.
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
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
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
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
Your system time may be incorrect and you may use following function to set the time offset based off Binance Server Time:
// use the client future for Futures
client.NewSetServerTimeService().Do(context.Background())
Or you can also overwrite the TimeOffset
yourself:
client.TimeOffset = 123
You can use the testnet by enabling the corresponding flag.
Note that you can't use your regular API and Secret keys for the testnet. You have to create an account on the testnet websites : https://testnet.binancefuture.com/ for futures and delivery or https://testnet.binance.vision/ for the Spot Test Network.
Use the binance.UseTestnet
flag before calling the client creation and the websockets methods.
import (
"github.com/sfwtoms2030/go-binance/v2"
)
binance.UseTestnet = true
client := binance.NewClient(apiKey, secretKey)
Use the futures.UseTestnet
flag before calling the client creation and the websockets methods
import (
"github.com/sfwtoms2030/go-binance/v2/futures"
)
futures.UseTestnet = true
BinanceClient = futures.NewClient(ApiKey, SecretKey)
Use the delivery.UseTestnet
flag before calling the client creation and the websockets methods
import (
"github.com/sfwtoms2030/go-binance/v2/delivery"
)
do some test
delivery.UseTestnet = true
BinanceClient = delivery.NewClient(ApiKey, SecretKey)