How to add quandl Api key to my request in python

Qasim Imtiaz picture Qasim Imtiaz · Jul 24, 2017 · Viewed 7.9k times · Source

I had made a private quandl account and received my quandl Api key but how to add the key to my request in python?

My Code:

import pandas as pd 
import quandl 

df = quandl.get('EOD/V')
print(df.head())

Error:

Traceback (most recent call last):
  File "C:\Users\qasim\Documents\python_machine_learning\regression.py", line 4, in <module>
    df = quandl.get('EOD/V')
  File "C:\Python27\lib\site-packages\quandl\get.py", line 48, in get
    data = Dataset(dataset_args['code']).data(params=kwargs, handle_column_not_found=True)
  File "C:\Python27\lib\site-packages\quandl\model\dataset.py", line 47, in data
    return Data.all(**updated_options)
  File "C:\Python27\lib\site-packages\quandl\operations\list.py", line 14, in all
    r = Connection.request('get', path, **options)
  File "C:\Python27\lib\site-packages\quandl\connection.py", line 36, in request
    return cls.execute_request(http_verb, abs_url, **options)
  File "C:\Python27\lib\site-packages\quandl\connection.py", line 44, in execute_request
    cls.handle_api_error(response)
  File "C:\Python27\lib\site-packages\quandl\connection.py", line 85, in handle_api_error
    raise klass(message, resp.status_code, resp.text, resp.headers, code)
quandl.errors.quandl_error.ForbiddenError: (Status 403) (Quandl Error QEPx05) You have attempted to view a premium database in anonymous mode, i.e., without providing a Quandl key. Please register for a free Quandl account, and then include your API key with your requests.
[Finished in 6.0s]

Answer

Bertrand Martel picture Bertrand Martel · Jul 24, 2017

From the Configuration doc part, you can set it with ApiConfig.api_key :

import quandl
quandl.ApiConfig.api_key = 'tEsTkEy123456789'

Also, from the quandl python doc, you can define additional optional arguments in the get method :

:param str api_key: Downloads are limited to 50 unless api_key is specified

So you could also use :

df = quandl.get('EOD/V', api_key='tEsTkEy123456789')