Coinbase TypeScript SDK example: cb-intx-ws.ts
Coinbase Institutional CBInternational Exchange cb intx websocket example for the Siebly Coinbase SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.
What This Example Covers
- Coinbase exchange API example in TypeScript.
- Uses the Siebly Coinbase SDK package
coinbase-apiinstead of hand-written HTTP request plumbing. - Source path:
Coinbase/Institutional/CBInternationalExchange/cb-intx-ws.ts. - Example category: Institutional CBInternational Exchange.
- Imports SDK symbols including
WebsocketClient. - Calls SDK methods such as
on(),subscribe().
How To Use This Example
- Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
- Open the repository source when you need the latest committed version: GitHub source file.
Example Path
Coinbase/Institutional/CBInternationalExchange/cb-intx-ws.ts
Source Link
Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Coinbase/Institutional/CBInternationalExchange/cb-intx-ws.ts
Related SDK Docs
Example Source
import 'dotenv/config';
import { WebsocketClient, WsTopicRequest } from 'coinbase-api';
const client = new WebsocketClient(
{
apiKey: process.env.CB_INTX_API_KEY!,
apiSecret: process.env.CB_INTX_API_SECRET!,
apiPassphrase: process.env.CB_INTX_API_PASSPHRASE!,
},
// logger,
);
client.on('open', (data) => {
console.log('open: ', data?.wsKey);
});
// Data received
client.on('update', (data) => {
console.info(new Date(), 'data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconenct
client.on('reconnect', (data) => {
console.log('reconnect: ', data);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log('reconnected: ', data);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error('close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info('response: ', JSON.stringify(data, null, 2));
});
client.on('exception', (data) => {
console.error('exception: ', data);
});
const OrderbookSubscribeRequest: WsTopicRequest = {
topic: 'LEVEL1', // ws topic
/**
* Anything in the payload will be merged into the subscribe "request",
* allowing you to send misc parameters supported by the exchange (such as `product_ids: string[]`)
*/
payload: {
product_ids: ['BTC-PERP'],
},
};
client.subscribe(OrderbookSubscribeRequest, 'internationalMarketData');