LogoLogo
AppBlogGovSocials
  • What is Premia?
    • Premia Origins
      • History
      • Principles
      • Mission, Vision, & Values
  • Quick Start
  • The Premia Protocol
    • Options on Premia
      • Options Primer
      • Order-book vs. AMM
    • Key Protocol Features
      • Base Layer (Exchange)
      • Depot Layer (Vaults)
      • Messaging Layer (RFQ & OB)
      • Fee Schedule
      • Differentiators
    • Concepts
      • LP Range Orders
      • Trading
      • Orderbook & Request-for-Quote (RFQ)
      • Fees
      • Exercise & Settlement
      • Margin
      • Oracles
      • Liquidity Mining
      • Referral Programs
      • Advanced Exchange Concepts
        • Token Integration Requirements
        • Flash Swaps and Loans
      • Vault Depot
        • What is a Depot?
        • APY Calculation
        • Underwriter Depot
    • Governance
      • Premian Civitas
        • Parliament
        • Blue Descent
        • Premian Assembly
        • "Influence" Politics
      • Voting (vxPremia)
        • vxPremia Manifold
        • vxPremia Rewards
          • Options Liquidity Mining
          • Protocol Commissions
          • AirDrip Initiative
      • Operator & Facilitator Role
        • Operational Expenditure and Maintenance Costs
        • Insurance Fund
      • Meta Economics
  • Developer Center
    • APIs
      • Orderbook API
        • REST API
        • WEBSOCKET
      • Subgraph API
      • Containerized API
        • API Reference
          • Orderbook
            • Quotes
            • Orders
          • Pool
            • Settle
            • Exercise
            • Annihilate
          • Account
            • Collateral Approval
            • Orders
            • Collateral Balances
            • Native Balance
            • Option Balances
          • Pools
            • Find Pools
            • Create Pools
            • Valid Maturities
            • Valid Strikes
          • Oracles
            • IV
            • Spot
          • Vaults
            • Quote
            • Trade
        • WebSockets Reference
          • Authorization
          • Connection
          • Quotes Channel
          • RFQ Channel
    • SDK
      • Guides
      • SDK Technical Docs
    • Contracts
      • Multi-Signature Wallets
      • V3 Protocol
        • Overview
        • Guides
        • Smart Contract Technical Documentation
      • Margin
      • Orderbook
      • vxPremia
      • Universal Router
      • Vaults
    • Integrations
      • Use Cases
      • Code Snippets
    • Subgraph
    • V3 Change log
  • Bots
    • Range Order Bot
  • Resources
    • VIP & Institutional Services
    • Media Kit
    • Bug Bounty
    • Audits
    • Community Groups
    • Research
      • SSVI
    • Legal, Terms, & Disclaimers
      • Terms of Service
      • Privacy Policy
      • Cookie Policy
      • Prohibited Use
      • Affiliate & Advocate Program
      • Self-Service Affiliate Agreement
      • Bug Bounty Terms
      • Academy/Newsletter Disclaimer
      • Trading Competition Terms and Disclaimers
      • Blue Descent
      • Legal Disclaimer
      • Media Disclaimer
      • AML/KYC Policies
      • Intellectual Property
      • Amendments & Updates
      • Contact Us
      • FAQs (WIP)
    • 📰Newsletter
    • 🌊Crypto News Blotter
    • 🎓Academy
    • 🔵Premia Blue (V3)
    • 💎Premia V2 Docs
Powered by GitBook

Terms and Disclaimers

  • Privacy Policy
On this page
  • Authorization Flow
  • API KEY

Was this helpful?

  1. Developer Center
  2. APIs
  3. Containerized API
  4. WebSockets Reference

Authorization

Authorization Flow

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 Code
NOTE: since this is an API,  WS logic can be written in any language. This example
is in typescript, but the overall sequence will be the same for any language.
*/

import { RawData, WebSocket } from 'ws'

// Example if Containerized API runtime is local
const url = `ws://localhost:${process.env.HTTP_PORT}`
const wsConnection = new WebSocket(url)
const MOCK_API_KEY = '3423ee2bfd89491f82b351404'

const authMessage: AuthMessage = {
  type: "AUTH",
  apiKey: MOCK_API_KEY,
  body: null
}

// initialize variable to store authorization response
let infoMessage = ''

// handle message ingestion from ws
const wsCallback = (data: RawData) => {
  const message: InfoMessage | ErrorMessage | RFQMessage = JSON.parse(data.toString())
  
  switch (message.type) {
    case 'INFO': {
        // auth result message will arrive here
	infoMessage = message.message
	break
    }
    default: {
  	throw `Unexpected message type ${message.type}`
    }
  }
}

// subscribe/listen to WS messages
wsConnection.on('message', wsCallback)

// send AuthMessage to authorize connection 
wsConnection.send(JSON.stringify(authMsg))

// unsubscribe/remove message listener
wsConnection.off('message', wsCallback)

// should say `Session authorized. Subscriptions enabled.`
console.log(infoMessage)

// auth message 
interface AuthMessage {
  type: 'AUTH'
  apiKey: string
  body: null
}

// INFO message from Premia orderbook
interface InfoMessage {
  type: 'INFO'
  body: null
  message: string
}

PreviousWebSockets ReferenceNextConnection

Last updated 1 year ago

Was this helpful?