2. APIs and communication protocols: REST, FIX and WebSocket
Level 13: Connecting to Real Brokers
APIs (Application Programming Interfaces) and communication protocols are the piece that lets two programs talk to each other. They define a set of rules and conventions for building applications and interacting with them. In trading there are three technologies you will run into again and again: REST, FIX and WebSocket.
REST API
REST (Representational State Transfer) is an architecture based on web standards that uses HTTP. It is *stateless* and employs the classic HTTP methods: GET, POST, PUT and DELETE. It is used massively to build interfaces with databases and backend services, and it is the usual way to send orders to a cryptocurrency exchange or to a modern broker.
Its two key ideas are:
- Stateless: every HTTP request from client to server must contain all the information needed to understand and process it. The server remembers nothing about the previous request. This has a very concrete practical consequence that you will see in the next module: every request must be authenticated and signed separately, there is no login session that stays open.
- Uniform interface: a standard set of rules for building APIs, which simplifies the architecture and means that learning one REST API helps you learn the next.
In practice, with REST you ask. If you want to know the price, you request it. If you want to know it continuously, you have to request it over and over: that is called *polling*, and it collides head-on with the rate limits every exchange imposes.
FIX API Protocol 4.2
FIX (Financial Information eXchange) is a communication protocol designed specifically for the real-time exchange of information about securities transactions. It is used across the financial industry, above all on trading platforms, to communicate orders and executions.
Its two key ideas are:
- Real-time communication: it allows fast and efficient communication, which is crucial in financial transactions.
- Standardized: it guarantees that different financial institutions can communicate with each other consistently and reliably.
FIX is not HTTP. It works with persistent sessions, messages made up of tag-value pairs, and sequence numbers that make it possible to detect and recover lost messages. It is the institutional world's standard, and also the heaviest to implement. Version 4.2 remains very common despite its age, precisely because the entire ecosystem supports it.
WebSocket
WebSocket is a communication protocol that provides *full-duplex* channels over a single TCP connection. It is used in applications that need real-time two-way communication.
Its two key ideas are:
- Persistent connection: unlike HTTP, WebSocket allows a continuous flow of data by keeping the connection open between client and server.
- Real time: ideal for applications requiring immediate updates and interactions.
In trading, the typical division is this: WebSocket to receive data, REST to send orders. You subscribe over WebSocket to the order book and to fills, and the server pushes every change to you without your asking; when you decide to trade, you fire off a signed REST request. That combination gives you real-time data without exhausting the rate limits, and a clear, auditable confirmation of every order sent.