Websocket API Client

WSClient Constructor

coinbase.websocket.websocket_base.WSBase(api_key: str | None = None, api_secret: str | None = None, key_file: IO | str | None = None, base_url='wss://advanced-trade-ws.coinbase.com', timeout: int | None = None, max_size: int | None = 10485760, on_message: Callable[[str], None] | None = None, on_open: Callable[[], None] | None = None, on_close: Callable[[], None] | None = None, retry: bool | None = True, verbose: bool | None = False)[source]

WSBase Client

Initialize using WSClient


Parameters:

  • api_key | Optional (str) - The API key

  • api_secret | Optional (str) - The API key secret

  • key_file | Optional (IO | str) - Path to API key file or file-like object

  • base_url | (str) - The websocket base url. Default set to “wss://advanced-trade-ws.coinbase.com”

  • timeout | Optional (int) - Set timeout in seconds for REST requests

  • max_size | Optional (int) - Max size in bytes for messages received. Default set to (10 * 1024 * 1024)

  • on_message | Optional (Callable[[str], None]) - Function called when a message is received

  • on_open | Optional ([Callable[[], None]]) - Function called when a connection is opened

  • on_close | Optional ([Callable[[], None]]) - Function called when a connection is closed

  • retry | Optional (bool) - Enables automatic reconnections. Default set to True

  • verbose | Optional (bool) - Enables debug logging. Default set to False

WebSocket Utils

coinbase.websocket.websocket_base.WSBase.open(self) None

Open Websocket


Open the websocket client connection.

async coinbase.websocket.websocket_base.WSBase.open_async(self) None

Open Websocket Async


Open the websocket client connection asynchronously.

coinbase.websocket.websocket_base.WSBase.close(self) None

Close Websocket


Close the websocket client connection.

async coinbase.websocket.websocket_base.WSBase.close_async(self) None

Close Websocket Async


Close the websocket client connection asynchronously.

coinbase.websocket.websocket_base.WSBase.subscribe(self, product_ids: List[str], channels: List[str]) None

Subscribe


Subscribe to a list of channels for a list of product ids.

  • product_ids - product ids to subscribe to

  • channels - channels to subscribe to

async coinbase.websocket.websocket_base.WSBase.subscribe_async(self, product_ids: List[str], channels: List[str]) None

Subscribe Async


Async subscribe to a list of channels for a list of product ids.

  • product_ids - product ids to subscribe to

  • channels - channels to subscribe to

coinbase.websocket.websocket_base.WSBase.unsubscribe(self, product_ids: List[str], channels: List[str]) None

Unsubscribe


Unsubscribe to a list of channels for a list of product ids.

  • product_ids - product ids to unsubscribe from

  • channels - channels to unsubscribe from

async coinbase.websocket.websocket_base.WSBase.unsubscribe_async(self, product_ids: List[str], channels: List[str]) None

Unsubscribe Async


Async unsubscribe to a list of channels for a list of product ids.

  • product_ids - product ids to unsubscribe from

  • channels - channels to unsubscribe from

coinbase.websocket.websocket_base.WSBase.unsubscribe_all(self) None

Unsubscribe All


Unsubscribe from all channels you are currently subscribed to.

async coinbase.websocket.websocket_base.WSBase.unsubscribe_all_async(self) None

Unsubscribe All Async


Async unsubscribe from all channels you are currently subscribed to.

coinbase.websocket.websocket_base.WSBase.sleep_with_exception_check(self, sleep: int) None

Sleep with Exception Check


Sleep for a specified number of seconds and check for background exceptions.

  • sleep - number of seconds to sleep.

async coinbase.websocket.websocket_base.WSBase.sleep_with_exception_check_async(self, sleep: int) None

Sleep with Exception Check Async


Async sleep for a specified number of seconds and check for background exceptions.

  • sleep - number of seconds to sleep.

coinbase.websocket.websocket_base.WSBase.run_forever_with_exception_check(self) None

Run Forever with Exception Check


Runs an endless loop, checking for background exceptions every second.

async coinbase.websocket.websocket_base.WSBase.run_forever_with_exception_check_async(self) None

Run Forever with Exception Check Async


Async runs an endless loop, checking for background exceptions every second.

coinbase.websocket.websocket_base.WSBase.raise_background_exception(self) None

Raise Background Exception


Raise any background exceptions that occurred in the message handler.

Channels

coinbase.websocket.channels.heartbeats(self, product_ids: List[str]) None[source]

Heartbeats Subscribe


Description:

Subscribe to heartbeats channel for a list of products_ids.


Read more on the official documentation: Heartbeats Channel

async coinbase.websocket.channels.heartbeats_async(self, product_ids: List[str]) None[source]

Heartbeats Subscribe Async


Description:

Async subscribe to heartbeats channel for a list of products_ids.


Read more on the official documentation: Heartbeats Channel

coinbase.websocket.channels.heartbeats_unsubscribe(self, product_ids: List[str]) None[source]

Heartbeats Unsubscribe


Description:

Unsubscribe to heartbeats channel for a list of products_ids.


Read more on the official documentation: Heartbeats Channel

async coinbase.websocket.channels.heartbeats_unsubscribe_async(self, product_ids: List[str]) None[source]

Heartbeats Unsubscribe Async


Description:

Async unsubscribe to heartbeats channel for a list of products_ids.


Read more on the official documentation: Heartbeats Channel

coinbase.websocket.channels.candles(self, product_ids: List[str]) None[source]

Candles Subscribe


Description:

Subscribe to candles channel for a list of products_ids.


Read more on the official documentation: Candles Channel

async coinbase.websocket.channels.candles_async(self, product_ids: List[str]) None[source]

Candles Subscribe Async


Description:

Async subscribe to candles channel for a list of products_ids.


Read more on the official documentation: Candles Channel

coinbase.websocket.channels.candles_unsubscribe(self, product_ids: List[str]) None[source]

Candles Unsubscribe


Description:

Unsubscribe to candles channel for a list of products_ids.


Read more on the official documentation: Candles Channel

async coinbase.websocket.channels.candles_unsubscribe_async(self, product_ids: List[str]) None[source]

Candles Unsubscribe Async


Description:

Async unsubscribe to candles channel for a list of products_ids.


Read more on the official documentation: Candles Channel

coinbase.websocket.channels.market_trades(self, product_ids: List[str]) None[source]

Market Trades Subscribe


Description:

Subscribe to market_trades channel for a list of products_ids.


Read more on the official documentation: Market Trades Channel

async coinbase.websocket.channels.market_trades_async(self, product_ids: List[str]) None[source]

Market Trades Subscribe Async


Description:

Async subscribe to market_trades channel for a list of products_ids.


Read more on the official documentation: Market Trades Channel

coinbase.websocket.channels.market_trades_unsubscribe(self, product_ids: List[str]) None[source]

Market Trades Unsubscribe


Description:

Unsubscribe to market_trades channel for a list of products_ids.


Read more on the official documentation: Market Trades Channel

async coinbase.websocket.channels.market_trades_unsubscribe_async(self, product_ids: List[str]) None[source]

Market Trades Unsubscribe Async


Description:

Async unsubscribe to market_trades channel for a list of products_ids.


Read more on the official documentation: Market Trades Channel

coinbase.websocket.channels.status(self, product_ids: List[str]) None[source]

Status Subscribe


Description:

Subscribe to status channel for a list of products_ids.


Read more on the official documentation: Status Channel

async coinbase.websocket.channels.status_async(self, product_ids: List[str]) None[source]

Status Subscribe Async


Description:

Async subscribe to status channel for a list of products_ids.


Read more on the official documentation: Status Channel

coinbase.websocket.channels.status_unsubscribe(self, product_ids: List[str]) None[source]

Status Unsubscribe


Description:

Unsubscribe to status channel for a list of products_ids.


Read more on the official documentation: Status Channel

async coinbase.websocket.channels.status_unsubscribe_async(self, product_ids: List[str]) None[source]

Status Unsubscribe Async


Description:

Async unsubscribe to status channel for a list of products_ids.


Read more on the official documentation: Status Channel

coinbase.websocket.channels.ticker(self, product_ids: List[str]) None[source]

Ticker Subscribe


Description:

Subscribe to ticker channel for a list of products_ids.


Read more on the official documentation: Ticker Channel

async coinbase.websocket.channels.ticker_async(self, product_ids: List[str]) None[source]

Ticker Subscribe Async


Description:

Async subscribe to ticker channel for a list of products_ids.


Read more on the official documentation: Ticker Channel

coinbase.websocket.channels.ticker_unsubscribe(self, product_ids: List[str]) None[source]

Ticker Unsubscribe


Description:

Unsubscribe to ticker channel for a list of products_ids.


Read more on the official documentation: Ticker Channel

async coinbase.websocket.channels.ticker_unsubscribe_async(self, product_ids: List[str]) None[source]

Ticker Unsubscribe Async


Description:

Async unsubscribe to ticker channel for a list of products_ids.


Read more on the official documentation: Ticker Channel

coinbase.websocket.channels.ticker_batch(self, product_ids: List[str]) None[source]

Ticker Batch Subscribe


Description:

Subscribe to ticker_batch channel for a list of products_ids.


Read more on the official documentation: Ticker Batch Channel

async coinbase.websocket.channels.ticker_batch_async(self, product_ids: List[str]) None[source]

Ticker Batch Subscribe Async


Description:

Async subscribe to ticker_batch channel for a list of products_ids.


Read more on the official documentation: Ticker Batch Channel

coinbase.websocket.channels.ticker_batch_unsubscribe(self, product_ids: List[str]) None[source]

Ticker Batch Unsubscribe


Description:

Unsubscribe to ticker_batch channel for a list of products_ids.


Read more on the official documentation: Ticker Batch Channel

async coinbase.websocket.channels.ticker_batch_unsubscribe_async(self, product_ids: List[str]) None[source]

Ticker Batch Unsubscribe Async


Description:

Async unsubscribe to ticker_batch channel for a list of products_ids.


Read more on the official documentation: Ticker Batch Channel

coinbase.websocket.channels.level2(self, product_ids: List[str]) None[source]

Level2 Subscribe


Description:

Subscribe to level2 channel for a list of products_ids.


Read more on the official documentation: Level2 Channel

async coinbase.websocket.channels.level2_async(self, product_ids: List[str]) None[source]

Level2 Subscribe Async


Description:

Async subscribe to level2 channel for a list of products_ids.


Read more on the official documentation: Level2 Channel

coinbase.websocket.channels.level2_unsubscribe(self, product_ids: List[str]) None[source]

Level2 Unsubscribe


Description:

Unsubscribe to level2 channel for a list of products_ids.


Read more on the official documentation: Level2 Channel

async coinbase.websocket.channels.level2_unsubscribe_async(self, product_ids: List[str]) None[source]

Level2 Unsubscribe Async


Description:

Async unsubscribe to level2 channel for a list of products_ids.


Read more on the official documentation: Level2 Channel

coinbase.websocket.channels.user(self, product_ids: List[str]) None[source]

User Subscribe


Description:

Subscribe to user channel for a list of products_ids.


Read more on the official documentation: User Channel

async coinbase.websocket.channels.user_async(self, product_ids: List[str]) None[source]

User Subscribe Async


Description:

Async subscribe to user channel for a list of products_ids.


Read more on the official documentation: User Channel

coinbase.websocket.channels.user_unsubscribe(self, product_ids: List[str]) None[source]

User Unsubscribe


Description:

Unsubscribe to user channel for a list of products_ids.


Read more on the official documentation: User Channel

async coinbase.websocket.channels.user_unsubscribe_async(self, product_ids: List[str]) None[source]

User Unsubscribe Async


Description:

Async unsubscribe to user channel for a list of products_ids.


Read more on the official documentation: User Channel

Exceptions

coinbase.websocket.websocket_base.WSClientException()[source]

WSClientException


Exception raised for errors in the WebSocket client.

coinbase.websocket.websocket_base.WSClientConnectionClosedException()[source]

WSClientConnectionClosedException


Exception raised for unexpected closure in the WebSocket client.