API request not working in Streamlit sharing

Hi! I am trying to work with some API return data. My code is working on the local instance of Streamlit but when I deploy the code to Streamlit sharing, I am unable to get data from the API (returns Error 403). Same code works on local instance of Streamlit. Please help.

Code is as below

    import streamlit as st
    import pandas as pd
    import requests
    import json
    from datetime import date,datetime
    import geopandas as gpd
    import plotly.express as px

    state_index = {'Andaman and Nicobar Islands': 1,
     'Andhra Pradesh': 2,
     'Arunachal Pradesh': 3,
     'Assam': 4,
     'Bihar': 5,
     'Chandigarh': 6,
     'Chhattisgarh': 7,
     'Dadra and Nagar Haveli': 8,
     'Daman and Diu': 37,
     'Delhi': 9,
     'Goa': 10,
     'Gujarat': 11,
     'Haryana': 12,
     'Himachal Pradesh': 13,
     'Jammu and Kashmir': 14,
     'Jharkhand': 15,
     'Karnataka': 16,
     'Kerala': 17,
     'Ladakh': 18,
     'Lakshadweep': 19,
     'Madhya Pradesh': 20,
     'Maharashtra': 21,
     'Manipur': 22,
     'Meghalaya': 23,
     'Mizoram': 24,
     'Nagaland': 25,
     'Odisha': 26,
     'Puducherry': 27,
     'Punjab': 28,
     'Rajasthan': 29,
     'Sikkim': 30,
     'Tamil Nadu': 31,
     'Telangana': 32,
     'Tripura': 33,
     'Uttar Pradesh': 34,
     'Uttarakhand': 35,
     'West Bengal': 36}

    st.set_page_config(layout = 'wide')
    def VaccineCheck(state):
        today = date.today()
        current_date = today.strftime("%d-%m-%Y")   ##Gets todays Date

        st_id = str(state_index[state])
        headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:85.0) Gecko/20100101 Firefox/85.0'}
        district_url = "https://cdn-api.co-vin.in/api/v2/admin/location/districts/"+st_id

        response = requests.get(district_url,headers = headers)


When running this on local Streamlit I get the response code 200. But when I run it online on Streamlit sharing, I get Error 403. Is it an issue with the headers? Please help.

Thank you!

I also get an 403 if i use this url in the browser.
Check the message that explains the reason:

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. 

Maybe geoblocking?

Hi @adigigo, welcome to the Streamlit community! :wave: :partying_face:

The CoWin API is geo-fenced to allow only Indian IP addresses to connect. You are able to run your app without the 403 error on your local machine as you might be based in India.

Here’s a list of previous threads on the same topic:

  1. Unable to host streamlit app using heroku due to error: cannot create /app/.streamlit/config.toml - #15 by asehmi
  2. Streamlit App not working when deployed - #2 by Nikhil0504

Best, :balloon:

1 Like