This document provides comprehensive examples for using the KuCoin SPOT API with Node.js and JavaScript. It covers various functionalities including account management, fund transfers, trade execution, order management, and market data retrieval. The examples are designed to help developers quickly integrate KuCoin Spot API into their NodeJS, Javascript and Typscript applications.
If you are here, it means you will be great addition to our Node.js Traders community on Telegram where we discuss trading ideas, provide support regarding SDKs and share valuable resources!
-
KuCoin Documentation - official Kucoin API docs
-
Node.js & JavaScript SDK for Kucoin - Github repo of our SDK
Current file contains only certain most used examples. If you can't find what you need, you can search through SpotClient.ts - all of the endpoints and functions will be there! Otherwise, just ask in Node.js Traders Telegram group.
Do you need help with Futures? Check out Futures Quickstart guide
Table of contents:
// Install by npm
npm install kucoin-api
// Install by yarn
yarn add kucoin-api
// require
const { SpotClient } = require('kucoin-api');
// import
import { SpotClient } from 'kucoin-api';
// initialise Spot Client
const spotClient = new SpotClient({
// insert your api key, secret and passphrase - use env vars, if not just fill the string values
apiKey: process.env.KUCOIN_API_KEY || 'insert-your-api-key',
apiSecret: process.env.KUCOIN_API_SECRET || 'insert-your-api-secret',
apiPassphrase:
process.env.KUCOIN_API_PASSPHRASE || 'insert-your-api-passphrase',
});
// Get Account Summary
spotClient.getAccountSummary();
// Get all Account Balances
spotClient.getBalances();
// Get specific Account or Currency Balance
spotClient.getBalance({
currency: 'USDT',
type: 'main', // 'trade' | 'margin' | 'trade_hf'
});
// Example call to get account details by ID
spotClient.getAccountDetails({ accountId: '5bd6e9286d99522a52e458de' });
// Margin endpoints for balances
spotClient.getMarginBalances();
spotClient.getMarginBalance();
spotClient.getIsolatedMarginBalance();
// Example call to get account ledgers with specified parameters
spotClient.getTransactions({ currency: 'BTC', startAt: 1601395200000 });
// Example call to get high-frequency account ledgers with specified parameters
spotClient.getHFTransactions({
bizType: 'TRADE_EXCHANGE',
currency: 'YOP,DAI',
startAt: 1601395200000,
});
// Example call to get high-frequency margin account ledgers with specified parameters
spotClient.getHFMarginTransactions({
bizType: 'MARGIN_EXCHANGE',
currency: 'YOP,DAI',
startAt: 1601395200000,
});
// Example call to create a deposit address
spotClient.createDepositAddress({
currency: 'BTC',
// Optional parameter
chain: 'BTC',
});
// Example call to get deposit addresses with specified parameters
spotClient.getDepositAddressesV2({
currency: 'BTC',
});
// Example call to get deposits
spotClient.getDeposits();
// Example call to get withdrawals with specified parameters
spotClient.getWithdrawals({
currency: 'BTC', // Optional parameter
});
// Example call to submit a withdrawal
spotClient.submitWithdraw({
currency: 'BTC',
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
amount: 0.01,
// Optional parameters
memo: 'exampleMemo',
chain: 'BTC',
});
// Example call to cancel a withdrawal by ID
spotClient.cancelWithdrawal({
withdrawalId: '5bffb63303aa675e8bbe18f9',
});
// Get all subaccounts
spotClient.getSubAccountsV2({});
// Example call to create a sub-account
spotClient.createSubAccount({
// Fill in the required parameters for creating a sub-account
subName: 'exampleSubAccount',
password: 'examplePassword',
access: 'trade',
});
// Example call to get sub-account balance with specified parameters
spotClient.getSubAccountBalance({
subUserId: '5caefba7d9575a0688f83c45',
includeBaseAmount: false,
});
// Example call to get sub-account balances
spotClient.getSubAccountBalancesV2();
// Example call to get transferable funds
spotClient.getTransferable({
currency: 'BTC',
type: 'MAIN',
});
// Example call to submit a transfer from master to sub-account
spotClient.submitTransferMasterSub({
clientOid: client.generateNewOrderID(), // or use your custom UUID
amount: 0.01,
currency: 'USDT',
direction: 'OUT', // 'IN' for transfer to master, 'OUT' for transfer to sub
subUserId: 'enter_sub_user_id_here',
});
// Example call to submit an inner transfer within same account
spotClient.submitInnerTransfer({
clientOid: client.generateNewOrderID(), // or use your custom UUID
amount: 0.01,
currency: 'USDT',
from: 'main', // Source account type
to: 'trade', // Destination account type
});
// Get all subaccount APIs
spotClient.getSubAPIs({
subName: 'my_sub_name',
});
// Create APIs for Sub-Account
spotClient.createSubAPI({
subName: 'my_sub_name',
passphrase: 'my_passphrase',
remark: 'my_remark',
});
// Modify Sub-Account APIs
spotClient.updateSubAPI({
subName: 'my_sub_name',
passphrase: 'my_passphrase',
apiKey: 'my_api_key',
});
// Delete Sub-Account APIs
spotClient.deleteSubAPI({
subName: 'my_sub_name',
passphrase: 'my_passphrase',
apiKey: 'my_api_key',
});
// Get All Currencies List
spotClient.getCurrencies();
// Get info for a specific currency
spotClient.getCurrency({
currency: 'BTC',
});
// Get all Symbols
spotClient.getSymbols();
// Example call to get ticker information for a specific symbol
spotClient.getTicker({
symbol: 'BTC-USDT',
});
// All tickers
spotClient.getTickers();
// Get 24h stats for a specific symbol
spotClient.get24hrStats({
symbol: 'BTC-USDT',
});
// get partial orderbook
spotClient.getOrderBookLevel20({ symbol: 'BTC-USDT' });
// get partial orderbook
spotClient.getOrderBookLevel100({ symbol: 'BTC-USDT' });
// get full orderbook
spotClient.getFullOrderBook({ symbol: 'BTC-USDT' });
// Example call to get Klines (candlestick data) with specified parameters
spotClient.getKlines({
type: '1min',
symbol: 'BTC-USDT',
startAt: 1566703297,
endAt: 1566789757,
});
Please, read official Kucoin API docs to understand how to place orders, cancel orders, etc. and what is needed for each endpoint. These are just low-end examples to understand how to use it with SDK.
// Market short order
const marketShort = spotClient.submitOrder({
clientOid: client.generateNewOrderID(), // or use your custom UUID
side: 'sell',
symbol: 'ETH-BTC',
type: 'market',
size: '0.5', // Specify the quantity to sell
});
// Market long order
const marketLong = spotClient.submitOrder({
clientOid: client.generateNewOrderID(), // or use your custom UUID
side: 'buy',
symbol: 'ETH-BTC',
type: 'market',
size: '0.5', // Specify the quantity to buy
});
// Limit short order
const limitShort = spotClient.submitOrder({
clientOid: client.generateNewOrderID(), // or use your custom UUID
side: 'sell',
symbol: 'ETH-BTC',
type: 'limit',
price: '0.03', // Specify the price to sell
size: '0.5', // Specify the quantity to sell
timeInForce: 'GTC', // Good Till Canceled
});
// Limit long order
const limitLong = spotClient.submitOrder({
clientOid: client.generateNewOrderID(), // or use your custom UUID
side: 'buy',
symbol: 'ETH-BTC',
type: 'limit',
price: '0.03', // Specify the price to buy
size: '0.5', // Specify the quantity to buy
timeInForce: 'GTC', // Good Till Canceled
});
//request
const multipleOrders = [
{
clientOid: '3d07008668054da6b3cb12e432c2b13a',
side: 'buy',
type: 'limit',
price: '0.01',
size: '0.01',
},
{
clientOid: '37245dbe6e134b5c97732bfb36cd4a9d',
side: 'buy',
type: 'limit',
price: '0.01',
size: '0.01',
},
];
spotClient.submitMultipleOrders({
symbol: 'KCS-USDT',
orderList: multipleOrders,
});
// Get open orders
spotClient.getOrders({ status: 'active' });
// Get closed orders
spotClient.getOrders({ status: 'done' });
// Get List of Orders Completed in 24h
spotClient.getRecentOrders();
// Get Details of a Single Order by ClientOrderId
spotClient.getOrderByClientOid({ clientOid: 'clientOid' });
// Or By OrderId
spotClient.getOrderByOrderId({ orderId: 'orderId' });
spotClient.cancelOrderById({ orderId: 'orderId' });
spotClient.cancelOrderByClientOid({ clientOid: 'clientOid' });
//cancel all orders for symbol
spotClient.cancelAllOrders({ symbol: 'XBTUSDTM' });
// cancel all orders for all symbols
spotClient.cancelAllOrders();
// Get Specific Fills
spotClient.getFills({ type: 'market' });
// or search for all
spotClient.getFills();
// Recent Fills from last 24 hours
spotClient.getRecentFills();
All of the examples are 99% same as regular spot, but you can follow the official HF Trade API documentation and list of functions in SpotClient.ts to find what you need!
All of the examples are 99% same as regular spot, but you can follow the official Margin Trade API documentation and list of functions in SpotClient.ts to find what you need!
For Websocket examples, please refer to these links:
If you need help, something is wrong/missing or you have suggestions, please join our Node.js Traders community group on Telegram and let us know!