Working with API
Learn how to work with API and how to use the BLS library.
We'll cover the following
Things we need
There are two and a half things we’ll need to access the API successfully:
- BLS endpoint addresses matching the specific data series you need
- Python code to launch the request
- A
BLS API key
to unlock higher request rates (in case we need them)
To work with Python, as shown in the figure, we need to access the API.
Getting the series endpoint addresses
For getting the series endpoint addresses, we may need to do some digging around in the BLS website, as shown in the figure. The following image shows what that looks like, including the endpoint codes - like LNS11000000
for the Civilian Labor Force set.
We can also search for data sets on this page. Searching for “computer,” for instance, will take us to a list that includes the deeply tempting “Average hourly wage for level 11 computer and mathematical occupations in Austin-Round Rock, TX.” The information shown when expanding that selection includes its series ID (endpoint) of WMU00124201020000001500000011
.
Looking at the data, it turns out that level 11 computer and mathematical professionals in Austin-Round Rock, Texas, could expect to earn $51.76/hour in 2019.
But, how do we turn series IDs into Python-friendly data? Well, that’s what we’ll learn next.
Using the BLS library
Getting the GET and PUT requests precisely correct can be complicated. But because I enjoy a simple life, let’s go with one of the available third-party Python libraries. The one we will be using is called bls
.
Using the command line, activate your BLS_API_Key
. You can register for the API here.
They’ll send you an email with your key and a validation URL that you’ll need to click.
Once you’ve got your key, you export it to your system environment. On Linux or macOS, that would mean running something like this where your key is substituted for the invalid one shown below:
export BLS_API_KEY=lk88af0f0d5fd1iw290s52a01b8q
What do we need to unlock higher request rates?