A TypeScript client for the Kucoin cryptocurrency exchange API, optimized for the Gainium application. This client provides access to essential spot and futures trading endpoints, account management, market data, and WebSocket feeds.
Author: Maksym Shamko (https://github.com/maksymshamko)
Organization: Gainium (https://github.com/Gainium | https://gainium.io/)
- Complete TypeScript typing for all API responses
- Support for both Spot and Futures markets
- Real-time data via WebSockets
- Comprehensive error handling
- Automatic retry mechanism for failed requests
- Support for key Kucoin API endpoints used by Gainium
# Install from npm once published
npm install @gainium/kucoin-api
# or
yarn add @gainium/kucoin-apiimport KucoinApi from '@gainium/kucoin-api';
// Initialize client with API keys
const kucoin = new KucoinApi({
key: 'YOUR_API_KEY',
secret: 'YOUR_API_SECRET',
passphrase: 'YOUR_API_PASSPHRASE'
});
// For public endpoints, you can initialize without keys
const publicKucoin = new KucoinApi();// Get account balances
const balances = await kucoin.getAccounts();
console.log(balances);
// Get futures account details
const futuresAccount = await kucoin.getFuturesAccounts();
console.log(futuresAccount);// Place a limit order
const order = await kucoin.placeOrder({
clientOid: 'unique-order-id',
side: 'buy',
symbol: 'BTC-USDT',
type: 'limit',
price: '30000',
size: '0.001'
});
// Place a market order
const marketOrder = await kucoin.placeOrder({
clientOid: 'unique-market-order-id',
side: 'buy',
symbol: 'BTC-USDT',
type: 'market',
funds: '30' // Specify funds for quote currency amount
});
// Cancel an order
const cancelResult = await kucoin.cancelOrder({ id: 'order-id' });// Get ticker for a symbol
const ticker = await kucoin.getTicker('BTC-USDT');
// Get all tickers
const allTickers = await kucoin.getAllTickers();
// Get klines/candlestick data
const klines = await kucoin.getKlines({
symbol: 'BTC-USDT',
startAt: 1609459200,
endAt: 1609545600,
type: '1hour'
});// Subscribe to ticker updates
const unsubscribe = await kucoin.ws().ticker(['BTC-USDT'], (ticker) => {
console.log('Ticker update:', ticker);
});
// Subscribe to order updates
const orderUnsubscribe = await kucoin.ws().order((orderUpdate) => {
console.log('Order update:', orderUpdate);
});
// Subscribe to account balance updates
const balanceUnsubscribe = await kucoin.ws().balance((balanceUpdate) => {
console.log('Balance update:', balanceUpdate);
});
// Unsubscribe when no longer needed
unsubscribe();
orderUnsubscribe();
balanceUnsubscribe();getAccounts()- Get account balancesgetFuturesAccounts()- Get futures account detailsgetApiKey()- Get API key informationgetAffiliateUserRebateInformation()- Get affiliate rebate information
placeOrder()- Place a spot orderplaceFuturesOrder()- Place a futures ordercancelOrder()- Cancel a spot ordercancelOrderByClientId()- Cancel a spot order by client IDcancelFuturesOrderByOrderId()- Cancel a futures order by order IDcancelFuturesOrderByClientId()- Cancel a futures order by client IDgetOrders()- Get list of spot ordersgetFuturesOrders()- Get list of futures ordersgetOrderById()- Get a spot order by IDgetFuturesOrderById()- Get a futures order by IDgetOrderByClientId()- Get a spot order by client IDgetFuturesOrderByClientId()- Get a futures order by client IDlistFills()- Get list of order fills
getSymbols()- Get list of available spot symbolsgetFuturesSymbols()- Get list of available futures symbolsgetTicker()- Get ticker for a spot symbolgetFuturesTicker()- Get ticker for a futures symbolgetAllTickers()- Get all spot tickersgetFees()- Get trading fees for symbolsgetBaseFees()- Get base fee ratesgetKlines()- Get spot candlestick datagetFuturesKlines()- Get futures candlestick datagetFuturesPositions()- Get all futures positionsgetFuturesPositionBySymbol()- Get position for a specific futures symbol
ws().ticker()- Subscribe to spot ticker updatesws().tickerAll()- Subscribe to all spot tickersws().futuresTicker()- Subscribe to futures ticker updatesws().futuresPositions()- Subscribe to futures position updatesws().order()- Subscribe to spot order updatesws().futuresOrder()- Subscribe to futures order updatesws().balance()- Subscribe to spot balance updatesws().futuresBalance()- Subscribe to futures balance updatesws().klines()- Subscribe to candlestick updates
For detailed parameter descriptions and return types, refer to the JSDoc comments in the source code.
MIT
Contributions are welcome! Please feel free to submit a Pull Request.