Printing JSON API Results Binance with Python
============================================================================================================? =============. ===
In this article, we will show you how to print the JSON response of a binance API request using the Python’s “Requests ” Library.
Libraries required
——————-
- “Requests”: a popular Python library to make HTTP requests.
JSON: not required but useful for analyzing the JSON answer.
In start -up with the code
—————————
First of all, let’s import the necessary libraries and configure our API keys:
`Python
Import json

Define the key to your binance API and your secret
Binance_api_key = "your_binance_api_key"
Binance_api_secret = "your_binance_api_secret"
Your Binance symbol (for example, ETH, BTC)
Symbol_to_query = "btcusdt"
replace by the ticker for which you want to recover data for
Binanceapi class:
Def __init __ (Self):
self.Api_Key = binance_api_key
self.Api_secret = binance_api_secret
'
Make the API request
-----------------------
Then, let's apply for a binance API with the desired symbol and parameters:
Python
Def Get_Price (Ticker):
URL = F "
headers = {
"X-MBX-Adepikey": Self.Api_Key,
"X-MBX-Secret-Key": self.Api_secret,
}
Response = requests.post (URL, headers = headers)
If response.status_code == 200:
Return JSON.LOADS (response.content)
other:
Print (F "Price recovery error: {Answer.status_code}")
'
Printing of the JSON answer
---------------------------
Now that we have our request for an API, modify it to print only the relevant data:
Python
Def print_api_respons (data):
If ISINSTANCE (data, dict):
Print ("API response:")
For the key, value in data.tems ():
print (f "{key}: {value}")
Elif Isstance (data, list):
Print ("API response:")
For the element in the data:
Print (article)
Call our function with the desired symbol
price_data = get_price (symbol_to_query)
print_api_respons (price_data)
'
Example of use
---------------
Here is an example of how you can change your code to select a specific symbol and display its price:
Python
Import json
Define the key to your binance API and your secret
Binance_api_key = "your_binance_api_key"
Binance_api_secret = "your_binance_api_secret"
Your Binance symbol (for example, ETH, BTC)
Symbol_to_Query = "ethusdt"
replace by the Ticker for which you want to recover data for
Binanceapi class:
Def __init __ (Self):
self.Api_Key = binance_api_key
self.Api_secret = binance_api_secret
Def Get_Price (Ticker):
URL = F "
headers = {
"X-MBX-Adepikey": Self.Api_Key,
"X-MBX-Secret-Key": self.Api_secret,
}
Response = requests.post (URL, headers = headers)
If response.status_code == 200:
Return JSON.LOADS (response.content)
other:
Print (F "Price recovery error: {Answer.status_code}")
Def print_api_respons (data):
If ISINSTANCE (data, dict):
Print ("API response:")
For the key, value in data.tems ():
print (f "{key}: {value}")
Elif Isstance (data, list):
Print ("API response:")
For the element in the data:
Print (article)
Call our function with the desired symbol
price_data = get_price (symbol_to_query)
print_api_respons (price_data)
'
Do not forget to replaceyour_binance_api_key ‘and your_binance_api_secret` by your API key and your secret.