NAV
Shell HTTP JavaScript Node.JS PHP Ruby Python Java GO
  • Getting started
  • REST APIs
  • WebSockets
  • Standards and Conventions
  • Errors and Rate Limits
  • WebSockets

    Crypto Market Data

    Crypto APIs provides WebSockets with real time data (quotes, trades, etc.) from top crypto exchanges (you can check the list of supported exchanges in REST APIs > Crypto Market Data > List All Exchanges)

    General

    Address

    Crypto APIs WebSockets is located on wss://ws.cryptoapis.io

    Limitations

    Init Message

    Example Init message

    
    {
        "type": "init",
        "api_key": "Your API key",
        "heartbeat": true,
        "subscribe_to_exchanges_ids": ["5b1ea9d21090c200146f7393", "5bc0893ea6f99f00093f0202"],
        "event_type": ["FUTURES", "OPTION", "SPOT"],
        "base_asset_ids": ["5b1ea92e584bf50020130612"],
        "quote_asset_ids": ["5b1ea92e584bf5002013064d"],
        "trades": true,
        "quotes": true,
        "direction": ["BUY"],
        "exchange_rates": true
    }
    
    
    
    const WebSocket = require('ws');
    
    var initMsg = {
        type: "init",
        api_key: "Your API key",
        heartbeat: true,
        subscribe_to_exchanges_ids: ["5b1ea9d21090c200146f7393", "5bc0893ea6f99f00093f0202"],
        event_type: ["FUTURES", "OPTION", "SPOT"],
        base_asset_ids: ["5b1ea92e584bf50020130612"],
        quote_asset_ids: ["5b1ea92e584bf5002013064d"],
        trades: true,
        quotes: true,
        direction: ["BUY"],
        exchange_rates: true
    };
    
    const ws = new WebSocket('wss://ws.cryptoapis.io');
    
    ws.on('open', function open() {
        ws.send(JSON.stringify(initMsg));
    });
    
    ws.on('message', function incoming(message) {
        console.log(message);
    });
    
    

    When your WebSocket connection is established, you must send an Init message which contains:

    Filter Description
    subscribe_to_exchanges_ids Array with exchanges IDs. Send empty array or do not include this filter if you want to receive data for all exchanges.
    event_type Type of symbol (possible values are: SPOT, FUTURES, OPTION or any combination of them)
    base_asset_ids Array with base asset IDs.
    quote_asset_ids Array with quote asset IDs.
    direction Initiator of the transaction - BUY, SELL or both.
    trades Optional. Boolean value for trades subscription (possible values are true or false). Default is true.
    quotes Optional. Boolean value for quotes subscription (possible values are true or false). Default is false.
    exchange_rates Optional. Boolean value for exchange rates subscription (possible values are true or false). Default is false.

    All filters are optional. You will receive all data related to any filter if you skip it.

    Update filters

    Example Update message

    
    {
        "type": "update_filters",
        "direction": ["SELL"]
    }
    
    

    You must send "update_filters" as type of the message and all filters you want to update. Filters will take effect immediately after the update message is received successfully.

    Heartbeat

    You will receive heartbeat message on every 15 seconds if there aren't other messages and if you requested it in the Init message.

    Errors

    Error message JSON

    
    {
        "type": "error",
        "message": "Invalid API key"
    }
    
    

    You will receive error message if you send something wrong or if you reach any of the WebSockets limitations. The connection will be interrupted after the error message is received. You will need to re-connect.

    Messages

    Trades

    Trade message JSON

    
    { 
        "message_type": "trade",
        "exchangeId": "5bc0893ea6f99f00093f0202",
        "type": "SPOT",
        "baseAssetId": "5b1ea92e584bf50020130619",
        "quoteAssetId": "5b1ea92e584bf50020130615",
        "eventTime": 1541078887070,
        "price": 0.45387,
        "amount": 1521.4952,
        "direction": "SELL"
    }
    
    

    Message data:

    Variable Description
    message_type Message type - "trade"
    exchangeId ID of the exchange
    type Trade type - SPOT, FUTURES or OPTION
    baseAssetId Base asset ID
    quoteAssetId Quote asset ID
    eventTime Time of trade
    price Price
    amount Base asset amount
    direction Initiator of the transaction - BUY or SELL

    Quotes

    Quote message JSON

    
    { 
        "message_type": "quote",
        "exchangeId": "5bc0893ea6f99f00093f0202",
        "type": "SPOT",
        "baseAssetId": "5b1ea92e584bf50020130619",
        "quoteAssetId": "5b1ea92e584bf50020130615",
        "timeExchange": 1541078887070,
        "price": 0.45387,
        "size": 1521.4952,
        "exchangeType": "SELL",
        "exchangeSymbol": "btc-usd"
    }
    
    

    Message data:

    Variable Description
    message_type Message type - "quote"
    exchangeId ID of the exchange
    type Trade type - SPOT, FUTURES or OPTION
    baseAssetId Base asset ID
    quoteAssetId Quote asset ID
    timeExchange Time of quote reported by exchange
    price Price
    size Base asset amount
    exchangeType Initiator of the transaction - ASKS or BIDS
    exchangeSymbol Symbol identifier custom for the exchange where trade took place.

    Exchange rates

    Exchange rate message JSON

    
    { 
        "message_type": "exchange_rate",
        "baseAsset": "5b1ea92e584bf50020130619",
        "weightedAveragePrice": 0.46381,
        "medianPrice": 0.43262,
        "weightedMedianAveragePrice": 0.45387,
        "timestamp": 1560328329
    }
    
    

    Message data:

    Variable Description
    message_type Message type - "exchange_rate"
    baseAsset Base asset ID
    weightedAveragePrice Exchange rate between base asset and USD calculated by weighted average of the last trades in every exchange for the last 24 hours by giving more weight to exchanges with higher volume
    medianPrice Exchange rate between base asset and USD calculated by median of the last trades in every exchange for the last 24 hours
    weightedMedianAveragePrice Exchange rate between base asset and USD calculated by median of the last trades in every exchange for the last 24 hours by giving more weight to exchanges with higher volume. Range of the prices is being adjusted by calculating percentage of wrong (extremely low or extremely high) prices
    timestamp Time (in UNIX Timestamp) of the market data used to calculate exchange rate

    Heartbeat

    Heartbeat message JSON

    
    {
        "type": "heartbeat"
    }
    
    

    The heartbeat message is received every 15 seconds if there aren't other messages and if you require it in the Init message.