API Overview
The GX Exchange API provides programmatic access to the GX Chain L1 decentralized exchange. It supports perpetual futures trading, spot markets, real-time market data streaming, and on-chain asset management through three complementary interfaces.
Architecture
| Interface | Protocol | Port | Description |
|---|---|---|---|
| REST API | HTTPS | 4001 | Market data queries and authenticated exchange actions |
| WebSocket | WSS | 4000 | Real-time streaming of orderbooks, trades, fills, and account events |
| EVM JSON-RPC | HTTPS | 3001 | Standard eth_* methods for GX EVM interaction |
Base URLs
| Environment | REST | WebSocket | EVM RPC |
|---|---|---|---|
| Mainnet | https://api.gx.exchange | wss://api.gx.exchange/ws | https://rpc.gx.exchange |
| Testnet | https://api.gx-exchange-testnet.xyz | wss://api.gx-exchange-testnet.xyz/ws | https://rpc.gx-exchange-testnet.xyz |
| Local | http://localhost:4001 | ws://localhost:4000 | http://localhost:3001 |
All connections require TLS 1.3 on mainnet and testnet.
Public Stats API
The Stats API provides protocol-level analytics data without authentication. Used by stats.gx.exchange, CoinGecko, DeFiLlama, and third-party dashboards.
Base URL: https://api.gx.exchange/stats
| Endpoint | Method | Description |
|---|---|---|
/stats/total_users | GET | Total unique accounts |
/stats/total_usd_volume | GET | 24-hour total trading volume (USD) |
/stats/total_deposits | GET | Total collateral deposited across all accounts |
/stats/total_withdrawals | GET | Total withdrawn |
/stats/cumulative_usd_volume | GET | Cumulative volume over time (chart data) |
/stats/daily_usd_volume | GET | Daily volume over time (chart data) |
/stats/open_interest | GET | Open interest per market with mark price |
/stats/funding_rate | GET | Current funding rate per market |
/stats/daily_unique_users | GET | Daily unique users (chart data) |
/stats/cumulative_users | GET | Cumulative user growth (chart data) |
/stats/asset_ctxs | GET | All market contexts: price, OI, volume, funding |
Example:
curl https://api.gx.exchange/stats/total_users
# {"total_users": 15432}
curl https://api.gx.exchange/stats/asset_ctxs
# [{"coin":"BTC-USD","mark_price":65000,"open_interest":1234567,"volume_24h":98765432,...}]No authentication required. Rate limited to 100 requests/minute per IP.
Request Format
All REST requests and responses use JSON. Set the Content-Type: application/json header on every request that includes a body.
The API exposes two primary REST endpoints:
POST /info— Public data queries. No authentication required. Thetypefield in the request body determines which data is returned.POST /exchange— Authenticated exchange actions (orders, cancels, transfers). Requires an EIP-712 signature.
Additional RESTful endpoints are available for specific resources:
| Method | Path | Auth | Description |
|---|---|---|---|
GET | /v1/health | None | Health check |
GET | /v1/height | None | Current block height |
GET | /v1/perpetualMarkets | None | List all perpetual markets |
GET | /v1/orderbooks/perpetualMarket/:ticker | None | Perpetual orderbook |
GET | /v1/addresses/:address/subaccountNumber/:n | None | Subaccount state |
POST | /v1/orders | EIP-712 | Place an order |
POST | /v1/orders/cancel | EIP-712 | Cancel an order |
GET | /v1/fills | None | Recent fills |
GET | /v1/trades/perpetualMarket/:ticker | None | Recent trades |
GET | /v1/spot/markets | None | List spot markets |
GET | /v1/spot/orderbook/:pair | None | Spot orderbook |
POST | /v1/api-keys | EIP-712 | Create API key |
GET | /v1/api-keys/:address | HMAC | List API keys |
POST | /v1/api-keys/:api_key/revoke | HMAC | Revoke API key |
Authentication Model
GX Exchange uses EIP-712 typed data signatures for all authenticated exchange operations. Every exchange action is signed with an Ethereum private key, providing cryptographic proof of authorization without transmitting secrets.
Direct Wallet Signing
Sign each request with the private key of the trading address. This is the simplest approach but requires the main wallet key to be available at signing time.
API Wallets (Agent Delegation)
For automated trading, authorize a secondary keypair (an “agent”) to sign on your behalf:
- Generate a fresh Ethereum keypair
- Authorize it via the
approveAgentexchange action signed by your main wallet - Use the agent’s private key for all subsequent request signatures
- Revoke the agent at any time
See Nonces and API Wallets and Signing for implementation details.
HMAC-SHA256 Authentication (API Keys)
API key endpoints use a separate HMAC-SHA256 signing scheme with three headers:
| Header | Description |
|---|---|
GX-API-KEY | Your public API key (e.g., gx_k_7f3a...) |
GX-API-SIGN | Base64-encoded HMAC-SHA256 signature |
GX-API-TIMESTAMP | Unix epoch timestamp in seconds |
The signature is computed as:
message = timestamp + method + path + body
signature = base64( HMAC-SHA256( secret, message ) )The server enforces a 30-second replay window on the timestamp.
EIP-712 Domain
All EIP-712 signatures use the following domain:
| Field | Value |
|---|---|
name | "GXExchange" |
version | "1" |
chainId | 42069 |
verifyingContract | 0x0000000000000000000000000000000000000000 |
Response Format
Successful responses return HTTP 200 with a JSON body.
Exchange endpoint responses include a status field:
{ "status": "ok", "response": { "type": "order", "data": { "statuses": [...] } } }{ "status": "err", "response": "Insufficient margin" }Info endpoint responses return the requested data directly as the response body.
Quick Start
import { GxClient } from "@gx-exchange/sdk";
const client = new GxClient();
await client.connect("https://api.gx.exchange");
// Public data -- no auth required
const mids = await client.getAllMids();
console.log("BTC mid:", mids["BTC"]);
// Authenticated trading -- requires wallet
client.setWallet("0xYourPrivateKey...");
const result = await client.placeOrder([{
coin: "BTC",
isBuy: true,
sz: 0.001,
limitPx: 60000,
tif: "Gtc",
reduceOnly: false,
}]);from gx_exchange.info import Info
from gx_exchange.utils.constants import MAINNET_API_URL
info = Info(MAINNET_API_URL, skip_ws=True)
mids = info.all_mids()
print(f"BTC mid: {mids.get('BTC')}")SDKs
| Language | Package | Install |
|---|---|---|
| TypeScript | @gx-exchange/sdk | npm install @gx-exchange/sdk |
| Python | gx-exchange-python-sdk | pip install gx-exchange-python-sdk |
| Rust | gx_rust_sdk | cargo add gx_rust_sdk |
Source: github.com/GX-EXCHANGE