For some reason, the Binance API is no longer working when you deploy an app that uses it in Streamlit Cloud.
I wrote this code:
import requests
import json
import streamlit as st
def get_orders(pair, depth):
response = requests.get(f'https://api.binance.com/api/v3/depth?symbol={pair}&limit={depth}')
byte_json = response.content
orders_json = json.loads(byte_json)
return orders_json
st.write(get_orders('BTCBUSD', 5))
On my computer, it works perfectly.
But when I run in Streamlit Cloud, I receive that error.
It worked in September of 2022, but is no longer working.
Does anyone know any workaround that I can use to solve that?
Goyo
December 27, 2022, 6:12pm
2
For this specific case (server in the US), use binance.us
instead of binance.us
.
This seems to be not documented at all but it worked for me.
The solution is to use https://data.binance.com/api/v3
instead of https://binance.com/api/v3
I tried but not worked here.
Goyo
December 28, 2022, 8:04am
5
I deployed a test application:
https://binance-api.streamlit.app/
import requests
import streamlit as st
options = [
"https://api.binance.com",
"https://api1.binance.com",
"https://api2.binance.com",
"https://api3.binance.com",
"https://api.binance.us",
"https://data.binance.com",
]
base_endpoint = st.selectbox(label="Base endpoint", options=options)
url = f"{base_endpoint}/api/v3/depth?symbol=BTCBUSD&limit=5"
st.write(url)
response = requests.get(url)
st.write(response.json())
Both https://api.binance.us
and https://data.binance.com
work from Streamlit Cloud, but they return different data. In my tests from a non-restricted location https://api.binance.com
returned the same data as https://api.binance.us
.