binance api documentation python

Published: 2026-04-12 20:10:25

Binance API Documentation: A Python Guide

Binance, one of the world's largest cryptocurrency exchanges by volume, provides comprehensive APIs that allow users to interact with its services in various ways. The RESTful APIs provided by Binance are designed for developers who wish to integrate trading into their applications or perform any other tasks supported by the platform. This article aims to guide Python developers through using Binance API documentation effectively, focusing on how to authenticate requests, retrieve data, and execute trades programmatically.

Understanding Binance APIs

Binance offers several types of APIs:

1. WebSocket Streaming API: Allows users to receive real-time updates about order book changes, trades, and various other events like symbol status update and balance change.

2. HTTP REST API (Version 1)](https://github.com/Binance/binance-official-api-docs/blob/master/rest_test.md): Supports basic services such as user data stream, account balance retrieval, and order placement.

3. WebSocket Subscription API: An advanced version of the WebSocket Streaming API that supports multiple symbols in a single connection.

4. HTTP REST API (Version 2)](https://github.com/Binance/binance-official-api-docs/blob/master/websocket.md): Intended for higher throughput, it's more suitable for high-frequency trading but lacks some features available in Version 1.

5. FAPI (Forex API): Primarily intended for forex market participants and traders that prefer the exchange of currencies.

6. Dapi API: Designed specifically for Korean developers.

7. Spot and Margin Testnet APIs: These are for developers who want to test their applications on a Binance-supported environment.

Authenticating Requests in Python

To use any Binance API, you first need an API key or access token (depending on the version of the API). For HTTP REST API (Version 1) and WebSocket Streaming API, you'll typically generate a signature using your API key and secret. Here is how to do this in Python:

```python

import hashlib

import hmac

import time

api_key = 'YOUR_API_KEY'

secret_key = 'YOUR_SECRET_KEY'

timestamp = int(time.time())

method = 'GET'

uri = '/api/v1/order'

message = f'{timestamp}{method}{uri}'

signature = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()

```

This example demonstrates creating a signature for the `/api/v1/order` endpoint using API key and secret:

The timestamp is generated based on current time in seconds since epoch.

The method (`GET`) and URI are concatenated with the timestamp.

An HMAC SHA256 hash of this message string is computed using your secret key, yielding a signature unique to every request you make.

Note: Always store your API keys and secrets securely! Never expose them in public or share them with others as they can be used to impersonate you on Binance.

Retrieving Data

Once authenticated, interacting with the API is straightforward using Python's `requests` library:

```python

import requests

from urllib.parse import urlencode

headers = {

'X-MB-APIKEY': 'YOUR_API_KEY',

'Content-Type': 'application/json',

}

params = {'symbol': 'BTCUSDT'}

url = "https://api.binance.com/api/v1/ticker/price"

response = requests.get(url + urlencode(params), headers=headers)

print(response.json())

```

This script retrieves the current price for BTC/USDT trading pair:

It sets up HTTP headers with your API key and specifies JSON content type.

The `requests.get()` function makes a GET request to Binance's /api/v1/ticker/price endpoint, passing in parameters and headers.

Response is returned as a dictionary using the .json() method.

Executing Trades Programmatically

To execute trades programmatically, you can use the `ORDER_CLIENT` endpoint for placing an order:

```python

params = {

'symbol': 'BTCUSDT',

'side': 'BUY', # 'BUY' or 'SELL'

'type': 'LIMIT', # 'LIMIT' or 'MARKET'

'timeInForce': 'GTC', # 'GTC', 'IOC', 'FOK'

'quantity': '1.0',

'price': '123456789',

}

headers = {

'X-MB-APIKEY': 'YOUR_API_KEY',

'Content-Type': 'application/json',

}

url = "https://api.binance.com/api/v1/order"

response = requests.post(url, headers=headers, data=params)

print(response.json())

```

This script sends a POST request to place a limit buy order for 1 BTC at the price of 123456789 USDT:

The `requests.post()` method is used with proper headers and payload (data) parameters.

Response provides information about your new trade order.

Security Note: Always ensure you validate your responses correctly to prevent unauthorized trades or loss of funds due to security vulnerabilities.

Conclusion

Binance APIs offer significant potential for developers, enabling the integration of cryptocurrency trading services into their applications. Python's simplicity and flexibility make it an excellent choice for interacting with these APIs. By following this guide, you should now have a solid understanding of how to authenticate requests, retrieve data, and execute trades programmatically using Binance API documentation in Python.

Recommended for You

🔥 Recommended Platforms