Getting First Price from Binance to Google Sheets: A Step-by-Step Guide
Are you a crypto enthusiast or trader looking for ways to automate your data analysis using Google Sheets and the Binance API? In this article, we will walk you through the process of getting the first price from the Binance P2P page in Google Sheets.
Prerequisites:
- You have a Binance account with P2P trading enabled.
- You have a Google Sheet set up to receive data from Binance.
- The IMPORTXML function is not available in Google Sheets, so we will use a workaround using the JSON function and manually parsing the response.
1. Step 2. Break down the response
The Binance P2P site returns an XML response containing price data. To analyze this data in Google Sheets, we need to:
- Use the «JSON.stringify()» method to convert the XML string to a JSON object.
- Then use the «JSON.parse()» function to parse the JSON object.
Here is the code snippet:
import json

Define the Binance API endpoint URLapi_url = " + "UZS"
Parse the XML response into a JSON objectjson_data = json.loads(api_url)
Step 2: Highlight the first price
Once we have the JSON object, we need to extract the first price. Since «JSON.parse()» returns an empty string if the input is not valid JSON, we will use a simple loop to loop through the data and find the first value that matches our search criteria.
Here is the code snippet:
Define the search term (in this case, the first price)search_term = "price"
Initialize the price variablefirst_price = nothing
Iterate through the JSON object to find the matching pricefor the key value json_data.items():
if isinstance(value, str) and search_term.lower() in field value.lower():
Update the first price variable with the matching priceif first_price is None or float(value) < float(first_price):
first_price = float(value)
If we found a match, print the price to Google Sheetsif first_price is not None:
print(f"Price: {first_price}")
3. Step 1: Exit Google Sheets
To print the first price in Google Sheets, you will need to create an array with the price value and then use IMPORTXML or a similar method in Google Sheets to transfer this data.
Here is an example of how to do this.
import json
Define the Binance API endpoint URLapi_url = " + "UZS"
Parse the XML response into a JSON objectjson_data = json.loads(api_url)
Extract the first price from the JSON objectsearch_term = "price"
first_price = nothing
Iterate through the JSON object to find the matching pricefor the key value json_data.items():
if isinstance(value, str) and search_term.lower() in field value.lower():
Update the first price variable with the matching priceif first_price is None or float(value) < float(first_price):
first_price = float(value)
Create a new row array with the price valueprice_array = [first_price]
Transfer data to Google Sheets using IMPORTXMLimportxml_value = "YOUR_BINARYSEAFD_API_URL_HERE"
importxml_output = f"YOUR_BINARYSEAFD_API_URL_HERE?action=importdata&json={importxml_value}"
print (importxml_output)
Usage Example:
Let’s say you have a Binance API endpoint URL that returns the following XML response:
You can modify the above code snippet to extract the first price from the XML response and output it to Google Sheets as follows:
- Replace “api_url” with the URL of your Binance API endpoint.
- Update the variable «search_term» to match the value of the «price» element in the XML response.