Using the following code, I am not getting any data from alphavantage, I get the following API error:
"Error Message": "Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/documentation/) for TIME_SERIES_DAILY." }
import requests
import alpha_vantage
API_URL = "https://www.alphavantage.co/query"
data = {
"function": "TIME_SERIES_DAILY",
"symbol": "NIFTY",
"outputsize": "compact",
"datatype": "csv",
"apikey": "xxx",
}
response = requests.get(API_URL, params=data)
print(response.json())
I edited the code to try something else, but I still got something strange, this time it was:
Response [200]
Here is the code for that:
import requests
import alpha_vantage
API_URL = "https://www.alphavantage.co/query"
data = {
"function": "TIME_SERIES_DAILY",
"symbol": "US",
"outputsize": "compact",
"datatype": "csv",
"apikey": "xxx"
}
response = requests.get(API_URL, params=data)
print(response)
What going on here?
(Key obfuscated below to xxx
)
There are three issues going on with your attempts to call this API.
In your first attempt, you correctly call the API but use an invalid ticker. NIFTY
along with other global indexes are not supported by the Alpha Vantage API.
On your second attempt, you print out a response object, which when converted to string looks just like the output you received, Response[200]
meaning a successful API call was made. To get the data from it, you have to print response.text
or response.json()
The third issue is more subtle, and depends on what you are trying to return. If you want to return a csv file using the datatype: "csv"
, you cannot use response.json()
since the format isn't for json. You can instead use the default datatype: "json"
, by leaving that field blank.
If you want to get a csv file, you can use print(response.text)
JSON example
import requests
import alpha_vantage
API_URL = "https://www.alphavantage.co/query"
data = {
"function": "TIME_SERIES_DAILY",
"symbol": "M&M.NSE",
"outputsize": "compact",
"apikey": "xxx"
}
response = requests.get(API_URL, params=data)
print(response.json())
CSV example
import requests
import alpha_vantage
API_URL = "https://www.alphavantage.co/query"
data = {
"function": "TIME_SERIES_DAILY",
"symbol": "M&M.NSE",
"outputsize": "compact",
"datatype": "csv",
"apikey": "xxx"
}
response = requests.get(API_URL, params=data)
print(response.text)