The detailed document https://docs.kucoin.com/futures, in order to receive the latest API change notifications, please
Watch
this repository.
go get github.com/Kucoin/kucoin-futures-go-sdk
Environment | BaseUri |
---|---|
Production | https://api-futures.kucoin.com(DEFAULT) https://api-futures.kucoin.cc |
Sandbox | https://api-sandbox-futures.kucoin.com |
To reinforce the security of the API, KuCoin upgraded the API key to version 2.0, the validation logic has also been changed. It is recommended to create(https://www.kucoin.com/account/api) and update your API key to version 2.0. The API key of version 1.0 will be still valid until May 1, 2021.
// API key version 2.0
s := kucoin.NewApiService(
// kucoin.ApiBaseURIOption("https://api.kucoin.com"),
kucoin.ApiKeyOption("key"),
kucoin.ApiSecretOption("secret"),
kucoin.ApiPassPhraseOption("passphrase"),
kucoin.ApiKeyVersionOption(ApiKeyVersionV2)
)
// API key version 1.0
s := kucoin.NewApiService(
// kucoin.ApiBaseURIOption("https://api.kucoin.com"),
kucoin.ApiKeyOption("key"),
kucoin.ApiSecretOption("secret"),
kucoin.ApiPassPhraseOption("passphrase"),
)
// Or add these options into the environmental variable
// Bash:
// export API_BASE_URI=https://api-futures.kucoin.com
// export API_KEY=key
// export API_SECRET=secret
// export API_PASSPHRASE=passphrase
// s := NewApiServiceFromEnv()
// Require package github.com/sirupsen/logrus
// Debug mode will record the logs of API and WebSocket to files.
// Default values: LogLevel=logrus.DebugLevel, LogDirectory="/tmp"
kumex.DebugMode = true
// Or export API_DEBUG_MODE=1
// Logging in your code
// kumex.SetLoggerDirectory("/tmp")
// logrus.SetLevel(logrus.DebugLevel)
logrus.Debugln("I'm a debug message")
See the test case for more examples.
without
authenticationrsp, err := s.ServerTime()
if err != nil {
log.Printf("Error: %s", err.Error())
// Handle error
return
}
var ts int64
if err := rsp.ReadData(&ts); err != nil {
// Handle error
return
}
log.Printf("The server time: %d", ts)
with
authentication// Without pagination
rsp, err := s.AccountOverview()
if err != nil {
// Handle error
return
}
as := kumex.AccountsModel{}
if err := rsp.ReadData(&as); err != nil {
// Handle error
return
}
for _, a := range as {
log.Printf("Available balance: %s %s => %s", a.Type, a.Currency, a.Available)
}
// Handle pagination
rsp, err := s.Orders(map[string]string{}, &kumex.PaginationParam{CurrentPage: 1, PageSize: 10})
if err != nil {
// Handle error
return
}
os := kumex.OrdersModel{}
pa, err := rsp.ReadPaginationData(&os)
if err != nil {
// Handle error
return
}
log.Printf("Total num: %d, total page: %d", pa.TotalNum, pa.TotalPage)
for _, o := range os {
log.Printf("Order: %s, %s, %s", o.Id, o.Type, o.Price)
}
Require package gorilla/websocket
go get github.com/gorilla/websocket github.com/pkg/errors
rsp, err := s.WebSocketPublicToken()
if err != nil {
// Handle error
return
}
tk := &kumex.WebSocketTokenModel{}
if err := rsp.ReadData(tk); err != nil {
// Handle error
return
}
c := s.NewWebSocketClient(tk)
// c.AcceptUserMessage = true
mc, ec, err := c.Connect()
if err != nil {
// Handle error
return
}
ch1 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)
ch2 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)
uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false)
if err := c.Subscribe(ch1, ch2); err != nil {
// Handle error
return
}
var i = 0
for {
select {
case err := <-ec:
c.Stop() // Stop subscribing the WebSocket feed
log.Printf("Error: %s", err.Error())
// Handle error
return
case msg := <-mc:
// log.Printf("Received: %s", kumex.ToJsonString(m))
t := &kumex.TickerLevel1Model{}
if err := msg.ReadData(t); err != nil {
log.Printf("Failure to read: %s", err.Error())
return
}
log.Printf("Ticker: %s, %s, %s, %s", msg.Topic, t.Sequence, t.Price, t.Size)
i++
if i == 5 {
log.Println("Unsubscribe XBTUSDTM")
if err = c.Unsubscribe(uch); err != nil {
log.Printf("Error: %s", err.Error())
// Handle error
return
}
}
if i == 10 {
log.Println("Subscribe XBTUSDTM")
if err = c.Subscribe(ch2); err != nil {
log.Printf("Error: %s", err.Error())
// Handle error
return
}
}
if i == 15 {
log.Println("Exit subscription")
c.Stop() // Stop subscribing the WebSocket feed
return
}
}
}
# Add your API configuration items into the environmental variable first
export API_BASE_URI=https://api-futures.kucoin.com
export API_KEY=key
export API_SECRET=secret
export API_PASSPHRASE=passphrase
export API_KEY_VERSION=2
# Run tests
go test -v