Building a Binance Futures Client in Python – part I
Posted by

Related reading
Beyond the Buzzwords: Building a Real Blockchain from First Principles
In this post, I build a simple blockchain in Python, focusing on mining, cryptography and the creation of the first coin.

Introduction
In this blog, I will build a client for Binance futures in Python. Although there are already Python packages available, like CCTX (CryptoCurrency eXchange Trading – which supports not only Binance but also other exchanges), building a client from the ground up not only helps with understanding how APIs work but also gives you complete control and flexibility over your trading logic.
One word of warning: I’m a researcher, not a software engineer, which means the code I produce is functional but not necessarily production-grade or optimized for performance. Think of this project more as a research prototype than a polished product.
This blog is meant for educational purposes only. Trading futures involves risk, including losing more than the principal, and may not be suitable for all investors.
What is Binance?
“Binance” is one of the world’s largest cryptocurrency exchanges, offering a wide range of trading products and services. Launched in 2017, it quickly grew to dominate the crypto market by providing users with access to spot trading, derivatives such as futures and options, staking, savings, and more. Known for its deep liquidity, wide range of assets, and advanced trading tools, Binance serves both retail and institutional clients worldwide.
Introduction to Futures and Binance Futures
Futures are legally binding agreements between two parties: one agrees to buy, and the other agrees to sell a commodity or financial instrument at a fixed price on a specified future date. As a result, futures allow you to speculate on the price of a crypto asset without actually owning it.
At the start of a futures transaction, no money changes hands for the full contract value. Instead, both the buyer and seller are required to post margin, a small portion of the contract’s total value, as collateral.
While Binance offers "traditional style" futures (which are dated futures contracts – typically quarterly), most trading volume happens in perpetual futures (also called perpetual swaps, or simply “perps”).
Like traditional futures, perpetual contracts let you speculate on price movements without owning the underlying asset. You're agreeing to exchange the difference between your entry and exit prices.
The key distinction is that perpetuals have no expiration date. You can hold the position indefinitely, but you'll either pay or receive a funding rate at regular intervals (every 8 hours). This funding mechanism helps keep the perp price closely aligned with the spot price, unlike traditional futures, which only converge to spot at expiration.
Because traders only need to post margin, both dated and perpetual futures are inherently leveraged products. In the case of perps, however, you’ll need to set the leverage for each asset in your account explicitly.
What is an API?
API stands for Application Programming Interface. It's a set of rules and protocols that allows different software applications to communicate with each other. When you're building a trading bot or client, the API acts as your gateway to interact programmatically with the exchange—fetching market data, placing trades, managing positions, and more.
Instead of manually using the Binance interface, your program sends requests to the API and receives structured responses in return. This is essential for automation, systematic trading, and the development of advanced strategies.
REST API vs WebSocket API
Binance offers two primary types of APIs: REST and WebSocket, each serving a distinct purpose.
The REST API (Representational State Transfer) operates similarly to a traditional request-response system—the client sends an HTTP request to Binance, and the server responds with the requested data. This method is ideal for placing and canceling orders, querying account balances, fetching historical data, or the latest market prices.
It's simple to use and great for recurring tasks, but it's not ideal for real-time data, as you need to poll the server for updates continually.
In this series, we’ll build our client around the REST API, which is sufficient for most use cases and provides a simpler starting point.
The WebSocket API provides a persistent, real-time connection between your client and Binance. Once connected, Binance pushes live data to your application without needing repeated requests. This method is ideal for streaming live order book updates, tracking real-time trades and price changes, and monitoring your own order and position updates in real-time.
Using WebSockets significantly reduces latency and bandwidth usage compared to constant REST polling—making it the preferred method for high-frequency or latency-sensitive trading systems.
Testnet and Mainnet
There are two ways to trade futures in Binance: Testnet and the Mainnet. The first is a sandbox environment designed for developers and traders to test strategies, bots, and API connections without risking real money. It uses simulated data and assets, allowing you to experiment freely. The Mainnet, on the other hand, is the live trading environment where real funds are at stake, and actual market conditions apply.
Testnet
The Rest API baseurl for testnet is: https://testnet.binancefuture.com, and the testnet Websocket baseurl is wss://stream.binancefuture.com
Mainnet
Binance organizes its endpoints into different categories, or base URLs, depending on the type of futures product you're interacting with. These are:
- FAPI: It gives access to USDⓈ - Margined perpetual Futures and delivery futures API. USDⓈ means USD-Stablecoin, that is, contracts settled in stablecoins like USDT or BUSD. Base URL: https://fapi.binance.com. This is the most commonly used endpoint, and the one most traders start with. Typical usage: Trading BTCUSDT or ETHUSDT perpetuals. The Websocket is wss://fstream.binance.com.
- DAPI: It provides access to the COIN-Margined Futures API. These are contracts where the margin and settlement are done in the underlying cryptocurrency itself (like BTC or ETH). Base URL: https://dapi.binance.com. You deposit and settle in the base coin (e.g., BTC). Typical users are traders who prefer to hold crypto assets as margin and want exposure to linear or inverse contracts. The Websocket is wss://dstream.binance.com.
- PAPI: Portfolio Margin API. This is a more advanced endpoint used by institutions or high-net-worth individuals with access to Portfolio Margin accounts. It offers cross-asset risk calculations, unified margining, and sometimes customized execution rules. Base URL: https://papi.binance.com. This endpoint is not enabled by default; you typically need to apply for or qualify for Portfolio Margin access.
Getting the API Keys
The Binance API supports two types of requests: public and private. Public requests, such as fetching the current order book for BTCUSDT, do not require API keys. However, private requests, like retrieving account information, checking your open positions, or placing an order, must be authenticated using your API keys.
API keys consist of a Key and a Secret. The key acts as your user ID, while the secret is used to sign requests, proving they come from you. For security, Binance allows you to create API keys with specific access permissions (e.g., read-only, trading-enabled, withdrawal-disabled) and IP whitelisting.
Testnet Keys
The image below shows where you can find your Testnet API keys.

Mainnet Keys
You can generate your mainnet API keys from the Binance web interface by going to Profile > API Management
. Treat your API secret like a password; never share it or include it directly in your code. Store it securely using environment variables or a secrets manager.
Conclusion
That concludes Part I of the Building a Binance futures client blog.
In Part II, I will begin implementing the Binance class, starting with its public functions.
In Part III, I will cover the private (signed) endpoints of the Binance Futures API, including how to authenticate requests, check account status, set leverage and margin, place and cancel orders, and review trades and positions.
Want deeper insights into risk and trading strategies? Subscribe to Trading Shepherd today and stay ahead of market volatility!"