The LMEX API gives you programmatic access to one of the fastest-growing crypto derivatives platforms. This tutorial walks through everything from authentication to placing your first live order.
Log into your LMEX account at lmex.io, navigate to Account Settings, then API Management. Create a new API key pair — save the secret immediately as it will not be shown again. Set environment variables to keep credentials out of your code.
LMEX uses HMAC-SHA384 request signing. Every authenticated request needs three headers: your API key, a nonce (millisecond timestamp), and an HMAC signature.
import os, time, hmac, hashlib, requests
API_KEY = os.environ["LMEX_API_KEY"]
API_SECRET = os.environ["LMEX_API_SECRET"]
BASE_URL = "https://api.lmex.io/futures/api/v2.3"
def sign(path):
nonce = str(int(time.time() * 1000))
msg = path + nonce
sig = hmac.new(API_SECRET.encode(), msg.encode(), hashlib.sha384).hexdigest()
return {"request-api": API_KEY, "request-nonce": nonce, "request-sign": sig}
def get(path):
return requests.get(BASE_URL + path, headers=sign(path)).json()
def post(path, body):
headers = {**sign(path), "Content-Type": "application/json"}
return requests.post(BASE_URL + path, json=body, headers=headers).json()
summary = get("/market_summary")
btc = next(s for s in summary if s["symbol"] == "BTC-PERP")
price = float(btc["lastPrice"])
print(f"BTC-PERP last price: {price:,.2f}")
ohlcv = get("/ohlcv?symbol=BTC-PERP&resolution=60&limit=100")
closes = [float(c[4]) for c in reversed(ohlcv)]
book = get("/orderbook/L2?symbol=BTC-PERP&depth=10")
best_bid = float(book["buyQuote"][0][0])
best_ask = float(book["sellQuote"][0][0])
print(f"Spread: {best_ask - best_bid:.2f}")
def place_order(symbol, side, size, order_type="MARKET", price=None):
body = {"symbol": symbol, "side": side, "orderType": order_type, "size": size}
if price:
body["price"] = price
return post("/order", body)
result = place_order("BTC-PERP", "BUY", 0.001)
print(f"Order placed: {result['orderID']}")
import websocket, json
def on_message(ws, message):
data = json.loads(message)
if data.get("topic", "").startswith("update:"):
bids = data["data"].get("buyQuote", [])
if bids:
print(f"Best bid: {bids[0][0]}")
ws = websocket.WebSocketApp("wss://ws.lmex.io/ws/oss/futures", on_message=on_message)
ws.run_forever()
LMEX enforces 10 requests per second for order endpoints. Always implement exponential backoff for 429 errors in production bots.
Q: How do I authenticate with LMEX API using Python requests library?
Use your API key and secret to create HMAC-SHA256 signatures for each request. Include the API key in headers and sign the request payload with your secret key following LMEX's authentication documentation.
Q: What Python libraries are required for LMEX API trading automation?
The essential libraries include `requests` for HTTP calls, `hmac` and `hashlib` for authentication, `json` for data handling, and `websocket-client` for real-time data feeds. Additional libraries like `pandas` and `numpy` are helpful for data analysis.
Q: How to handle LMEX API rate limits in Python trading scripts?
Implement exponential backoff using `time.sleep()` and monitor response headers for rate limit information. Use connection pooling with `requests.Session()` and consider implementing a queue system for high-frequency operations.
Q: Can I use LMEX Python API for both testnet and mainnet trading?
Yes, LMEX provides separate endpoints for testnet and mainnet environments. Simply change the base URL in your Python configuration and use different API credentials for each environment to switch between testing and live trading.