bitmexgo: a golang library for Bitmex REST APIs

Zhen Wang
2 min readOct 25, 2018

--

I’ve open-sourced bitmexgo, a golang package that enables clients to call REST APIs for the Bitmex crypto trading platform. You may use it to analyze the most liquid crypto futures market or develop trading bots in Go.

Bitmex’s official golang library, which is automatically generated by swagger-codegen, turns out to be nonfunctional: due to its broken authentication flow, none of the APIs works at all. Ideally the bugs should be fixed at the swagger layer, but it appears to be a pretty thorny problem. Therefore I directly applied patches on top of the auto-generated code and created this new bitmexgo package.

In addition to bug fixes, I’ve rubbed out all external dependencies from the original code base to make bitmexgo leaner and self-contained.

To install the package, run the following shell command:

go get github.com/zmxv/bitmexgo

Apart from a few simplifications here and there, bitmexgo’s API adheres to the original interface quite faithfully. For example:

import "github.com/zmxv/bitmexgo"

// Get your API key/secret pair
// at https://www.bitmex.com/app/apiKeys
apiKey := "..."
apiSecret := "..."

// Create an authentication context
auth := bitmexgo.NewAPIKeyContext(apiKey, apiSecret)

// Create a shareable API client instance
apiClient := bitmexgo.NewAPIClient(bitmexgo.NewConfiguration())

// Call APIs without parameters by passing the auth context.
// e.g. getting exchange-wide turnover and volume statistics:
stats, res, err := apiClient.StatsApi.StatsGet(auth)

// Call APIs with default parameters by passing the auth context
// and a nil.
// e.g. getting all open positions:
pos, res, err := apiClient.PositionApi.PositionGet(auth, nil)

// Call APIs with additional parameters by constructing
// a corresponding XXXOpts struct.
// e.g. submitting a limit order to buy 20000 contracts of
// XBTUSD at $6000.5:
var params bitmexgo.OrderNewOpts
params.OrdType.Set("Limit")
params.Side.Set("Buy")
params.OrderQty.Set(20000)
params.Price.Set(6000.5)
order, res, err := apiClient.OrderApi.OrderNew(auth, "XBTUSD", &params)

The swagger-generated code contains no tests, neither does the bitmexgo package. I have, however, manually tested many of its APIs in a side project without any issues.

As required by Bitmex, bitmexgo needs an API key/secret pair to work, which can be created at https://www.bitmex.com/app/apiKeys.

--

--