A Golang SDK for phemex API.
Most REST APIs listed in phemex API document are implemented, as well as the AOP websocket APIs.
For best compatibility, please use Go >= 1.8.
Make sure you have read phemex API document before continuing.
Name | Description | Status |
---|---|---|
rest-api.md | Details on the Rest API |
go get github.com/Krisa/go-phemex
import (
"github.com/Krisa/go-phemex"
)
Init client for API services. Get APIKey/SecretKey from your phemex account.
var (
apiKey = "your api key"
secretKey = "your secret key"
)
client := phemex.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.
order, err := client.NewCreateOrderService().Symbol("BTCUSD").
Side(phemex.SideTypeBuy).Type(phemex.OrderTypeLimit).
TimeInForce(phemex.TimeInForceTypeGTC).Quantity("5").
Price("1000").Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(order)
_, err := client.NewCancelOrderService().Symbol("BTCUSD").
OrderID(4432844).Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
openOrders, err := client.NewListOpenOrdersService().Symbol("BTCUSD").
Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
for _, o := range openOrders {
fmt.Println(o)
}
res, err := client.NewGetAccountPositionService().Currency("BTC").Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
You initiate PhemexClient the same way
wsHandler := func(message interface{}) {
switch message := message.(type) {
case *phemex.WsAOP:
// snapshots / increments
case *phemex.WsPositionInfo:
// when a position is active
case *phemex.WsError:
// on connection
}
}
errHandler := func(err error) {
// initiate reconnection with `once.Do...`
}
auth := PhemexClient.NewWsAuthService()
if test {
auth = auth.URL("wss://testnet.phemex.com/ws")
}
c, err := auth.Do(context.Background())
// err handling
err = PhemexClient.NewStartWsAOPService().SetID(1).Do(c, wsHandler, errHandler)
// err handling