Get Real-time Crypto Price Using Python And Binance API - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

Report

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.20000000DOGEUSDT price is 0.14710000LTCUSDT price is 125.00000000


Last Updated : 17 May, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Get Real-time Crypto Price Using Python And Binance API - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6415

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.