Get Real-time Crypto Prices Using Python And Binance API

Get Real-time Crypto Prices Using Python And Binance API

By Pratyush Mishra

·

3 min read

Introduction

Hi everyone👋,In this article, we are going to see how to get the real-time price of cryptocurrencies using Binance API in Python.

A cryptocurrency, crypto-currency, or crypto is a digital currency designed to work as a medium of exchange through a computer network that is not reliant on any central authority, such as a government or bank, to uphold or maintain it.

You can refer to our YouTube video to see a working tutorial for a better understanding and a step-by-step guide of the same.

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 the 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 the JSON module to convert extracted JSON data to a Python dictionary.

Get Crypto Price Using Python And Binance API

# Import libraries
import json
import requests

# defining key/request url
key = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"

# requesting data from url
data = requests.get(key)
data = data.json()
print(f"{data['symbol']} price is {data['price']}")
  • First of all, we will be importing modules like json and requests

  • Now we will be defining the request URL in this case it is api.binance.com as you can note here we have set the symbol = BTCUSDT which stands for Bitcoin / Tether Cryptocurrency Price, you can replace it with DOGEUSDT which simply means Dogecoin in order to fetch the current prices.

  • Here requests.get() will send a request to a specified URL and save it in data and json() converted data to a Python dictionary.

  • In the end, we will be printing the current price using the print method under which we will be passing our dictionary.

Get Multiple Real-time Crypto prices

# Import libraries
import json
import requests

# Defining Binance API URL
key = "https://api.binance.com/api/v3/ticker/price?symbol="

# 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']}")
  • The concept behind this chunk of code is the same as above, but instead of printing the data for single crypto, we will be Making a list for multiple cryptos.

  • Then we will be running for loop in the list which is currencies in this case.

  • While running the for loop we will be concatenating the key with each list element in the currency.

  • Make sure to increment the value of j by using j=j + 1

  • In the end, we will be printing the current price using the print method under which we will be passing our dictionary.

Output

On Executing the application it returns values like this

  • BTCUSDT price is 41522.20000000
  • DOGEUSDT price is 0.14710000
  • LTCUSDT price is 125.00000000

Final Thoughts

That’s it!

In this article, you have successfully learned how to get real-time crypto prices using python and Binance API

Alright, guys! I hope this article was helpful for you, and if was then leave your comments below. I will meet you in another article until then KEEP CODING🤗.

Â