MEXC API SDK

repository·main·Indexed 43 days ago

https://github.com/mexcdevelop/mexc-api-sdk

Official SDK for interacting with MEXC exchange's Market and Trade APIs, providing multi-language support for JS, Python, Go, Java, and .NET. Version 1.0.0 allows developers to retrieve market data, manage spot trading operations, and access user account information. Key features include authenticated requests via signRequest(), public data retrieval with publicRequest(), and dedicated modules for spot trading, order management, and candlestick data.

Tokens
4K
Snippets
26
Records
29
Agent score
56%

What's inside MEXC

  1. Initialize the MEXC Spot client

    main

    To interact with the MEXC Spot API, you must initialize a client using your apiKey and apiSecret. The SDK provides implementations for several languages. Below are the initialization patterns for JavaScript, Go, Python, Java, and C#.

    import * as Mexc from 'mexc-sdk';
    const apiKey = 'apiKey';
    const apiSecret = 'apiSecret';
    const client = new Mexc.Spot(apiKey, apiSecret);
  2. Install the MEXC API SDK

    main

    To use the MEXC SDK, you must have Node.js LTS installed. The SDK is distributed as pre-compiled files for multiple languages. Follow these steps to install:

    1. Clone the repository: git clone https://github.com/mxcdevelop/mexc-api-sdk.git
    2. Navigate to the dist/ directory and find the folder corresponding to your target language.
    3. Unzip the files for your language.

    Supported languages include:

    • dotnet
    • go
    • java
    • js
    • python
    git clone https://github.com/mxcdevelop/mexc-api-sdk.git
    cd dist/{language}
    # Unzip the files for your language
  3. Place and manage orders

    main

    The Trade category allows for executing orders, canceling them, and querying order status. Note that newOrderTest can be used to validate order parameters without executing a real trade.

    // Place a new limit order
    client.newOrder('BTCUSDT', 'BUY', 'LIMIT', {
      price: '50000',
      quantity: '0.01',
      timeInForce: 'GTC'
    });
    
    // Cancel an order by ID
    client.cancelOrder('BTCUSDT', { orderId: '12345' });
    
    // Cancel all open orders for a symbol
    client.cancelOpenOrders('BTCUSDT');
  4. Use Market data APIs

    main

    The Market category provides read-only access to exchange information, price data, and order books. Most methods are available via the client instance.

    // Ping the server
    client.ping();
    
    // Check server time
    client.time();
    
    // Get exchange information
    client.exchangeInfo({ symbol: 'BNBBTC' });
    
    // Get recent trades
    client.trades('BTCUSDT', { limit: 500 });
    
    // Get order book depth
    client.depth('BTCUSDT', { limit: 100 });
    
    // Get kline (candlestick) data
    client.klines('BTCUSDT', '1m', { limit: 500 });
  5. Access MEXC SDK modules

    main

    The mexc-sdk package exports its core functionality through a central entrypoint. You can access all major functional modules by importing from the main package. The SDK is organized into the following logical modules:

    • common: Shared utilities and base types.
    • base: Base classes and fundamental SDK logic.
    • market: Market data services (e.g., Order Books, Recent Trades, Exchange Information).
    • userData: User-specific data and account information.
    • spot: Spot trading operations.
    • trade: General trading and order management services.
    // Example of importing modules from the SDK
    import { Market, Spot, Trade } from 'mexc-sdk';
  6. Get aggregate trades with aggTrades()

    main

    Retrieve compressed or aggregate trades list.

    Note: If providing both startTime and endTime, the interval must be less than one hour.

    Options:

    • options.fromId: The ID to start from (inclusive).
    • options.startTime: Timestamp in milliseconds (inclusive).
    • options.endTime: Timestamp in milliseconds (inclusive).
    • options.limit: Number of trades to return. Default 500; max 1000.
    const agg = await market.aggTrades('BTCUSDT', { 
      startTime: 1672531200000, 
      limit: 500 
    });
  7. Get candlestick data with klines()

    main

    Retrieve Kline/Candlestick data for a symbol over a specific interval.

    Parameters:

    • symbol: The trading pair.
    • interval: The candlestick interval (e.g., 1m, 5m, 1h).
    • options.startTime: Start timestamp in ms.
    • options.endTime: End timestamp in ms.
    • options.limit: Number of candles. Default 500; max 1000.
    const candles = await market.klines('BTCUSDT', '1h', { limit: 100 });
  8. Get account trade list with accountTradeList()

    main

    Retrieve the list of trades for a specific symbol. This method performs a signed GET request to the /myTrades endpoint.

    Parameters:

    • symbol (string): The trading pair (e.g., 'BTCUSDT'). The symbol is automatically converted to uppercase.
    • options (object): Configuration for the request. Defaults to { limit: 500 }.

    Options properties:

    • orderId: Can only be used in combination with symbol.
    • startTime: Filter trades starting from this timestamp.
    • endTime: Filter trades ending at this timestamp.
    • fromId: The TradeId to fetch from. If not provided, the most recent trades are returned by default.
    • limit: Number of trades to return. Default is 500, maximum is 1000.
    // Example: Fetching the last 100 trades for BTCUSDT
    const trades = userData.accountTradeList('BTCUSDT', { limit: 100 });
    
    // Example: Fetching trades starting from a specific ID
    const tradesFromId = userData.accountTradeList('ETHUSDT', { fromId: 12345 });
  9. Get ticker price statistics

    main

    Retrieve various ticker-related data.

    • avgPrice(symbol): Returns the current average price for the symbol.
    • ticker24hr(symbol?): Returns 24-hour price change statistics. If no symbol is provided, it returns statistics for all symbols.
    • tickerPrice(symbol?): Returns the current price for a symbol. If no symbol is provided, it returns prices for all symbols.
    • bookTicker(symbol?): Returns the best bid/ask price and quantity (Order Book Ticker). If no symbol is provided, it returns for all symbols.
    // Specific symbol
    const price = await market.tickerPrice('BTCUSDT');
    const stats = await market.ticker24hr('BTCUSDT');
    
    // All symbols
    const allPrices = await market.tickerPrice();
    const allBookTickers = await market.bookTicker();
  10. Get recent or historical trades with trades() and historicalTrades()

    main

    Access trade history for a specific symbol.

    Recent Trades (trades): Retrieves the most recent trades.

    • options.limit: Number of trades to return. Default 500; max 1000.

    Historical Trades (historicalTrades): Retrieves older trade data.

    • options.limit: Number of trades to return. Default 500; max 1000.
    // Recent trades
    const recent = await market.trades('BTCUSDT', { limit: 100 });
    
    // Historical trades
    const history = await market.historicalTrades('BTCUSDT', { limit: 500 });