A Python SDK for KuCoin cryptocurrency exchange API providing REST endpoints implementation, simple authentication handling, response exception handling, and websocket support for trading, market data, user account management, margin trading, lending, and earning features
—
Comprehensive user account operations including balance management, transfers, deposits, withdrawals, sub-account management, and fee information. Provides complete access to account ledgers and transaction history.
Access account balances and details.
def get_account_list(currency: str = None, account_type: str = None):
"""
Get list of accounts.
Args:
currency (str, optional): Filter by currency
account_type (str, optional): Filter by account type ('main', 'trade', 'margin')
Returns:
list: Account information with balances
"""
def get_account(accountId: str):
"""
Get details for a specific account.
Args:
accountId (str): Account ID
Returns:
dict: Account details with balance information
"""
def create_account(account_type: str, currency: str):
"""
Create a new account.
Args:
account_type (str): Account type ('main', 'trade', 'margin')
currency (str): Currency code
Returns:
dict: Created account information
"""
def get_transferable(currency: str, account_type: str, **kwargs):
"""
Get transferable balance for an account.
Args:
currency (str): Currency code
account_type (str): Account type
Returns:
dict: Transferable balance information
"""Transaction history and ledger access.
def get_account_ledger(**kwargs):
"""
Get account ledger (transaction history).
Args:
currency (str, optional): Filter by currency
direction (str, optional): Filter by direction ('in', 'out')
bizType (str, optional): Filter by business type
startAt (int, optional): Start time
endAt (int, optional): End time
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Paginated ledger entries
"""
def get_account_hold(accountId: str, **kwargs):
"""
Get account holds (frozen funds).
Args:
accountId (str): Account ID
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Account hold information
"""Transfer funds between accounts and users.
def inner_transfer(currency: str, from_payer: str, to_payee: str, amount: str, clientOid: str = '', from_tag: str = None, to_tag: str = None):
"""
Transfer funds between accounts.
Args:
currency (str): Currency to transfer
from_payer (str): Source account type
to_payee (str): Destination account type
amount (str): Transfer amount
clientOid (str, optional): Client operation ID
from_tag (str, optional): Source tag for isolated margin
to_tag (str, optional): Destination tag for isolated margin
Returns:
dict: Transfer order ID
"""
def transfer_master_sub(currency: str, amount: str, direction: str, subUserId: str, clientOid: str = '', accountType: str = None, subAccountType: str = None):
"""
Transfer between master and sub-accounts.
Args:
currency (str): Currency to transfer
amount (str): Transfer amount
direction (str): Transfer direction ('OUT' master->sub, 'IN' sub->master)
subUserId (str): Sub-account user ID
clientOid (str, optional): Client operation ID
accountType (str, optional): Master account type
subAccountType (str, optional): Sub-account type
Returns:
dict: Transfer order ID
"""
def flex_transfer(clientOid: str, amount: str, fromAccountType: str, type: str, toAccountType: str, currency: str, fromUserId: str = None, fromAccountTag: str = None, toUserId: str = None, toAccountTag: str = None):
"""
Flexible transfer with advanced options.
Args:
clientOid (str): Client operation ID
amount (str): Transfer amount
fromAccountType (str): Source account type
type (str): Transfer type
toAccountType (str): Destination account type
currency (str): Currency to transfer
fromUserId (str, optional): Source user ID
fromAccountTag (str, optional): Source account tag
toUserId (str, optional): Destination user ID
toAccountTag (str, optional): Destination account tag
Returns:
dict: Transfer result
"""Deposit address management and history.
def create_deposit_address(currency: str, chain: str = None):
"""
Create a deposit address.
Args:
currency (str): Currency code
chain (str, optional): Blockchain network
Returns:
dict: Deposit address and memo/tag
"""
def get_deposit_address(currency: str, chain: str = None):
"""
Get deposit address.
Args:
currency (str): Currency code
chain (str, optional): Blockchain network
Returns:
dict: Deposit address information
"""
def get_deposit_addressv2(currency: str, chain: str = None):
"""
Get deposit addresses (v2 - supports multiple chains).
Args:
currency (str): Currency code
chain (str, optional): Specific blockchain network
Returns:
list: Deposit addresses for all or specified chains
"""
def get_deposit_list(**kwargs):
"""
Get deposit history.
Args:
currency (str, optional): Filter by currency
startAt (int, optional): Start time
endAt (int, optional): End time
status (str, optional): Deposit status
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Paginated deposit history
"""Withdrawal operations and history.
def apply_withdrawal(currency: str, address: str, amount: float, **kwargs):
"""
Submit a withdrawal request.
Args:
currency (str): Currency to withdraw
address (str): Withdrawal address
amount (float): Withdrawal amount
memo (str, optional): Address memo/tag
isInner (bool, optional): Internal transfer flag
remark (str, optional): Withdrawal remark
chain (str, optional): Blockchain network
Returns:
dict: Withdrawal ID
"""
def cancel_withdrawal(withdrawalId: str):
"""
Cancel a pending withdrawal.
Args:
withdrawalId (str): Withdrawal ID to cancel
Returns:
dict: Cancellation result
"""
def get_withdrawal_list(**kwargs):
"""
Get withdrawal history.
Args:
currency (str, optional): Filter by currency
status (str, optional): Withdrawal status
startAt (int, optional): Start time
endAt (int, optional): End time
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Paginated withdrawal history
"""
def get_withdrawal_quota(currency: str, chain: str = None):
"""
Get withdrawal quotas and limits.
Args:
currency (str): Currency code
chain (str, optional): Blockchain network
Returns:
dict: Withdrawal limits and available amounts
"""Complete sub-account operations.
def get_sub_user():
"""
Get list of sub-users.
Returns:
list: Sub-user information
"""
def create_sub_account(password: str, sub_name: str, access: str, **kwargs):
"""
Create a new sub-account.
Args:
password (str): Sub-account password (7-24 chars, letters+numbers)
sub_name (str): Sub-account name (7-32 chars, at least one number and letter)
access (str): Permission types ('Spot', 'Futures', 'Margin')
remarks (str, optional): Sub-account remarks
Returns:
dict: Created sub-account information
"""
def get_sub_account(subUserId: str):
"""
Get sub-account balance details.
Args:
subUserId (str): Sub-user ID
Returns:
dict: Sub-account balances across all account types
"""
def get_sub_accounts():
"""
Get aggregated balance of all sub-accounts.
Returns:
list: All sub-account balance information
"""API key management for sub-accounts.
def get_sub_account_api_list(sub_name: str, **kwargs):
"""
Get sub-account API key list.
Args:
sub_name (str): Sub-account name
apiKey (str, optional): Filter by API key
Returns:
dict: API key information
"""
def create_apis_for_sub_account(sub_name: str, passphrase: str, remark: str, **kwargs):
"""
Create API keys for sub-account.
Args:
sub_name (str): Sub-account name
passphrase (str): API key passphrase (7-32 chars)
remark (str): API key remark (1-24 chars)
permission (str, optional): Permission level
ipWhitelist (str, optional): IP whitelist
expire (int, optional): Expiration time
Returns:
dict: Created API key details
"""
def modify_sub_account_apis(sub_name: str, api_key: str, passphrase: str, **kwargs):
"""
Modify sub-account API key settings.
Args:
sub_name (str): Sub-account name
api_key (str): API key to modify
passphrase (str): API key passphrase
permission (str, optional): New permission level
ipWhitelist (str, optional): New IP whitelist
expire (int, optional): New expiration time
Returns:
dict: Modified API key information
"""
def delete_sub_account_apis(sub_name: str, api_key: str, passphrase: str):
"""
Delete sub-account API key.
Args:
sub_name (str): Sub-account name
api_key (str): API key to delete
passphrase (str): API key passphrase
Returns:
dict: Deletion confirmation
"""Trading fee rates and calculations.
def get_actual_fee(symbols: str):
"""
Get actual trading fee rates for symbols.
Args:
symbols (str): Comma-separated symbol list
Returns:
dict: Fee rates for each symbol
"""
def get_base_fee():
"""
Get base user trading fee rates.
Returns:
dict: Base maker and taker fee rates
"""Specialized high-frequency trading account operations.
def get_hf_account_ledgers(**kwargs):
"""
Get high-frequency account ledger.
Args:
currency (str, optional): Filter by currency
direction (str, optional): Filter by direction
bizType (str, optional): Filter by business type
lastId (str, optional): Last record ID for pagination
limit (int, optional): Number of records
startAt (int, optional): Start time
endAt (int, optional): End time
Returns:
list: HF account ledger entries
"""
def transfer_to_hf_account(client_oid: str, currency: str, from_payer: str, amount: str):
"""
Transfer funds to high-frequency trading account.
Args:
client_oid (str): Client operation ID
currency (str): Currency to transfer
from_payer (str): Source account type
amount (str): Transfer amount
Returns:
list: HF account information after transfer
"""from kucoin.client import User
# Initialize user client
user = User(api_key, api_secret, api_passphrase, is_sandbox=False)
# Get all account balances
accounts = user.get_account_list()
for account in accounts:
if float(account['balance']) > 0:
print(f"{account['currency']}: {account['balance']} ({account['type']})")
# Get specific currency balance
btc_accounts = user.get_account_list(currency='BTC')
print(f"BTC Accounts: {len(btc_accounts)}")# Transfer from main to trading account
transfer_result = user.inner_transfer(
currency='USDT',
from_payer='main',
to_payee='trade',
amount='100'
)
print(f"Transfer order: {transfer_result['orderId']}")
# Check account balances after transfer
trade_account = user.get_account_list(currency='USDT', account_type='trade')[0]
print(f"Trade account balance: {trade_account['balance']} USDT")# Create deposit address for Bitcoin
btc_deposit = user.create_deposit_address('BTC')
print(f"BTC Deposit Address: {btc_deposit['address']}")
# Get deposit history
deposits = user.get_deposit_list(currency='BTC', status='SUCCESS')
for deposit in deposits['items']:
print(f"Deposit: {deposit['amount']} BTC from {deposit['walletTxId']}")# Check withdrawal quotas
quota = user.get_withdrawal_quota('BTC')
print(f"Available for withdrawal: {quota['availableAmount']} BTC")
print(f"Minimum withdrawal: {quota['withdrawMinSize']} BTC")
# Submit withdrawal (be very careful with real addresses!)
withdrawal = user.apply_withdrawal(
currency='BTC',
address='1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', # Example address - DO NOT USE
amount=0.001,
remark='Test withdrawal'
)
print(f"Withdrawal submitted: {withdrawal['withdrawalId']}")# Create a new sub-account
sub_account = user.create_sub_account(
password='SecurePass123',
sub_name='trading-bot-01',
access='Spot',
remarks='Automated trading bot'
)
print(f"Created sub-account: {sub_account['subName']}")
# Get sub-account balances
sub_balance = user.get_sub_account(sub_account['uid'])
print(f"Sub-account main balance: {sub_balance['mainAccounts']}")
# Create API keys for sub-account
api_keys = user.create_apis_for_sub_account(
sub_name='trading-bot-01',
passphrase='APIPassphrase123',
remark='Bot API keys',
permission='General'
)
print(f"API Key: {api_keys['apiKey']}")AccountInfo = dict
# {
# "id": str, # Account ID
# "currency": str, # Currency code
# "type": str, # Account type
# "balance": str, # Total balance
# "available": str, # Available balance
# "holds": str # Held balance
# }
LedgerEntry = dict
# {
# "id": str, # Entry ID
# "currency": str, # Currency
# "amount": str, # Change amount
# "fee": str, # Fee amount
# "balance": str, # Balance after change
# "bizType": str, # Business type
# "direction": str, # Direction ('in' or 'out')
# "createdAt": int, # Timestamp
# "context": dict # Business context
# }
DepositInfo = dict
# {
# "address": str, # Deposit address
# "memo": str, # Address memo/tag
# "amount": float, # Deposit amount
# "fee": float, # Deposit fee
# "currency": str, # Currency
# "isInner": bool, # Internal deposit flag
# "walletTxId": str, # Wallet transaction ID
# "status": str, # Deposit status
# "remark": str, # Deposit remark
# "createdAt": int, # Creation time
# "updatedAt": int # Update time
# }
WithdrawalInfo = dict
# {
# "id": str, # Withdrawal ID
# "address": str, # Withdrawal address
# "memo": str, # Address memo
# "currency": str, # Currency
# "amount": float, # Withdrawal amount
# "fee": float, # Withdrawal fee
# "walletTxId": str, # Wallet transaction ID
# "isInner": bool, # Internal withdrawal flag
# "status": str, # Withdrawal status
# "remark": str, # Withdrawal remark
# "createdAt": int, # Creation time
# "updatedAt": int # Update time
# }
SubAccountInfo = dict
# {
# "userId": str, # Sub-user ID
# "subName": str, # Sub-account name
# "remarks": str, # Sub-account remarks
# "mainAccounts": list, # Main account balances
# "tradeAccounts": list, # Trade account balances
# "marginAccounts": list # Margin account balances
# }Install with Tessl CLI
npx tessl i tessl/pypi-kucoin-python