Python for Finance capstone. Unable to access tiingo databases

Hey there, I haven’t been able to use my api token correctly for accessing S&P data from tiingo.com.

Here is my code. `

import requests
import pandas as pd 
import pandas_datareader.data as web
import pandas_datareader.wb as wb

from fin_data_config import tiingo_token
import numpy as np 
from datetime import datetime 
import os

headers = {
        'Content-Type': 'application/json',
        'Authorization' : 'token {}'.format(tiingo_token)
        }
requestResponse = requests.get("https://api.tiingo.com/api/test/",
                                    headers=headers)

start = datetime(1999, 1, 1)
end = datetime(2019, 1, 1)

gold_data = pd.read_csv('/Users/trevorhegarty/my_code/finance/fin_capstone/gold_prices.csv')
google_data = pd.read_csv('/Users/trevorhegarty/my_code/finance/fin_capstone/google_stock.csv')
bit_data = pd.read_csv('/Users/trevorhegarty/my_code/finance/fin_capstone/bit_data.csv')
standars_poor_data = web.DataReader('S$P', 'tiingo', start, end)

`

Here is the response

" ValueError: The tiingo API key must be provided either through the api_key variable or through the environmental variable TIINGO_API_KEY."

Many thanks

I don’t see where you provided the actual API Key.

Normally you’d import the requests library and include the following:

url= 'website address goes here'

query = {'apikey : ‘whatever your API key is from the site’, ‘whatever parameters’: ‘you’re trying to access’}`

and then:

requests.get(url, params = query)
response = requests.get(url, params = query)
response.ok

and then if the response is True go from there.

Hey!

I saved my APi key to a seperate file fin_data_config and imported it as tiingo_token. I just got it to work!

Another question, how do I access S&P 500 data from this? Once I have the correct ticker do I run the pandas datareader on the header??

headers = {
        'Content-Type': 'application/json',
        'Authorization' : 'Token {}'.format(tiingo_token)
        }
requestResponse = requests.get('https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1',
                                    headers=headers)
print(requestResponse.json())

standars_poor_data = web.DataReader('S$P', 'tiingo', start, end)

Thank you!

Ah, ha. Okay, now I get what you did. :slight_smile:
I haven’t done this lesson…But I did grab some data using an API key for another project.

BUT, I would think that it’s a json file, right? And then it’s imported or printed out as a dictionary. From there you can print the dictionary keys, values and items.(?)
Ex:

if(response.ok):
    data = response.json()

print(data)

and then, print out the keys, values and items.

Perhaps someone else can chime in here about this specific lesson? :slight_smile:

Yeah that does seem to be the case!

If it’s a dictionary it should be easily converted in to a data set no?

Yeah this is for the financial analytics with python and the data importation section did a good job of showing how to manipulate the data but not necessarily import it cleanly.

Thanks!