In this article, we are going to see how to get the real-time price of cryptocurrencies using Binance API in Python.
Binance API
Binance API is a method that allows you to connect to the Binance servers using several programming languages. With it, you can automate your trading and make HTTP requests to send and receive data.
Here we access Binance API using Python with requests module. We will be sending requests to Binance API and extracting the real-time price of the required cryptocurrency in JSON format. We will use JSON module to convert extracted JSON data to a Python dictionary.
Example 1: Get Crypto Price Using Python And Binance API
Here requests.get() will send a request to a specified URL and save it in data and json() converted data to a Python dictionary.
Python3
# Import libraries import json import requests # defining key/request url # requesting data from url data = requests.get(key) data = data.json() print (f "{data['symbol']} price is {data['price']}" ) |
Output:
BTCUSDT price is 41522.20000000
Example 2: Get Multiple Real-time Crypto prices
Python3
# Import libraries import json import requests # Defining Binance API URL # Making list for multiple crypto's currencies = [ "BTCUSDT" , "DOGEUSDT" , "LTCUSDT" ] j = 0 # running loop to print all crypto prices for i in currencies: # completing API for request url = key + currencies[j] data = requests.get(url) data = data.json() j = j + 1 print (f "{data['symbol']} price is {data['price']}" ) |
Output:
BTCUSDT price is 41522.20000000 DOGEUSDT price is 0.14710000 LTCUSDT price is 125.00000000