How To Get BTC Price on Binance from Binance API

0 votes

I want to get a good Btc price using the Binance API, but each time I run script, I get the following ERROR:-
value return : null or 36425 or SyntaxError: Unexpected token < in JSON at position 0.

I am unable to understand why this is inconsistent. If anyone could please help me? Here is my code:-

function test () { var options = {muteHttpExceptions: true}; resBTCPrice = JSON.parse(UrlFetchApp.fetch('https://api.binance.com/api/v3/avgPrice?symbol=BTCUSDT',options).getContentText()); Logger.log(resBTCPrice.price) }

Feb 28, 2022 in Blockchain by Aditya
• 7,680 points
2,441 views

1 answer to this question.

0 votes

In the Script environment for Apps, the code is giving us the parsing error as the HTML page including an ERROR is returned to the remote server at times, rather than the expected JSON. Have shared my code below:-



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <TITLE>ERROR: The request could not be satisfied</TITLE> 
</HEAD><BODY> 
<H1>403 ERROR</H1> 
<H2>The request could not be satisfied.</H2> 
<HR noshade size="1px"> 
Request blocked. 
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. 
<BR clear="all"> 
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation. 
<BR clear="all"> 
<HR noshade size="1px"> 
<PRE> 
Generated by cloudfront (CloudFront) 
Request ID: UyFmlcv2qjKSBvlulcdv06KufF5KCoHm0CpZJu28tE5-aw9KvRquHw== 
</PRE> 
<ADDRESS> 
</ADDRESS> 
</BODY></HTML>

The request to CloudFront goes via Google servers. From my own experience, this happens often. It's probably because of the reason that so many Google users are trying to request the data that CloudFront blocks some of Google servers from flooding them with requests. If you're using Google Spreadsheets and you don't mind getting an estimate price instead of the exact price trading on Binance at the moment of the request, you can use the =GOOGLEFINANCE("CURRENCY:BTCUSD") formula to get the price of BTC provided by Google excluding BINANCE. You can request your own script (that is deployed outside of Google servers) in the Apps Script.

resBTCPrice = JSON.parse(UrlFetchApp.fetch('https://example.com/get-btc-price',options).getContentText());

This get-btc-price script then simply requests the original Binance API and prints the output. This way, you can bypass the rate limits (and error message) imposed by CloudFront to Google servers.

answered Feb 28, 2022 by Soham
• 9,710 points

Related Questions In Blockchain

0 votes
1 answer

How to get price from bitcoin to USD with api

If I'm not wrong, is this what ...READ MORE

answered Apr 7, 2022 in Blockchain by Aditya
• 7,680 points
2,913 views
0 votes
1 answer

How to get historic Bitcoin Price Index using coinbase or any other open source API?

I would recommend you to use the ...READ MORE

answered Apr 7, 2022 in Blockchain by Rahul
• 9,680 points
1,376 views
0 votes
2 answers

How to get notified when an event triggers on ethereum smart contract?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Blockchain by aqowcmbevs
2,411 views
0 votes
1 answer

How to get peer admin's certificate and key on Bluemix?

Follow the steps mentioned below: Create connection profile ~/.composer-connection-profiles/bmx-stage1-kubes/connection.json {  "name": ...READ MORE

answered Jul 16, 2018 in Blockchain by slayer
• 29,370 points
1,164 views
0 votes
1 answer
0 votes
1 answer

How exchanges (Binance, Coinbase) record user trade into blockchain

Merkle tree will allow us to multiplex ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,710 points
635 views
0 votes
1 answer

yfinance Crypto symbol list

After what I know about, YahooFinance uses ...READ MORE

answered Mar 24, 2022 in Blockchain by Rahul
• 9,680 points
2,098 views
0 votes
1 answer

Is there a way to get the market cap or market cap rank of a coin using the Binance API?

This is how I did so: https://www.binance.com/exchange-api/v2/public/asset-service/product/get-products Get this ...READ MORE

answered Apr 6, 2022 in Blockchain by Aditya
• 7,680 points
3,014 views
0 votes
1 answer

(bitcoin) Calculate hash from getwork function - how to do it?

This is correct as there is a ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,710 points
731 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP