Demo trading
Available environments
OKX supports production and demo trading. Regional selection and simulation are separate settings: market chooses the account's regional service, while demoTrading chooses demo routing. Do not use market: 'demo'.
Demo trading
Create an API key, secret and passphrase from OKX Demo Trading. Set demoTrading: true independently on RestClient, WebsocketClient and WebsocketAPIClient as needed. Use the same account region on each client.
import { RestClient, WebsocketClient } from 'okx-api';
function requiredEnv(name: string): string {
const value = process.env[name];
if (!value) throw new Error(`Set ${name} in your server environment.`);
return value;
}
// Configure clients only. No requests, subscriptions or orders are sent.
const credentials = {
apiKey: requiredEnv('OKX_DEMO_API_KEY'),
apiSecret: requiredEnv('OKX_DEMO_API_SECRET'),
apiPass: requiredEnv('OKX_DEMO_API_PASSPHRASE'),
};
export const rest = new RestClient({ ...credentials, demoTrading: true });
export const ws = new WebsocketClient({
accounts: [credentials],
demoTrading: true,
});This example constructs global demo clients without making requests. For a regional account, set the appropriate market on both clients as described in Configuration. Keep demo credentials in server-side environment variables, separate from live credentials.
Coverage and limitations
The SDK supplies x-simulated-trading: 1 for REST and selects demo WebSocket routes. No manual simulation header or URL override is needed. The default remains live trading.
Demo trading does not support every API feature. OKX explicitly excludes functions such as deposits, withdrawals and purchase/redemption. Confirm availability for your product and account region in OKX's demo-trading documentation.
What these environments can prove
Use test environments to check request formats, credentials, order handling and reconnect recovery within the features they support. Testnet liquidity, prices, fills, fees and availability can differ substantially from production. Accounts, balances and order history may also be reset.
Testnet is not a reliable place to evaluate strategy performance. Demo trading may use production market data, but simulated fills do not establish live profitability, slippage, execution quality or capacity. Treat both as integration tests, not evidence that a strategy will succeed with real funds. See the CEX testnet overview for the distinction between testnet and demo trading.
Before switching environments
- Keep credentials and configuration separate for each account, product and environment. Never fall back to live credentials when a test key is missing.
- Configure every REST, subscription and WebSocket API client separately. Changing one client does not reconfigure another.
- Check the selected environment's supported endpoints and instruments before sending a request. Similar method signatures do not imply identical availability.
- Keep authenticated requests on your server. Confirm the destination and account before enabling any write operation.
Production remains the default. Selecting a test environment is an explicit choice for each client; it is not a global SDK setting.