Minh Dao
Minh Dao

Reputation: 17

How To Get BTC Price on Binance from Binance API

I want to get Btc price from Binance API, but each time I run script , value return : null or 36425 or SyntaxError: Unexpected token < in JSON at position 0. I don't know why this is inconsistent, 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)
}

Upvotes: 1

Views: 14950

Answers (3)

Petr Hejda
Petr Hejda

Reputation: 43491

Other answers provide correct ways to get the data in other environments than Google Apps Script.

In the Apps Script environment, your code throws the parsing error because the remote server sometimes returns a HTML page containing an error - not the expected JSON.

<!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 (that caches the results from the "real" Binance API) goes from Google servers.

From my own experience, this happens often. It's probable that so many Google users are trying to request the data that CloudFront blocks some of Google servers from flooding them with requests.


Workaround 1:

If you're using Google Spredsheets 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 (not by Binance).

Workaround 2:

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.

Upvotes: 1

CodeWalker
CodeWalker

Reputation: 2368

Vanilla JavaScript

fetch('https://api.binance.com/api/v3/avgPrice?symbol=BTCUSDT')
.then(r => r.json()
.then(j => console.log(parseFloat(j.price).toFixed(2))));

Output

36200.65

Upvotes: 1

Toan T
Toan T

Reputation: 567

For example of simple jQuery

var settings = {
  "url": "https://api.binance.com/api/v3/avgPrice?symbol=BTCUSDT",
  "method": "GET",
  "timeout": 0,
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Result

{
    "mins": 5,
    "price": "36174.68934930"
}

Upvotes: 2

Related Questions