API Reference
Complete API documentation for LX - WebSocket, REST, and gRPC APIs
LX API Documentation
Specification: LP-9002 DEX API & RPC | Discussions
Overview
LX provides multiple API protocols for integration:
- JSON-RPC 2.0 - Primary RPC interface (
dex.*namespace) - gRPC - High-performance binary protocol (port 9760)
- WebSocket - Real-time push feeds
- REST - Standard HTTP endpoints
- FIX - Financial Information eXchange (institutional)
Implementation Status
| Component | Source | Status |
|---|---|---|
| JSON-RPC API | pkg/api/jsonrpc.go | Complete |
| WebSocket Server | pkg/api/websocket_server.go | Complete |
| Trader Client | pkg/client/trader_client.go | Complete |
| gRPC Protocol | pkg/grpc/pb/ | Complete |
WebSocket API
Connection
ws://localhost:8081/ws # Development
wss://ws.lux.exchange/v1 # ProductionEndpoints
| Protocol | Development | Production |
|---|---|---|
| WebSocket | ws://localhost:8081 | wss://ws.lux.exchange |
| REST | http://localhost:8080 | https://api.lux.exchange |
| gRPC | localhost:9760 | grpc.lux.exchange:9760 |
| FIX 4.4 | localhost:9880 | fix.lux.exchange:9880 |
Message Format
All WebSocket messages use JSON:
{
"type": "message_type",
"data": {},
"timestamp": 1234567890
}Subscriptions
Order Book
Subscribe to real-time order book updates:
{
"type": "subscribe",
"channel": "orderbook",
"symbol": "BTC-USDT",
"depth": 20
}Response:
{
"type": "orderbook_snapshot",
"symbol": "BTC-USDT",
"bids": [[50000, 1.5], [49999, 2.0]],
"asks": [[50001, 1.2], [50002, 0.8]],
"timestamp": 1234567890
}Trades
Subscribe to trade stream:
{
"type": "subscribe",
"channel": "trades",
"symbol": "BTC-USDT"
}Response:
{
"type": "trade",
"symbol": "BTC-USDT",
"price": 50000,
"size": 0.5,
"side": "buy",
"timestamp": 1234567890
}Trading Operations
Place Order
{
"type": "place_order",
"symbol": "BTC-USDT",
"side": "buy",
"order_type": "limit",
"price": 50000,
"size": 0.1,
"time_in_force": "GTC",
"post_only": false
}Response:
{
"type": "order_placed",
"order_id": "123e4567-e89b-12d3-a456-426614174000",
"status": "open",
"filled_size": 0,
"remaining_size": 0.1,
"timestamp": 1234567890
}Cancel Order
{
"type": "cancel_order",
"order_id": "123e4567-e89b-12d3-a456-426614174000"
}REST API
Base URL
http://localhost:8080/api/v1 # Development
https://api.lux.network/v1 # ProductionEndpoints
GET /orderbook/:symbol
Get order book snapshot.
Parameters:
symbol(path): Trading pair (e.g., BTC-USDT)depth(query): Book depth (default: 20, max: 100)
Response:
{
"symbol": "BTC-USDT",
"bids": [
{"price": 50000, "size": 1.5},
{"price": 49999, "size": 2.0}
],
"asks": [
{"price": 50001, "size": 1.2},
{"price": 50002, "size": 0.8}
],
"timestamp": 1234567890
}POST /orders
Place a new order.
Request:
{
"symbol": "BTC-USDT",
"side": "buy",
"type": "limit",
"price": 50000,
"size": 0.1,
"time_in_force": "GTC"
}Response:
{
"order_id": "123e4567-e89b-12d3-a456-426614174000",
"symbol": "BTC-USDT",
"side": "buy",
"type": "limit",
"price": 50000,
"size": 0.1,
"status": "open",
"created_at": 1234567890
}DELETE /orders/:order_id
Cancel an order.
Response:
{
"order_id": "123e4567-e89b-12d3-a456-426614174000",
"status": "cancelled",
"cancelled_at": 1234567890
}GET /trades
Get recent trades.
Parameters:
symbol(query): Trading pairlimit(query): Number of trades (default: 100, max: 1000)
Response:
{
"trades": [
{
"trade_id": "abc123",
"symbol": "BTC-USDT",
"price": 50000,
"size": 0.5,
"side": "buy",
"timestamp": 1234567890
}
]
}gRPC API
Proto Definition
syntax = "proto3";
package lxdex;
service TradingService {
rpc PlaceOrder(OrderRequest) returns (OrderResponse);
rpc CancelOrder(CancelRequest) returns (CancelResponse);
rpc GetOrderBook(OrderBookRequest) returns (OrderBookResponse);
rpc StreamTrades(StreamRequest) returns (stream Trade);
}
message OrderRequest {
string symbol = 1;
string side = 2;
string type = 3;
double price = 4;
double size = 5;
}
message OrderResponse {
string order_id = 1;
string status = 2;
int64 timestamp = 3;
}Connection
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
client := pb.NewTradingServiceClient(conn)JSON-RPC 2.0
Endpoint
POST http://localhost:8080/rpcMethods
| Method | Description | Parameters |
|---|---|---|
lx_ping | Test connectivity | {} |
lx_getInfo | Get node information | {} |
lx_placeOrder | Place order | symbol, type, side, price, size, userID |
lx_cancelOrder | Cancel order | orderId |
lx_getOrder | Get order details | orderId |
lx_getOrderBook | Get order book | depth |
lx_getBestBid | Get best bid | {} |
lx_getBestAsk | Get best ask | {} |
lx_getTrades | Get recent trades | limit |
Example
curl -X POST http://localhost:8080/rpc \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "lx_placeOrder",
"params": {
"symbol": "BTC-USD",
"type": 0,
"side": 0,
"price": 50000,
"size": 1.0,
"userID": "trader1"
},
"id": 1
}'Authentication
API Key
Include in headers:
X-API-Key: your_api_key_here
X-API-Secret: your_api_secret_hereJWT
After login:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...Rate Limiting
Limits
| Endpoint Type | Limit |
|---|---|
| Public endpoints | 100 req/min |
| Private endpoints | 300 req/min |
| WebSocket connections | 5 concurrent |
| Order placement | 10 orders/sec |
Headers
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1234567890Error Handling
Error Format
{
"error": {
"code": "INSUFFICIENT_BALANCE",
"message": "Insufficient balance for order",
"details": {
"required": 100.5,
"available": 50.0
}
}
}Error Codes
| Code | Description |
|---|---|
INVALID_SYMBOL | Trading pair not supported |
INVALID_ORDER_TYPE | Order type not recognized |
INSUFFICIENT_BALANCE | Not enough balance |
ORDER_NOT_FOUND | Order ID does not exist |
RATE_LIMIT_EXCEEDED | Too many requests |
UNAUTHORIZED | Authentication required |
FORBIDDEN | Access denied |
INTERNAL_ERROR | Server error |
JSON-RPC Error Codes
| Code | Description |
|---|---|
-32700 | Parse Error |
-32600 | Invalid Request |
-32601 | Method Not Found |
-32602 | Invalid Params |
-32603 | Internal Error |