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

    Crypto Market Data & Trading

    Crypto APIs provides WebSockets with real time data (trades, exchange rates, 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,
        "direction": ["BUY"],
        "exchange_rates": true,
        "order_book_stream": 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,
        direction: ["BUY"],
        exchange_rates: true,
        order_book_stream: 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.
    exchange_rates Optional. Boolean value for exchange rates subscription (possible values are true or false). Default is false.
    order_book_stream Optional. Boolean value for order book stream subscription (possible values are true or false). Default is false.
    exchange_account_ids Optional. Array with exchange account IDs. Subscribe for messages from the exchanges private websocket streams for your Private APIs accounts.

    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

    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

    Order Book (Stream)

    Order book stream message JSON

    
    { 
        "message_type": "order_book_stream",
        "message_command": "update",
        "exchangeId": "5bc0893ea6f99f00093f0202",
        "type": "SPOT",
        "baseAssetId": "5b1ea92e584bf50020130619",
        "quoteAssetId": "5b1ea92e584bf50020130615",
        "exchangeSymbol": "BCHBTC",
        "type": "BIDS",
        "price": 0.032498,
        "size": 40.91,
        "timeExchange": 1578925314527
    }
    
    

    Message data:

    Variable Description
    message_type Message type - "order_book_stream"
    exchangeId ID of the exchange
    type Trade type - SPOT, FUTURES or OPTION
    baseAssetId Base asset ID
    quoteAssetId Quote asset ID
    timeExchange Time of event occurrence
    price Price
    size Base asset amount
    type Type of order book event - BIDS or ASKS
    exchangeSymbol Symbol specific by the exchange where event occurred

    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.

    Exchange Accounts

    Exchange accounts message JSON

    
    { 
        "message_type": "exchange_account",
        "accountId" : "5e610898242b3b70a4eba21a",
        "userId" : "5e610898242b3b70a4eba21a",
        "exchangeId" : "BITFINEX",
        "type" : "wallet update",
        "date" : "2020-04-08T07:18:24.961Z",
        "data" : {
            "walletType" : "exchange",
            "currency" : "BTC",
            "walletBalance" : 0.00856892500234373,
            "unsettledInterest" : 0,
            "availableBalance" : 0.00856892500234373,
            "description" : "Trading fees for 0.00132491 BTC (BTCUSD) @ 7369.9365 on BFX (0.2%)",
            "meta" : null
        }
    }
    
    

    Message data:

    Variable Description
    message_type Message type - "exchange_account"
    accountId ID of the exchange account
    userId ID of the user
    exchangeId ID of the exchange
    type Exchange message type
    date Date time of the message
    data Specific message data (eg. currency, symbol, amount, description etc.)