Navigating the world of trading APIs can be challenging, especially when dealing with technical configurations and error handling. This comprehensive guide addresses common questions and issues users encounter when working with trading APIs—particularly those related to account setup, order execution, and troubleshooting. Whether you're a beginner or an experienced developer, this resource will help clarify key concepts and improve your API integration experience.
Understanding Passphrase and APIKey Security
When creating an APIKey, you'll be prompted to set a passphrase. This is a user-defined password used to authenticate your API access. Unlike other credentials, the passphrase is not recoverable—if forgotten, the only option is to generate a new APIKey. Therefore, it's crucial to store your passphrase securely using a trusted password manager or encrypted storage solution.
👉 Discover how secure API access can enhance your trading strategy
Creating a Demo Account APIKey
To test your strategies without risking real funds, use a demo (paper) trading account. You can create a dedicated APIKey for demo trading by following these steps:
- Log in to your account
- Navigate to Trade > Demo Trading > Personal Center
- Select Create Demo APIKey
- Begin simulated trading immediately
This allows developers and traders to validate their bots and algorithms in a risk-free environment before going live.
Does an APIKey Expire?
APIKey expiration depends on usage and configuration:
- Unrestricted APIKeys (with trading or withdrawal permissions and no IP binding) are automatically deleted after 14 days of inactivity. Inactivity means no calls to private or account-authenticated endpoints.
- IP-bound keys or those with read-only permissions do not expire, regardless of usage frequency.
To maintain long-term access, ensure regular interaction with authenticated endpoints such as balance checks, order placement, or transaction history retrieval.
Contract Order Units: Can You Trade in USDT or Coins?
No. Contract trading via API operates exclusively in contract units ("lots" or "sheets"), not in USDT or individual coins. To convert between coin amounts and contract units, use the Unit Conversion API:
- Endpoint:
GET /api/v5/public/convert-contract-unit - Parameters: Include
instId,fromCcy,toCcy, andsz
This enables accurate position sizing based on your desired exposure.
How to Calculate Price Change Percentage
While there's no direct endpoint for 24-hour price change percentage, you can compute it using ticker data:
Price Change % = ((Last Price - Open24h) / Open24h) × 100Use the Get Ticker endpoint (GET /api/v5/market/ticker) to retrieve:
last: Latest traded priceopen24h: 24-hour opening price
This method ensures precise calculation aligned with platform metrics.
Frequently Asked Questions
Q: What does "posSide" mean in contract orders?
A: The posSide parameter defines the position direction—long or short. It’s required in single-position mode for contracts. For example:
- Opening a long:
side=buy,posSide=long - Closing a long:
side=sell,posSide=long
In net mode, only posSide=net is allowed.
Q: Why am I getting error 51000 "Parameter posSide error"?
A: This occurs when posSide is missing or invalid. Check your account’s position mode:
- Use
posSideonly in single-position mode for futures - In net mode or spot/leverage trading, omit
posSideor set it tonet
👉 Learn how advanced order types can optimize your trades
Q: How do I find contract value and minimum order size?
A: Use the Get Instruments endpoint (GET /api/v5/public/instruments). Key fields include:
ctVal: Contract face valueminSz: Minimum order quantitylotSz: Order size increment
This data is essential for compliance with lot size rules.
Instrument ID (instId) Format Guide
Each trading pair has a standardized instId format:
- Spot/Spot Leverage:
BTC-USDT Perpetual Contracts:
- Coin-margined:
BTC-USD-SWAP - USDT-margined:
BTC-USDT-SWAP(uppercase only)
- Coin-margined:
Futures Contracts:
- Expiry format:
BTC-USD-210326(YYMMDD)
- Expiry format:
Options:
- Call:
BTC-USD-210326-2000-C - Put:
BTC-USD-210326-2000-P
- Call:
Ensure correct formatting to avoid invalid instrument errors.
Setting Up Take-Profit and Stop-Loss Orders
You can configure take-profit (TP) and stop-loss (SL) in two ways:
With Regular Orders: Use the
attachAlgoOrdsarray in the place-order request."attachAlgoOrds": [{ "tpTriggerPx": "50000", "tpOrdPx": "-1" }]- As Standalone Algo Orders: Use the Place Algo Order endpoint with order type
trigger.
Both methods support conditional execution based on market price.
Common TP/SL Errors (51046–51049)
These codes indicate incorrect trigger pricing logic:
- Sell order: TP price > last price; SL price < last price
- Buy order: TP price < last price; SL price > last price
Violating these rules results in rejection. Always validate prices against current market data before submission.
Handling Timestamp Errors (50102)
Error 50102 indicates a timestamp mismatch between your system and the server. To resolve:
- Call the Get System Time endpoint (
GET /api/v5/public/time) - Sync your local clock to stay within ±30 seconds of server time
- Note: Request headers use UTC+0, while the time endpoint returns UTC+8
Maintaining accurate time prevents authentication failures due to expired requests.
Environment Mismatch Error (50101)
Error 50101 occurs when the APIKey environment doesn't match the request context:
- Live trading: Use live APIKey + header
x-simulated-trading: 0 - Demo trading: Use demo APIKey + header
x-simulated-trading: 1
Double-check both credentials and headers to ensure consistency.
Account Mode Restrictions (51010)
Certain trading types require specific account modes:
- To trade margin or contracts, you must not be in "simple mode"
Switch via:
- Web/App: Settings icon → Account Mode
- API:
POST /api/v5/account/set-position-mode
Initial changes must be made via UI; subsequent updates can use API.
Lot Size Validation Error (51121)
This error means your order quantity isn’t a multiple of the product’s lot size (lotSz). Always:
- Retrieve instrument details via
/api/v5/public/instruments - Ensure order size is divisible by
lotSz - Respect
minSzandmaxSzlimits
Automated systems should validate inputs before sending requests.
Withdrawal Whitelist Issues (58207)
Even if whitelist verification is disabled on the withdrawal page, API withdrawals still require pre-approved addresses. To avoid this error:
- Add the address manually through the UI
- Enable "Exempt from Verification"
- Confirm it appears in your approved list
APIs do not bypass security policies—even when UI settings suggest otherwise.
Request Timeout Errors (50004)
Error 50004 typically occurs during high-load periods like funding fee settlements at 08:00, 16:00, and 24:00 UTC. Recommendations:
- Avoid placing critical orders near these times
- Implement retry logic with exponential backoff
- Always verify final state via order or transaction query endpoints—timeout does not indicate failure
System stability improves significantly outside peak windows.
Final Tips
If you're still facing unexplained issues:
- Review full API documentation
- Validate signatures and headers
- Monitor rate limits
- Test in demo mode first
👉 Access powerful tools to streamline your API trading journey