OKX.Net
OKX.Net is strongly typed client library for accessing the OKX 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
- Automatic websocket (re)connection management
- Client side rate limiting
- Cient side order book implementation
- Extensive logging
- Support for different environments
- Easy integration with other exchange client based on the CryptoExchange.Net base library
Supported Frameworks
The library is targeting both .NET Standard 2.0
and .NET Standard 2.1
for optimal compatibility
.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 |
Discord
A Discord server is available here. Feel free to join for discussion and/or questions around the CryptoExchange.Net and implementation libraries.
Support the project
DonateMake 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
Alternatively, sponsor me on Github using Github Sponsors.
Getting Started
The package is available on Nuget. After installing the package the OKX API is available by using the OKXRestClient
and OKXSocketClient
.
Installation
Nuget
dotnet add package JK.OKX.Net
GitHub packages
OKX.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
The NuGet package files are added along side the source with the latest GitHub release which can found here.
API Access
OKX.Net can be configured using Dotnet dependency injection, after which the clients can be injected into your services. It also correctly configures logging and HttpClient usage.
builder.Services.AddOKX(options => {
// Options can be configured here, for example:
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});
The IOKXRestClient
and IOKXSocketClient
can then be injected.
Alternatively the rest and socket client can be constructed directly
var restClient = new OKXRestClient(options => {
// Options can be configured here, for example:
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});
var socketClient = new OKXSocketClient();
Examples
Spot API
Get SymbolsGet a list of supported spot symbols
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetSymbolsAsync(OKX.Net.Enums.OKXInstrumentType.Spot);
Get Symbol TickerGet the 24h price ticker of a spot symbol
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetTickerAsync("ETH-USDT");
Get Recent TradesGet the most recent trades for a spot symbol
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetRecentTradesAsync("ETH-USDT");
Get BalancesGet asset balances of the spot account
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Account.GetAccountBalanceAsync();
Place Limit OrderPlace a new limit order for buying 0.1 ETH at a price of 2000 USDT
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Trading.PlaceOrderAsync(
"ETH-USDT",
OKX.Net.Enums.OKXOrderSide.Buy,
OKX.Net.Enums.OKXOrderType.LimitOrder,
0.1m,
2000,
tradeMode: OKX.Net.Enums.OKXTradeMode.Cash);
Place Market OrderPlace a new market order for buying 50 USDT worth of ETH at the best available price
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Trading.PlaceOrderAsync(
"ETH-USDT",
OKX.Net.Enums.OKXOrderSide.Buy,
OKX.Net.Enums.OKXOrderType.MarketOrder,
quantity: 50,
quantityAsset: OKX.Net.Enums.OKXQuantityAsset.QuoteAsset,
tradeMode: OKX.Net.Enums.OKXTradeMode.Cash);
Get Order InfoRetrieve order information for a specific order
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Trading.GetOrderDetailsAsync("ETH-USDT", 123);
Cancel an orderCancel a currently active order
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Trading.CancelOrderAsync("ETH-USDT", 123);
Futures API
Get SymbolsGet a list of supported usd futures symbols
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetSymbolsAsync(OKX.Net.Enums.OKXInstrumentType.Futures);
Get Symbol TickerGet the 24h price ticker of a spot symbol
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetTickerAsync("ETH-USDC-240628");
Get Recent TradesGet the most recent trades for a spot symbol
var okxClient = new OKXRestClient();
var result = await okxClient.UnifiedApi.ExchangeData.GetRecentTradesAsync("ETH-USDC-240628");
Get BalancesGet balance of the perpetual futures account
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Account.GetAccountBalanceAsync();
Get PositionsGet open positions
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Account.GetAccountPositionsAsync();
Place Limit OrderPlace a new limit order for going long on 0.1 ETH at a price of 2000 USDT
var okxClient = new OKXRestClient(options => {
options.ApiCredentials = new OKXApiCredentials("KEY", "SECRET", "PASS");
});
var result = await okxClient.UnifiedApi.Trading.PlaceOrderAsync(
"ETH-USDC-240628",
OKX.Net.Enums.OKXOrderSide.Buy,
OKX.Net.Enums.OKXOrderType.LimitOrder,
0.1m,
price: 2000,
positionSide: OKX.Net.Enums.OKXPositionSide.Long);