Kucoin.Net
Kucoin.Net is a strongly typed client library for accessing the Kucoin 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 (production, testnet)
- 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 Kucoin API is available by using the KucoinRestClient
and KucoinSocketClient
.
Installation
Nuget
dotnet add package Kucoin.Net
GitHub packages
Kucoin.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
Kucoin.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.AddKucoin(options => {
// Options can be configured here, for example:
options.ApiCredentials = new KucoinApiCredentials("APIKEY", "APISECRET", "PASS");
});
The IKucoinRestClient
and IKucoinSocketClient
can then be injected.
Alternatively the rest and socket client can be constructed directly
var restClient = new KucoinRestClient(options => {
// Options can be configured here, for example:
options.ApiCredentials = new KucoinApiCredentials("APIKEY", "APISECRET", "PASS");
});
var socketClient = new KucoinSocketClient();
Rate limiting
By default a rate limiter is enabled which makes sure the server rate limits aren't exceeded. For more info on what the rate limits for the Kucoin API are see the Spot API docs. Note that rate limiting is only implemented to prevent going over the request rate limit, it does not apply for order limits.
Whether client ratelimiting is enabled in the library and what the behaviour should be when the limit is reached can be controlled with the RatelimiterEnabled
and RateLimitingBehaviour
client options
services.AddKucoin(x =>
x.RatelimiterEnabled = true;
x.RateLimitingBehaviour = RateLimitingBehaviour.Wait;
}, x =>
{
x.RatelimiterEnabled = true;
x.RateLimitingBehaviour = RateLimitingBehaviour.Wait;
});
To be notified of when a rate limit is hit the static KucoinExchange.RateLimiter
exposes an event which triggers when a rate limit is reached
KucoinExchange.RateLimiter.RateLimitTriggered += (rateLimitEvent) => Console.WriteLine("Limit triggered: " + rateLimitEvent);
// Output: Limit triggered: RateLimitEvent { ApiLimit = Public Rest, LimitDescription = Limit of 2000 per 00:00:30, RequestDefinition = GET api/v1/market/stats, Host = https://api.kucoin.com/, Current = 1995, RequestWeight = 15, Limit = 2000, TimePeriod = 00:00:30, DelayTime = 00:00:19.8111238, Behaviour = Wait }
Kucoin applies different rate limits based on the account VIP level. By default the rate limit is set to the most conservative VIP0
tier. To change the rate limit tier call the Configure
method
KucoinExchange.RateLimiter.Configure(Kucoin.Net.Enums.VipLevel.Vip5);
Examples
Spot API
Get SymbolsGet a list of supported spot symbols
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.SpotApi.ExchangeData.GetSymbolsAsync();
Get Symbol TickerGet the 24h price ticker of a spot symbol
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.SpotApi.ExchangeData.GetTickerAsync("ETH-USDT");
Get Recent TradesGet the most recent trades for a spot symbol
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.SpotApi.ExchangeData.GetTradeHistoryAsync("ETH-USDT");
Get BalancesGet asset balances of the spot account
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.SpotApi.Account.GetAccountsAsync();
Place Limit OrderPlace a new limit order for buying 0.1 ETH at a price of 2000 USDT
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.SpotApi.Trading.PlaceOrderAsync(
"ETH-USDT",
Kucoin.Net.Enums.OrderSide.Buy,
Kucoin.Net.Enums.NewOrderType.Limit,
0.1m,
2000);
Place Market OrderPlace a new market order for buying 50 USDT worth of ETH at the best available price
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.SpotApi.Trading.PlaceOrderAsync(
"ETH-USDT",
Kucoin.Net.Enums.OrderSide.Buy,
Kucoin.Net.Enums.NewOrderType.Market,
quoteQuantity: 50);
Get Order InfoRetrieve order information for a specific order
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.SpotApi.Trading.GetOrderAsync("123");
Cancel an orderCancel a currently active order
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.SpotApi.Trading.CancelOrderAsync("123");
Futures API
Get ContractsGet a list of open futures contracts
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.FuturesApi.ExchangeData.GetOpenContractsAsync();
Get Symbol TickerGet the 24h price ticker of a spot symbol
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.FuturesApi.ExchangeData.GetTickerAsync("ETHUSDM");
Get Recent TradesGet the most recent trades for a spot symbol
var kucoinClient = new KucoinRestClient();
var result = await kucoinClient.FuturesApi.ExchangeData.GetTradeHistoryAsync("ETHUSDM");
Get BalancesGet balance of the perpetual futures account
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.FuturesApi.Account.GetAccountOverviewAsync();
Get PositionsGet open positions
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.FuturesApi.Account.GetPositionsAsync();
Place Limit OrderPlace a new limit order for buying 1 contract with 10x leverage at a price of 2000 USDT
var kucoinClient = new KucoinRestClient(options => {
options.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
});
var result = await kucoinClient.FuturesApi.Trading.PlaceOrderAsync(
"ETHUSDM",
Kucoin.Net.Enums.OrderSide.Buy,
Kucoin.Net.Enums.NewOrderType.Limit,
10,
1,
2000);