API Architecture Overview: StarkEx & ZK-Rollup
Unlike classic DEXs, ApeX Pro uses a Layer-2 scaling solution built on StarkWare (StarkEx). Orders are matched in a high-performance off-chain engine, while cryptographic proofs (ZK-proofs) are published to Ethereum. This delivers CEX-grade latency with full non-custodial control.
→ More about the ApeX smart-contract architecture
API Security: Protecting the L1 and L2 Layers
To build trading bots on ApeX you will need standard API keys and an L2 Stark Key. Treat them as carefully as your seed phrase. A compromised L2 key lets an attacker trade on your behalf.
Key security practices:
- Two-factor model: Requests use a pair of API Key / Secret (for session authentication) + L2 Stark Private Key (for cryptographically signing the orders themselves).
- IP whitelist: Bind your API keys to a static VPS IP address.
- Secure storage: Keep `STARK_PRIVATE_KEY` and `API_SECRET` only in environment variables (`.env`) or AWS Secrets Manager.
API Use Cases: AI Trading and Algorithmic Strategies
The ApeX Protocol API is the foundation for USDC-based algorithmic derivatives trading. Quant funds and algo traders use it for:
- Funding Rate Arbitrage: Monitoring rates between ApeX and Bybit/Binance for delta-neutral profit extraction.
- High-frequency market making (HFT): Maintaining liquidity in the order book using WebSockets (the StarkEx engine processes thousands of transactions per second).
- Liquidation hunting: Analyzing
Mark PriceandOracle Priceto enter positions automatically during other market participants' liquidations.
Key Generation: L1 Wallet, L2 Stark Key and API Key
On ApeX, developer onboarding consists of 3 steps:
- Connecting an L1 Wallet: Connect your Ethereum wallet (MetaMask / WalletConnect) in the ApeX Pro interface.
- L2 Stark Key generation: ApeX will ask your wallet for a signature to derive a
Stark Key. This L2 key is used by the mathematical engine to validate your orders. - Creating an API Key: Go to your account Dashboard -> API Management. Click "Create API". The system will generate an API Key and a Passphrase/Secret (required for signing HTTP headers).
Important: When placing an order via the API (POST `/api/v1/order`), the order body is signed with your L2 Stark Key (Stark Signature), while the HTTP request itself is protected by headers based on your API Key.
API Architecture
The ApeX Protocol API offers two main interfaces:
- REST API (pull model): Placing and canceling orders, account balance queries and historical trade data.
- WebSocket API (push model): Instant updates of the order book (L2 Order Book), price ticks and your account state (Private Stream).
Environments reference
- Mainnet (ApeX Pro):
https://pro.apex.exchange| Public WS:wss://quote.pro.apex.exchange/realtime_public - Testnet:
https://testnet.apex.exchange— use it to debug bots without risking real USDC. - Header authentication: Requires `APEX-API-KEY`, `APEX-TIMESTAMP`, `APEX-SIGNATURE` and `APEX-PASSPHRASE`.
Error Handling (Error Codes)
When automating, it is important to handle server responses correctly. Here are the main errors you will encounter in the ApeX API:
| Code (HTTP) | Message (Cause) | Solution |
|---|---|---|
| 400 / 10000 | Invalid API-key, Signature, or Timestamp. | Make sure your system time is synchronized (deviation no more than 5 seconds). Verify that the HMAC signature for the HTTP headers is formed correctly. |
| 400 | Invalid Stark Signature. | The order was rejected by the StarkEx engine. Check that the order hash is built correctly and signed with your L2 Stark Private Key. |
| 400 | Insufficient Margin. | Not enough free margin (USDC) to open a position. Increase leverage or reduce the order size (qty). |
Guides and Tutorials
- REST API vs. WebSocket — Which architecture should you choose?
- L1 / L2 Authentication — A breakdown of StarkWare cryptography and HMAC.
First Steps: Connecting and Examples
Connectivity Check (Ping / Symbols)
To test connectivity, request the list of available trading pairs (this endpoint requires no authentication):
curl -X GET "https://pro.apex.exchange/api/v1/symbols"
Subscribing to Trades over WebSocket
To receive real-time trades for the BTC-USDC pair (the main margin pair on ApeX):
URL: `wss://quote.pro.apex.exchange/realtime_public`
{
"op": "subscribe",
"args": [
"trade.BTCUSDC"
]
}
Node.js: Basic REST Request Example
An example using axios to fetch the current order book (Orderbook L2) without authentication:
const axios = require('axios');
async function getOrderbook() {
try {
const response = await axios.get('https://pro.apex.exchange/api/v1/depth', {
params: {
symbol: 'BTCUSDC',
limit: 5
}
});
console.log("Топ 5 заявок Ask:", response.data.data.asks);
} catch (error) {
console.error("Ошибка API:", error.response ? error.response.data : error.message);
}
}
getOrderbook();
Key Endpoints Overview
Market Data (Public)
- GET /api/v1/symbols: Contract specifications (tick size, minimum lot, price step).
- GET /api/v1/depth: Order book depth.
- GET /api/v1/klines: Historical candlestick data (OHLCV).
- GET /api/v1/ticker: Current quotes, including the funding rate and 24h volumes.
Trading and Account (Authentication Required)
- GET /api/v1/account: Retrieve balance data (USDC), available margin and account state.
- GET /api/v1/position: The list of all your open positions, liquidation prices and unrealized PnL.
- POST /api/v1/order: Create a new order (LIMIT, MARKET, STOP_MARKET). Note: the request body must include a Stark Signature (order signature).
- DELETE /api/v1/order: Cancel a specific order by its ID.
Third-Party Integrations: CScalp and Hummingbot
Manual Trading and Scalping (CScalp)
The ApeX API is fully supported by the professional trading terminal CScalp. Add an "ApeX Pro" connection in the settings, enter your generated L1 API keys and L2 Passphrase, and the terminal will use WebSocket to stream the order book with millisecond latency and place orders in one click.
Market Making with Hummingbot
For quants and bot builders: Hummingbot ships a built-in connector for the ApeX platform. You can deploy a bot out of the box and configure Pure Market Making (PMM) or Cross-Exchange Arbitrage strategies (e.g., arbitrage between Binance and ApeX) without writing L2 order-signature logic yourself.
Frequently Asked Questions (FAQ) about the ApeX API
What is the base URL of the ApeX Protocol REST API?
The main URL for REST requests on Mainnet (ApeX Pro): https://pro.apex.exchange. For WebSocket (public data): wss://quote.pro.apex.exchange/realtime_public. For testing strategies there is a Testnet: https://testnet.apex.exchange.
Why isn't a regular API key enough to place orders?
Unlike CEXs, ApeX is built on ZK-Rollup architecture (StarkEx). Regular API keys only authenticate the HTTP request. The trading intent itself (size, price, margin) must be cryptographically signed with your L2 key (Stark Key) for the blockchain to consider the transaction valid.
How do I keep my API keys and L2 keys secure?
Always use an IP whitelist in the API management panel. Never store your keys (Stark Private Key, Passphrase) in public code repositories — use secure secret storage (.env or secret managers).