Upon connection with the Websocket server, users must first send an AUTH message with an api key. If this step is not done, any requests to Stream public/RFQ quotes, Publish RFQ requests, or Stream RFQ requests will be denied.
API KEY
In order to use websockets, an api key is required. Currently, all api key requests must be made to research@premia.finance. Please use subject line: 'API KEY REQUEST' and an api key will be returned.
/*TypeScript Sample CodeNOTE: since this is an API, WS logic can be written in any language. This exampleis in typescript, but the overall sequence will be the same for any language.*/import { RawData, WebSocket } from'ws'// Example if Containerized API runtime is localconsturl=`ws://localhost:${process.env.HTTP_PORT}`constwsConnection=newWebSocket(url)constMOCK_API_KEY='3423ee2bfd89491f82b351404'constauthMessage:AuthMessage= { type:"AUTH", apiKey:MOCK_API_KEY, body:null}// initialize variable to store authorization responselet infoMessage =''// handle message ingestion from wsconstwsCallback= (data:RawData) => {constmessage:InfoMessage|ErrorMessage|RFQMessage=JSON.parse(data.toString())switch (message.type) {case'INFO': {// auth result message will arrive here infoMessage =message.messagebreak }default: {throw`Unexpected message type ${message.type}` } }}// subscribe/listen to WS messageswsConnection.on('message', wsCallback)// send AuthMessage to authorize connection wsConnection.send(JSON.stringify(authMsg))// unsubscribe/remove message listenerwsConnection.off('message', wsCallback)// should say `Session authorized. Subscriptions enabled.`console.log(infoMessage)
// auth message interfaceAuthMessage { type:'AUTH' apiKey:string body:null}// INFO message from Premia orderbookinterfaceInfoMessage { type:'INFO' body:null message:string}