MEXC API Overview & Getting Started
MEXC provides a comprehensive API that allows developers and traders to programmatically access trading, account, and market data features. The API supports two protocols: REST API — for executing trades, managing orders, checking balances, and retrieving historical data. Uses standard HTTP requests (GET, POST, DELETE). Base URL: api.mexc.com. WebSocket API — for real-time market data streaming including live order books, trade feeds, candlestick data, and account updates. Base URL: wss://wbs.mexc.com/ws. To get started: (1) Create a MEXC account at mexc.com (referral code mexc-bonus20), (2) Go to your Profile > API Management, (3) Click "Create API" and give it a descriptive label (e.g., "Trading Bot"), (4) Set permissions — choose between Read Only, Spot Trading, Futures Trading, or Withdrawal access. For automated trading, enable both Read and Spot/Futures Trading permissions. Never enable withdrawal permission for trading bots. (5) Optionally restrict the API key to specific IP addresses for additional security, (6) Save your API Key and Secret Key securely. The Secret Key is only shown once. Store it in environment variables, never in code.
REST API Endpoints & Authentication
Key MEXC REST API endpoints. Public (no auth required): GET /api/v3/exchangeInfo — trading rules and pair info; GET /api/v3/depth — order book for a symbol; GET /api/v3/trades — recent trades; GET /api/v3/klines — candlestick/OHLCV data; GET /api/v3/ticker/price — current prices. Private (requires API key and signature): POST /api/v3/order — place a new order; DELETE /api/v3/order — cancel an order; GET /api/v3/openOrders — list open orders; GET /api/v3/account — account balance and info; GET /api/v3/myTrades — your trade history. Authentication: All private endpoints require three headers: X-MEXC-APIKEY (your API key), a timestamp parameter, and a signature (HMAC SHA256 of the query string using your Secret Key). The signature prevents request tampering. Rate limits: 20 requests per second for order endpoints, 10 requests per second for other endpoints. Rate limits are per API key. Exceeding them returns HTTP 429. The API follows Binance-compatible format, so most libraries built for Binance (like python-binance or ccxt) work with minimal modifications.
Sign Up & Claim Your Bonus
Use Code mexc-bonus20 Claim Bonus
Building a Trading Bot with Python
The easiest way to build a MEXC trading bot is using the ccxt library, which supports MEXC alongside 100+ other exchanges. Install with: pip install ccxt. Basic setup: import ccxt, create an exchange instance with exchange = ccxt.mexc({apiKey: YOUR_API_KEY, secret: YOUR_SECRET}). Fetch balances: exchange.fetch_balance(). Place a limit buy order: exchange.create_limit_buy_order("BTC/USDT", amount, price). Place a market sell order: exchange.create_market_sell_order("BTC/USDT", amount). Cancel order: exchange.cancel_order(order_id, "BTC/USDT"). Fetch OHLCV data: exchange.fetch_ohlcv("BTC/USDT", "1h", limit=100). For real-time data, use the WebSocket: subscribe to trade streams or order book updates for instant data without polling. Best practices for production bots: (1) Always use environment variables for API keys, never hardcode them, (2) Implement proper error handling — network timeouts, rate limit responses, and exchange errors, (3) Add logging to track every order and decision, (4) Test thoroughly on demo/testnet before using real funds, (5) Set maximum position sizes and daily loss limits in your code, (6) Monitor your bot continuously — automated does not mean unattended.
TradingView Integration & Webhook Alerts
You can connect TradingView alerts to MEXC for semi-automated or fully automated trading. TradingView allows you to create alerts based on technical indicators, price levels, or custom Pine Script strategies. When an alert fires, it can send a webhook HTTP request to your server, which then executes a trade on MEXC via the API. Setup workflow: (1) Write a trading strategy or set up an indicator alert in TradingView, (2) Create a webhook receiver — this is a small web server (Python Flask, Node.js Express, etc.) that listens for TradingView webhook POST requests, (3) In the webhook handler, parse the alert message (which can include symbol, side, amount), (4) Use the MEXC API (via ccxt or direct REST calls) to execute the corresponding trade, (5) In TradingView, set the alert webhook URL to your server address. Important considerations: your webhook server must be always online (use a cloud service like AWS, DigitalOcean, or Railway), add authentication to your webhook endpoint to prevent unauthorized trade execution, implement rate limiting and validation on incoming alerts, and keep logs of every alert received and trade executed for debugging and tax purposes. Many traders use this approach to automate Pine Script strategies that they have backtested on TradingView, executing them on MEXC zero-fee spot pairs for maximum profitability.