No history yet

API Architecture

Connecting to Prediction Markets

To build an effective trading bot, your first task is to establish a stable and fast connection to the market's data stream. The way you connect depends entirely on the platform's architecture. We'll explore two distinct models: Kalshi, a centralized and federally regulated exchange, and Polymarket, a decentralized market built on the Polygon blockchain.

Think of it like accessing two different kinds of buildings. To enter Kalshi's corporate headquarters, you need a company-issued ID badge—an API key. For Polymarket's open, decentralized plaza, you need a universally recognized, cryptographically secure key—your wallet's signature. Each requires a different approach to authentication.

Authentication: Keys vs. Signatures

Kalshi uses a traditional, straightforward authentication method. You generate a unique API key from your account settings, which acts as your secret password. Every request you send to their API must include this key in the header to prove your identity. It's a simple and effective system common across web services.

```python
# Example of a request to the Kalshi API
import requests

api_key = "your_kalshi_api_key"
headers = {
    "Authorization": api_key,
    "Content-Type": "application/json"
}

response = requests.get("https://trading-api.kalshi.com/v2/markets", headers=headers)
print(response.json())

Polymarket, being a decentralized application, requires a different method. Instead of a secret key stored on their servers, you prove ownership of your wallet by signing a message. This uses a standard called EIP-712 signing, which creates a human-readable

structured message that you sign with your wallet's private key. The resulting signature is sent with your API request. This proves you authorized the action without ever revealing your private key. Because these transactions are on the Polygon network, they are typically 'gasless,' meaning Polymarket covers the minor network fees for you.

Fetching Data: REST and WebSockets

Once authenticated, you need to get market data. Both platforms offer two primary ways to do this: a REST API and a WebSocket connection.

REST API: This is used for making individual requests for information that doesn't change constantly. Think of it as asking a question and getting a one-time answer. You would use REST to fetch a list of available markets, check your account balance, or retrieve historical trade data.

WebSocket: For data that updates in real-time, like an order book, a WebSocket is essential. Unlike REST, a WebSocket opens a persistent, two-way communication channel between you and the server. Once the connection is established, the server can push updates to you instantly without you needing to repeatedly ask. This is critical for getting the latest bid and ask prices the moment they change.

Building a robust connection layer means handling the unique quirks of each platform. For Kalshi, a key challenge is managing session tokens

. Your API key is used to request a temporary session token, which then authenticates your subsequent requests. These tokens expire every 30 minutes, so your bot must include logic to automatically refresh the token before it becomes invalid to avoid interrupted service.

Your bot's connection manager should treat session tokens as a short-term lease, not a permanent key. Always check the expiry and refresh proactively.

For both platforms, you must also respect rate limits—the maximum number of requests you can send in a given period. Exceeding these limits will get your connection temporarily blocked. A well-behaved bot includes delays between requests to stay within the allowed limits. It's also wise to implement a [{]}

mechanism for your WebSocket connection. This involves periodically sending a small message to the server to confirm the connection is still alive and prevent it from being terminated due to inactivity.

By building a modular connection layer that abstracts away these platform-specific details, your main trading logic can remain clean and focused. It can simply ask for an authenticated connection without worrying about whether it's using an API key or a signed message, or whether the session token needs refreshing.

Ready to check your understanding?

Quiz Questions 1/5

What is the primary difference in the authentication method required by Kalshi versus Polymarket?

Quiz Questions 2/5

For which of the following tasks would a WebSocket connection be most essential?

With a solid connection manager in place, you're ready to start processing market data and executing trades.