Subscribing to Advanced Trade WebSocket channels

Hi @muktupavels! We would like to inform you that the interval is not daily, updates will be sent every 250ms if there are any updates applicable. However, the fields returned in the status channel do not change often (base increment for example is not something that would change every 250ms), so the data is mostly consistent and changes infrequently.

We hope this clarifies. Happy Holidays!

Hi this thread was in Nov/Dec timeframe and some of the documentation etc has been updated. However, I’m getting some of the same types of errors reported above, for my python code I have:

 33     now = int(time.time())
 34     str_to_sign_websocket = str(now) + 'level2' + ',BTC-USD'
 35     signature_websocket = hmac.new(str_to_sign_websocket.encode('utf-8'), coinbase_config['API_SECRET'].encode('utf-8'), hashlib.sha256).hexdigest()
 36     print(signature_websocket)
 37     subscribe_message = {
 38         "type": "subscribe",
 39         'product_ids' : ['BTC-USD'] ,
 40         "channel": "level2",
 41         "signature" : signature_websocket,
 42         "timestamp": str(now),
 43         "api_key": coinbase_config['API_KEY'],
 44     }
 45     print(json.dumps(subscribe_message))
 46     ws.send(json.dumps(subscribe_message))

this results in the following error:

{"type":"error","message":"channel `level2` is not supported"}

the redacted message looks like

{"type": "subscribe", "product_ids": ["BTC-USD"], "channel": "level2", "signature": "535130085e825858d9be9445b305f5e2c067e82a53876f75359ae266f2bf3022", "timestamp": "1678998959", "api_key": "xxxxxxxxxxxxxxxxxxxx"}

Can someone help here. I’m able to subscribe to the Rest API with the same keys no problem.

Don’t know if that is problem but try removing comma before BTC-USD in str_to_sign_websocket.

Thanks for the suggestion, same result however. I also tried the way the documentation describes it (Overview | Coinbase Cloud):

{
    "type": "subscribe",
    "product_ids": [
        "ETH-USD",
        "ETH-EUR"
    ],
    "channels": [
        "level2",
        "heartbeat",
        {
            "name": "ticker",
            "product_ids": [
                "ETH-BTC",
                "ETH-USD"
            ]
        }
    ]
}

but this results in this error

{"type":"error","message":"channel is required"}```

The docs you linked to here are for the old Coinbase Pro feed. You want the Coinbase Advanced docs. You had it right in the first post with the signature etc. You’re getting the “channel is required” error because the old ws feed required “channels” while the new one requires “channel”.

This makes me wonder if you’re referencing the old docs to get the endpoint as well. Make sure you’re hitting the new endpoint with the new sub format and see if that helps. Should be wss://advanced-trade-ws.coinbase.com.

Hi jmicko,

Thanks, I was indeed using the incorrect endpoint. For those that come here and would like a (minimal) working python example this has been working for me on level 2 data:

with open("coinbase.yml", 'r') as file:
    coinbase_config = yaml.safe_load(file)

print(coinbase_config)

def on_open(ws):

    product_ids = ["BTC-USD"]
    message = {
        "type": "subscribe",
        "product_ids": product_ids,
        "channel": "level2",
        'api_key' : coinbase_config['API_KEY'],
    }

    nonce = int(time.time())
    to_sign = f"{nonce}{message['channel']}{','.join(message['product_ids'])}"
    signature = hmac.new(coinbase_config['API_SECRET'].encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
    message['signature'] = signature
    message['timestamp'] = str(nonce)

    ws.send(json.dumps(message))


def on_message(ws, message):
    print(message)

url_websocket = 'wss://advanced-trade-ws.coinbase.com'

#websocket.enableTrace(True)
ws = websocket.WebSocketApp(url_websocket,on_open=on_open,on_message=on_message)
ws.run_forever()
2 Likes