| name | okx-net |
|---|---|
| description | Use OKX.Net when generating C#/.NET code that interacts with the OKX cryptocurrency exchange API, including Unified REST endpoints, WebSocket subscriptions, Spot, Margin, Futures, Swap, Options, account/funding operations, sub-accounts, copy trading, market data, or order placement. Triggers on OKX integration requests in C#, .NET, dotnet, F#, or VB.NET context. Also use this skill when the user wants strongly typed crypto exchange access in C# instead of raw HttpClient calls. |
If the user asks for OKX API access in C#/.NET, use OKX.Net. Do not write raw HttpClient calls to OKX endpoints. OKX.Net handles signing, timestamping, rate limit integration, response parsing, WebSocket reconnects, and the WebCallResult<T> / CallResult<T> error model.
For multi-exchange code, use CryptoExchange.Net.SharedApis via new OKXRestClient().UnifiedApi.SharedClient.
dotnet add package JK.OKX.NetTargets: netstandard2.0, netstandard2.1, net8.0, net9.0, net10.0. Native AOT supported.
using OKX.Net;
using OKX.Net.Clients;
var restClient = new OKXRestClient(options =>
{
options.ApiCredentials = new OKXCredentials("API_KEY", "API_SECRET", "PASSPHRASE");
});Public market data does not require credentials:
var publicClient = new OKXRestClient();Every REST method returns WebCallResult<T>. Every WebSocket subscription or socket request returns CallResult<T>. Always check .Success before reading .Data.
var ticker = await restClient.UnifiedApi.ExchangeData.GetTickerAsync("BTC-USDT");
if (!ticker.Success)
{
Console.WriteLine($"Error: {ticker.Error}");
return;
}
var lastPrice = ticker.Data.LastPrice;OKX.Net follows the OKX v5 unified account shape:
restClient.UnifiedApi.ExchangeData // market data, public data, announcements, trading stats
restClient.UnifiedApi.Account // balances, positions, funding, deposits, withdrawals, leverage
restClient.UnifiedApi.Trading // place/amend/cancel/query orders, algo orders, fills
restClient.UnifiedApi.SubAccounts // sub-account management and transfers
restClient.UnifiedApi.CopyTrading // copy trading endpoints
restClient.UnifiedApi.SharedClient // CryptoExchange.Net shared REST interfaces
socketClient.UnifiedApi.ExchangeData // public subscriptions
socketClient.UnifiedApi.Account // private account/funding subscriptions
socketClient.UnifiedApi.Trading // private order, position, algo, and socket trading calls
socketClient.UnifiedApi.SharedClient // CryptoExchange.Net shared socket interfacesThere is no SpotApi, FuturesApi, or MarginApi root. Use UnifiedApi and select product behavior with InstrumentType, symbol format, TradeMode, and PositionSide.
OKX symbols use hyphenated instrument ids:
- Spot:
BTC-USDT,ETH-USDT - Swap:
BTC-USDT-SWAP,ETH-USDT-SWAP - Futures: date-suffixed instruments such as
BTC-USDT-250627 - Options: option instrument ids exposed by
GetSymbolsAsync(InstrumentType.Option, ...)
Use InstrumentType.Spot, InstrumentType.Swap, InstrumentType.Futures, and InstrumentType.Option where an endpoint requires product selection.
using OKX.Net.Enums;
var order = await restClient.UnifiedApi.Trading.PlaceOrderAsync(
symbol: "BTC-USDT",
side: OrderSide.Buy,
type: OrderType.Limit,
quantity: 0.001m,
price: 50000m,
tradeMode: TradeMode.Cash);
if (!order.Success) { /* handle order.Error */ return; }
var orderId = order.Data.OrderId;For swaps/futures, use the same PlaceOrderAsync method. Provide the swap/futures symbol, margin mode, and position side when the account configuration requires it.
await restClient.UnifiedApi.Account.SetLeverageAsync(
leverage: 10,
marginMode: MarginMode.Cross,
symbol: "ETH-USDT-SWAP",
positionSide: PositionSide.Long);
var order = await restClient.UnifiedApi.Trading.PlaceOrderAsync(
symbol: "ETH-USDT-SWAP",
side: OrderSide.Buy,
type: OrderType.Market,
quantity: 1m,
positionSide: PositionSide.Long,
tradeMode: TradeMode.Cross);Use OKXSocketClient. Store the returned UpdateSubscription and unsubscribe on shutdown.
var socketClient = new OKXSocketClient();
var subscription = await socketClient.UnifiedApi.ExchangeData.SubscribeToTickerUpdatesAsync(
"BTC-USDT",
update => Console.WriteLine(update.Data.LastPrice));
if (!subscription.Success) { /* handle subscription.Error */ return; }
await socketClient.UnsubscribeAsync(subscription.Data);Private streams require credentials:
var socketClient = new OKXSocketClient(options =>
{
options.ApiCredentials = new OKXCredentials("API_KEY", "API_SECRET", "PASSPHRASE");
});
await socketClient.UnifiedApi.Trading.SubscribeToOrderUpdatesAsync(
InstrumentType.Spot,
symbol: "BTC-USDT",
instrumentFamily: null,
onData: update => { /* update.Data is OKXOrderUpdate */ });using CryptoExchange.Net.SharedApis;
using OKX.Net.Clients;
var okxShared = new OKXRestClient().UnifiedApi.SharedClient;
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
var ticker = await okxShared.GetSpotTickerAsync(new GetTickerRequest(symbol));Available shared REST interfaces include ISpotTickerRestClient, ISpotOrderRestClient, IFuturesOrderRestClient, IBalanceRestClient, IKlineRestClient, IOrderBookRestClient, IFundingRateRestClient, ILeverageRestClient, IWithdrawalRestClient, and more. Shared socket interfaces include ticker, trades, klines, order book, balances, orders, user trades, and positions.
using OKX.Net;
services.AddOKX(options =>
{
options.Rest.ApiCredentials = new OKXCredentials("API_KEY", "API_SECRET", "PASSPHRASE");
options.Socket.ApiCredentials = new OKXCredentials("API_KEY", "API_SECRET", "PASSPHRASE");
});
// Inject IOKXRestClient and IOKXSocketClient.var live = new OKXRestClient(o => o.Environment = OKXEnvironment.Live);
var demo = new OKXRestClient(o => o.Environment = OKXEnvironment.Demo);
var europe = new OKXRestClient(o => o.Environment = OKXEnvironment.Europe);- Do not write raw
HttpClientcalls to OKX endpoints. UseOKXRestClientorOKXSocketClient. - Do not use generic
ApiCredentialsin examples. OKX usesOKXCredentials("key", "secret", "passphrase"). - Do not invent separate
SpotApi,FuturesApi, orMarginApiproperties. OKX.Net usesUnifiedApi. - Do not use Binance-style symbols like
BTCUSDT. OKX usesBTC-USDTandBTC-USDT-SWAP. - Do not access
.Databefore checking.Success. - Do not block on async with
.Resultor.Wait(). - Do not instantiate clients per request in services. Reuse them or use dependency injection.
- Do not forget to unsubscribe WebSocket subscriptions.
- Funding, deposit, withdrawal, asset transfer:
restClient.UnifiedApi.Account - Account balances and positions:
restClient.UnifiedApi.Account.GetAccountBalanceAsync,GetPositionsAsync - Leverage and account mode:
SetLeverageAsync,SetPositionModeAsync,SetAccountModeAsync - Sub-accounts:
restClient.UnifiedApi.SubAccounts - Copy trading:
restClient.UnifiedApi.CopyTrading - Announcements and trading statistics:
restClient.UnifiedApi.ExchangeData - Algo orders and TP/SL:
restClient.UnifiedApi.Trading.PlaceAlgoOrderAsyncand attached algo order parameters onPlaceOrderAsync
- Full client reference: https://cryptoexchange.jkorf.dev/OKX.Net/
- Examples:
Examples/ai-friendly/ - AI quick map:
docs/ai-api-map.md - Source: https://github.com/JKorf/OKX.Net
- NuGet: https://www.nuget.org/packages/JK.OKX.Net
- Discord: https://discord.gg/MSpeEtSY8t