Skip to content

JKorf/BitMEX.Net

Repository files navigation

BitMEX.Net BitMEX.Net

.NET License Since

BitMEX.Net is a client library for accessing the BitMEX REST and Websocket API.

Features

  • Response data is mapped to descriptive models
  • Input parameters and response values are mapped to discriptive enum values where possible
  • High performance
  • Automatic websocket (re)connection management
  • Client side rate limiting
  • Client side order book implementation
  • Support for managing different accounts
  • Extensive logging
  • Support for different environments
  • Easy integration with other exchange client based on the CryptoExchange.Net base library
  • Native AOT support

Supported Frameworks

The library is targeting both .NET Standard 2.0 and .NET Standard 2.1 for optimal compatibility, as well as the latest dotnet versions to use the latest framework features.

.NET implementation Version Support
.NET Core 2.0 and higher
.NET Framework 4.6.1 and higher
Mono 5.4 and higher
Xamarin.iOS 10.14 and higher
Xamarin.Android 8.0 and higher
UWP 10.0.16299 and higher
Unity 2018.1 and higher

Install the library

NuGet

NuGet version Nuget downloads

dotnet add package JKorf.BitMEX.Net

GitHub packages

BitMEX.Net is available on GitHub packages. You'll need to add https://nuget.pkg.github.com/JKorf/index.json as a NuGet package source.

Download release

GitHub Release

The NuGet package files are added along side the source with the latest GitHub release which can found here.

How to use

Basic request:

// Get the ETH/USDT ticker via rest request
var restClient = new BitMEXRestClient();
var tickerResult = await restClient.ExchangeApi.ExchangeData.GetSymbolsAsync("ETHUSDT");
var lastPrice = tickerResult.Data.Single().LastPrice;

Place order:

var restClient = new BitMEXRestClient(opts => {
	opts.ApiCredentials = new BitMEXCredentials("APIKEY", "APISECRET");
});

// Place Limit order to go long for 10 contracts ETH/USD at 2000
var orderResult = await restClient.ExchangeApi.Trading.PlaceOrderAsync(
    "ETH_USD",
    OrderSide.Buy,
    OrderType.Limit,
    10,    
    2000);

WebSocket subscription:

// Subscribe to ETH/USDT ticker updates via the websocket API
var socketClient = new BitMEXSocketClient();
var tickerSubscriptionResult = socketClient.ExchangeApi.SubscribeToSymbolUpdatesAsync("ETHUSDT", (update) =>
{
	// If update.Data.LastPrice == null the price hasn't changed since last update
	if (update.Data.LastPrice != null)
	{ 
		var lastPrice = update.Data.LastPrice;
	}
});

For information on the clients, dependency injection, response processing and more see the documentation, or have a look at the examples here or here.

AI documentation

For AI coding assistants and quick onboarding:

See cryptoexchange-skills-hub for installable skills.

BitMEX Quantities

BitMEX handles quantities a bit differently than most exchange API's. Asset quantities like account balances are denoted in a base value.
For example 9846 XBt instead of 0.00009846 BTC. The same logic is also applied to trading quantities, if you want to place a spot order for 0.1 BTC you'd need to specify a quantity of 10000000 XBt. Note that futures trading works with contracts.
How many decimal places are used for each asset and symbol can be requested using the restClient.ExchangeApi.ExchangeData.GetAssetsAsync and restClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync endpoints.

The library offers an easy way of converting between these quantities with the ToSharedAssetQuantity, ToSharedSymbolQuantity, ToBitMEXAssetQuantity and ToBitMEXSymbolQuantity extension methods. For example:
Getting balance info

// Make sure to call this at least once at the start of the program to retrieve the conversion data
await BitMEXUtils.UpdateSymbolInfoAsync();

var balances = await bitMEXRestClient.ExchangeApi.Account.GetBalancesAsync();
foreach (var balance in balances.Data)
{
    var bitMEXQuantity = balance.Quantity; // The quantity as used in the BitMEX API, for example 1000000
    var bitMEXAssetName = balance.Currency; // The currency name as used in the BitMEX API, for example `gwei`
    var assetQuantity = balance.Quantity.ToSharedAssetQuantity(balance.Currency); // The quantity in the actual assset, for example 0.01
    var assetName = BitMEXUtils.GetAssetFromCurrency(balance.Currency); // The asset name, for example `ETH`
}

Placing spot order

// Make sure to call this at least once at the start of the program to retrieve the conversion data
await BitMEXUtils.UpdateSymbolInfoAsync();

var symbols = await bitMEXRestClient.ExchangeApi.ExchangeData.GetActiveSymbolsAsync();
var ethUsdtSpotSymbol = symbols.Data.SingleOrDefault(x => x.BaseAsset == "ETH" && x.QuoteAsset == "USDT" && x.SymbolType == SymbolType.Spot);
var minQuantityInBaseUnit = ethUsdtSpotSymbol.LotSize; // For example 1000
var minQuantityInETH = ethUsdtSpotSymbol.LotSize.ToSharedSymbolQuantity(ethUsdtSpotSymbol.Symbol); // For example 0.001

var quantityToPlace = 0.1m;
var quantityBitMEX = quantityToPlace.ToBitMEXSymbolQuantity(ethUsdtSpotSymbol.Symbol); // For example 1000000
var result = await bitMEXRestClient.ExchangeApi.Trading.PlaceOrderAsync(ethUsdtSpotSymbol.Symbol, OrderSide.Buy, OrderType.Market, quantityBitMEX);

CryptoExchange.Net

BitMEX.Net is based on the CryptoExchange.Net base library. Other exchange API implementations based on the CryptoExchange.Net base library are available and follow the same logic.

CryptoExchange.Net also allows for easy access to different exchange API's.

Exchange Repository Nuget
Aster JKorf/Aster.Net Nuget version
Binance JKorf/Binance.Net Nuget version
BingX JKorf/BingX.Net Nuget version
Bitfinex JKorf/Bitfinex.Net Nuget version
Bitget JKorf/Bitget.Net Nuget version
BitMart JKorf/BitMart.Net Nuget version
Bitstamp JKorf/Bitstamp.Net Nuget version
BloFin JKorf/BloFin.Net Nuget version
Bybit JKorf/Bybit.Net Nuget version
Coinbase JKorf/Coinbase.Net Nuget version
CoinEx JKorf/CoinEx.Net Nuget version
CoinGecko JKorf/CoinGecko.Net Nuget version
CoinW JKorf/CoinW.Net Nuget version
Crypto.com JKorf/CryptoCom.Net Nuget version
DeepCoin JKorf/DeepCoin.Net Nuget version
Gate.io JKorf/GateIo.Net Nuget version
HTX JKorf/HTX.Net Nuget version
HyperLiquid JKorf/HyperLiquid.Net Nuget version
Gate.io JKorf/GateIo.Net Nuget version
Kraken JKorf/Kraken.Net Nuget version
Kucoin JKorf/Kucoin.Net Nuget version
Lighter JKorf/Lighter.Net Nuget version
Mexc JKorf/Mexc.Net Nuget version
OKX JKorf/OKX.Net Nuget version
Polymarket JKorf/Polymarket.Net Nuget version
Toobit JKorf/Toobit.Net Nuget version
Upbit JKorf/Upbit.Net Nuget version
Weex JKorf/Weex.Net Nuget version
WhiteBit JKorf/WhiteBit.Net Nuget version
XT JKorf/XT.Net Nuget version

When using multiple of these API's the CryptoClients.Net package can be used which combines this and the other packages and allows easy access to all exchange API's.

Discord

Nuget version
A Discord server is available here. For discussion and/or questions around the CryptoExchange.Net and implementation libraries, feel free to join.

Supported functionality

Rest API

API Supported Location
Address restClient.ExchangeApi.Account
AddressConfig restClient.ExchangeApi.Account
Announcement restClient.ExchangeApi.ExchangeData
ApiKey restClient.ExchangeApi.Account
Chat X
Execution restClient.ExchangeApi.Trading
Funding restClient.ExchangeApi.ExchangeData
Guild X
Instrument restClient.ExchangeApi.ExchangeData
Insurance restClient.ExchangeApi.ExchangeData
Leaderboard X
Liquidation restClient.ExchangeApi.ExchangeData
Order restClient.ExchangeApi.Trading
OrderBook restClient.ExchangeApi.ExchangeData
Porl X
Position restClient.ExchangeApi.Trading
Quote restClient.ExchangeApi.ExchangeData
ReferralCode X
Schema X
Settlement restClient.ExchangeApi.ExchangeData
Stats restClient.ExchangeApi.ExchangeData
Trade restClient.ExchangeApi.ExchangeData
User restClient.ExchangeApi.Account
UserAffiliates X
UserEvent restClient.ExchangeApi.Account
Wallet restClient.ExchangeApi.ExchangeData

WebSocket API

API Supported Location
Public restClient.ExchangeApi
Private restClient.ExchangeApi

Support the project

Any support is greatly appreciated.

Donate

Make a one time donation in a crypto currency of your choice. If you prefer to donate a currency not listed here please contact me.

Btc: bc1q277a5n54s2l2mzlu778ef7lpkwhjhyvghuv8qf
Eth: 0xcb1b63aCF9fef2755eBf4a0506250074496Ad5b7
USDT (TRX) TKigKeJPXZYyMVDgMyXxMf17MWYia92Rjd

Sponsor

Alternatively, sponsor me on Github using Github Sponsors.

Release notes

  • Version 4.1.0 - 09 Jul 2026

    • Updated CryptoExchange.Net to v12.1.0
    • Added Pool, FeeAsset and Strategy properties to BitMEXExecution model
    • Added FeeAsset mapping on Shared implementations
    • Added mapping for OrderStatus enum
    • Added values to TransactionType enum
    • Added Fee property to BitMEXBalanceSummary model
    • Added SystemWithdrawableMargin property to BitMEXMarginStatus model, fixed AccountId, RiskLimit mapping
    • Added Strategy and Pool properties to BitMEXOrder model
    • Added FarLegSymbol, NearLegSymbol and ReferencePrice properties to BitMEXSymbol model
    • Fix for TransactionType mapping BitMEXTransaction model
  • Version 4.0.0 - 29 Jun 2026

    • Result types:
      • (Web)CallResult types are replaced by HttpResult, WebSocketResult and QueryResult with the same logic
      • WebSocketResult and QueryResult now return additional info for websocket operations
      • Updated result types to record type
      • Removed implicit result type conversion to bool, if (result) no longer works, instead use if (result.Success)
      • Fixed result object nullability hinting, for example Data might be null if Success isn't checked for true
    • Clients:
      • Added ToString overrides on base API types
      • Added Exchange property on BaseApiClient
      • Added ApiCredentials property on Api clients
      • Updated ILogger source from client name to topic specific client name
      • Removed logging from client creation
      • Fixed issue in SocketApiClient.GetSocketConnection causing requests to always wait the full max 10 seconds when there was a reconnecting socket
    • Shared APIs:
      • Added missing dedicated option types
      • Added Discover method on ISharedClient interface, returning info on supported capabilities and operations
      • Added ResetStaticExchangeParameters method on ExchangeParameters
      • Added Status property to SharedWithdrawal model
      • Added TradingModes property to SharedBalance model
      • Updated Shared ExchangeParameters parameter names to be case insensitive
      • Updated code comments
      • Replaced ExchangeResult with ExchangeCallResult type
      • Removed TradingMode from the response model, only maintained on models where it makes sense
    • Added async streaming on UserDataTracker items with StreamUpdatesAsync
    • Added cancellation token support to UserDataTracker starting
    • Added SupportedEnvironments property to PlatformInfo
    • Added Clear() method on UserClientProvider to clear all cached clients
    • Added setter to BitMEXExchange.RateLimiter to allow custom rate limit settings
    • Added LaunchTime to BitMEXSymbol model
    • Various small performance improvements
    • Fixed websocket connection attempts counting towards rate limit even when server could not be reached

About

A C# .netstandard client library for the BitMEX.com REST and Websocket API focusing on clear usage and models

Topics

Resources

License

Stars

4 stars

Watchers

2 watching

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages