The haver library provides a convenient wrapper to Haver Analytics' RESTful API.
pip install haver
from haver import Haver
haver = Haver(api_key='<your-haver-API-token>')
The class Haver
also accepts keyword arguments to be passed to requests,
which handles the connection to the API under the hood. In this way,
by passing e.g. verify
and proxy
parameters, users can access Haver databases from behind firewalls.
For example:
haver = Haver(api_key='<your-haver-API-key>',
verify=False, # Or local path to certificates
proxies={'http': 'http://proxy-username:proxy-password@proxy-server.com:8080',
'https': 'http://proxy-username:proxy-password@proxy-server.com:8080'})
Instead of passing the API key explicitely each time,
the user can also set an environmental variable HAVER_TOKEN
containing the API key.
In this case connection will be as simple as
haver = Haver()
.
You can find your API key at this link.
All available databases can easily be listed as
haver.get_databases()
which will return a dictionary with keys the database names and values the corresponding database description:
{'UNPOP': 'U.N. Population Statistics',
'EPFRECA': 'Fund Country Allocations',
'EUFIN': 'Financial Data',
...
}
Further information on each dataset can be obtained via the method haver.database_info
,
and series within each database can be listed e.g. as
haver.get_series(database='UNPOP', full_info=True)
In order to retrieve data, the user has the option of querying one series at a time via the dedicated method
haver.read(database='EUDATA', series='N997CE')
which returns data in dictionary format, or querying multiple series as
haver.read_df(haver_codes=['N997CE@EUDATA','N025CE@EUDATA'])
where individual haver_codes
are created by joining series and database names as {series}@{database}
.
Luca Mingarelli, 2024