Part 3: Extend your reach
In Part 3 of the tutorial you use a Robots script with the Python Requests library to connect to the HighBond API. You also connect to two different third-party APIs – one that returns the dates of public holidays, and another that returns currency exchange rates.
The Requests library is a Python component that allows you to use Python to send API requests and receive responses. You can use it to interact with any REST API.
Note
You must have created the robot in Part 1, and the script in Part 2, before you can do the initial portion of Part 3. The latter portion of Part 3 (using Python with a third-party API) does not have any prerequisites.
What will I learn?
You'll learn how to:
-
use the Python Requests library as an alternate way for making HighBond API requests
-
use the Python Requests library to interact with a third-party API and bring real-world data into a Robots script
Use the Python Requests library with the HighBond API
We'll begin by using the Python Requests library to replicate part of what you have already achieved using HCL HighBond API methods.
Generally, we recommend using HCL methods to interact with the HighBond API because the required code is simpler. We'll use the Requests library in this case for two reasons:
-
You can begin to learn about the Requests library in a now familiar context before moving on to using Requests with a third-party API.
-
If you're experienced with Python and Requests, you may prefer to continuing using a familiar approach.
HCL methods can only be used with the HighBond API. You must use Requests for connecting from a Python/HCL script to a third-party API.
Retrieve your HighBond API token and update the password variable
Sign in to Diligent One
If you are currently signed out, sign in to Diligent One.
- Go to www.highbond.com.
- Enter your Diligent One credentials (email and password).
- Click Sign in.
- If your company uses more than one instance in Diligent One, make sure the appropriate instance is active.
Retrieve your Diligent One API token
- To use an existing token, access the token you saved in a secure place for future use.
-
Optional. You can also create a new token if the token you previously used is no longer available. For more information, see Create a HighBond API token in Launchpad.
Update the password variable
-
Open the Robots app in a separate tab:
-
Click Switch Apps .
-
Right-click Robots and select Open link in new tab.
-
-
Switch to the browser tab where Robots is open.
-
Open the robot that you created in Part 1 of the tutorial.
-
In the upper-right corner of the robot, click Development to switch to development mode.
-
In the Script versions tab, select the most recent script version, and in the Version details panel click Edit script.
The Robots script editor begins the start-up process.
-
From the Robots script editor, click Manage variables .
-
In the Variables window, paste your HighBond API token into the Value field for the v_hb_token variable.
- Click Save and close.
Result The variable is updated and you are returned to the script editor.
Note
As a security measure, any time you exit or refresh the script editor your HighBond API token is automatically deleted from the password variable. You must re-paste a valid token into the variable each time you reopen or refresh the script editor if you want to run a script that uses the HighBond API.
If you forget this step, you get an error message when you run the script:
HTTPError: 401 Client Error: Unauthorized for url: <url>
Update the existing Robots script to use Requests
You don't need to change much in the existing script in order to use Requests. We'll continue to use HCL methods in the script cells that find or create a Results collection and an analysis. We'll use Requests methods in the cell that finds or creates a Results table.
In the script, make all the changes that are listed in the table below.
Note
Did you retrieve your HighBond API token and update the password variable?
If you get a Python error (a red error block) at any point, see Troubleshooting.
Cell # | Instructions |
---|---|
Cell 1 |
Import required components In cell 1, update the code to import two additional Python libraries:
|
Code block for cell 1
There is no visible output when you run this cell.
|
|
Cell 2 |
Define the variables used in the script
|
Cell 3 |
Find or create a collection in Results
|
Cell 4 |
Find or create an analysis in Results
|
Cell 5 |
Create a table in Results In cell 5, replace the code with the code block below. Two Python Requests methods make the API requests:
Read the comments in the code for a detailed explanation of how we're using the Python Requests library. |
Code block for cell 5
|
|
Cell 6 |
Perform some data analysis
|
Cell 7 |
Save the output of the data analysis to the Results table
|
Check Results for the new table
Let's check the Results app to confirm that using Python Requests to create a new table was successful.
-
Open the Results app in a separate tab:
-
Click Switch Apps .
-
Right-click Results and select Open link in new tab.
-
-
Switch to the browser tab where Results is open.
-
Open the collection that you created in Part 2 of the tutorial.
Result The table that you just created using the Requests library and the HighBond API is present, nested inside the analysis that you created in Part 2 of the tutorial. The table contains the output results that you just saved.
Note
Depending on the current usage levels of Results there may be a delay in the output results populating the table.
-
Enable Show table IDs, if it is not already enabled.
The table ID displays to the left of the table name. It should be the same ID that was created and displayed when you ran the script cell to create the table.
-
Click the table name to open the table.
The output results that you saved from Robots to Results are displayed.
-
Switch back to the browser tab where Robots is open.
Restore the previous table name
If you're still running the scheduled task that you created in Part 2 of the tutorial, you may want to restore the previous table name in the script.
In cell 2, line 16, update the name to Test table 2
, or whatever name you previously used:
v_table_name = f"Test table 2 ({current_month})"
Save the script and exit the script editor
-
In Robots, on the script editor tool bar, click Save and commit.
-
Enter a commit message such as Updated script to use Python Requests library.
-
Click Commit to save and commit the script.
The message Your script has been committed successfully appears.
-
On the page header, click the robot name (Tutorial robot <your initials>).
Result You are returned to the Script versions tab in the robot. Every time that you commit and save a script, the saved version is added to this tab.
Use the Python Requests library with a third-party API
Okay, you've seen the Python Requests library in action. You're now ready to take the final step in the tutorial: use Robots, Python, and Python Requests to interact with a third-party API and bring some real-world data into a Robots script.
The APIs we'll connect to
We'll use two different publicly available APIs to practice connecting:
-
Nager.Date – a repository containing the dates of public holidays, by year, in over 100 countries
-
Open Exchange Rates – a currency data API that provides exchange rates in USD for 170 currencies
Authentication
Like the HighBond API, most public APIs require that you authenticate yourself using credentials whenever you send a request to the API. Nager.Date does not require authentication, so we'll use that first as the simplest option. Open Exchange Rates does require authentication. The process to create a free account and acquire an authentication token is straightforward.
Create a new Robots script to use Requests
Let's create a new robot with a new script to contain your practice with Python Requests and third-party APIs.
Sign in to Diligent One
Note
If you're already signed in, and in the Robots app, skip to Create a new robot.
- Go to www.highbond.com.
- Enter your Diligent One credentials (email and password).
- Click Sign in.
- If your company uses more than one instance in Diligent One, make sure the appropriate instance is active.
- Open the Robots app.
Create a new robot
-
In the Dashboard in Robots, select HighBond Robots and click Create a HighBond robot.
-
Enter a name for the robot such as API practice robot <your initials> and click Create a HighBond robot.
Result The Robots script editor in the new robot begins the start-up process. You cannot run a script, or a script cell, until the start-up process is complete.
Connect to Nager.Date
We'll connect to the Nager.Date API and return the dates and other associated information for all the public holidays for a specified year and country. You sometimes need the dates of public holidays when performing analysis on transactional data.
Conversion to a Pandas dataframe
The data is returned in JSON format. To make the data easier to visually scan, we'll convert it from JSON to a Pandas dataframe. Be aware, however, that using the returned data in a script does not require converting it to a dataframe. Depending on the logic of a script, using a dataframe may or may not be the best approach.
Add code blocks to the new script
Copy and paste each code block in the table below into a separate cell in the script editor.
Note
If you get a Python error (a red error block) at any point, see Troubleshooting.
Cell # | Instructions |
---|---|
Cell 1 |
Import required components The code in cell 1 imports two Python libraries:
|
Code block for cell 1
Copy and paste this code block into cell 1 in the script editor, and then run the cell. There is no visible output when you run this cell.
|
|
Cell 2 |
Make an API request In cell 2, connect to the Nager.Date API and return public holidays for the specified year and country. |
Code block for cell 2
|
Example of the Nager.Date API response
If you didn't update the GET request, all the public holidays in the United States for 2021 are returned. In addition to the actual date on which the holiday falls, other useful information is returned, such as which jurisdictions observe holidays that are not observed in the entire country.
Learn more
Update the GET request (cell 2, line 4) to return public holidays for a different year or country. In addition to the current year, you can return data for either previous or future years.
For example, this GET request returns holidays in Brazil for 2020:
response = requests.get("https://date.nager.at/api/v3/publicholidays/2020/BR")
Tip
Consult the Nager.Date web site for the supported country codes.
Connect to Open Exchange Rates
Next we'll connect to the Open Exchange Rates API and return the latest exchange rates for all the currencies supported by the API. You may require exchange rates when performing analysis on financial data.
Open Exchange Rates returns point-in-time exchange rates, with the source updated hourly.
Conversion to a Pandas dataframe
The data is returned in JSON format. To make the data easier to visually scan, we'll convert it from JSON to a Pandas dataframe. Be aware, however, that using the returned data in a script does not require converting it to a dataframe. Depending on the logic of a script, using a dataframe may or may not be the best approach.
Create a free Open Exchange Rates account
First you need to create a free Open Exchange Rates account and retrieve your authentication token.
-
Go to the Open Exchange Rates sign up page for a free account:
-
Follow the on-screen instructions to create your account.
-
Once you have created your account, go to the App IDs page and copy your App ID (authentication token).
Create a password variable for the Open Exchange Rates token
Before you begin building the script, create a password variable for the Open Exchange Rates token.
-
From the Robots script editor, click Manage variables .
-
In the Variables window, specify the following values to create the password variable:
Field/option Value Name Specify
v_oxr_token
.Unlike
v_hb_token
, you can specify any variable name that you want. However, you need to specifyv_oxr_token
to work with the supplied code blocks in the tutorial.Type Select Password.
Value Paste your Open Exchange Rates token from the clipboard into the field.
Task input Automatically enabled when you select Password. Task input label Specify Enter your Open Exchange Rates token:
User input required Leave selected. Task input description Optional. You can leave the description blank. - Click Save and close.
Result The variable is saved and you are returned to the script editor.
Note
As a security measure, any time you exit or refresh the script editor the token is automatically deleted from the variable. You must re-paste a valid token into the variable each time you reopen or refresh the script editor if you want to run a script that uses the variable.
If you forget this step, you get an error message when you run the script.
Add code blocks to the existing script
Copy and paste each code block in the table below into a separate cell in the script editor.
Note
If you get a Python error (a red error block) at any point, see Troubleshooting.
Cell # | Instructions |
---|---|
Cell 3 |
Make an API request In cell 3, connect to the Open Exchange Rates API and return the most recent rates. |
Code block for cell 3
|
|
Cell 4 |
Assign returned information to variables In cell 4, assign specific rates to variables. You could then use the variables in subsequent data analysis that requires converting between currencies. |
Code block for cell 4
Example of cell output EXCHANGE RATES (2021-07-23 20:00:00) |
Learn more
To return only a subset of the available exchange rates, add a query string parameter to the exchange rate GET request (cell 3, line 7):
&symbols=comma-separated list of ISO currency codes
For example, this GET request returns the exchange rates for only five currencies: Chinese Yuan, Euro, British pound, Mexican peso, and US dollar:
response = requests.get(f"https://openexchangerates.org/api/latest.json?app_id={oxrtoken}&symbols='CNY','EUR','GBP','MXN','USD'")
Example of the Open Exchange Rates API response
If you used the currency codes in the example above, you now get a dataframe that contains only the exchange rates you specified.
Save the script and exit the script editor
Save your final tutorial work and exit the script.
-
In Robots, on the script editor tool bar, click Save and commit.
-
Enter a commit message such as Created script to use Python Requests library with third-party APIs.
-
Click Commit to save and commit the script.
The message Your script has been committed successfully appears.
-
On the page header, click the robot name (API practice robot <your initials>).
Result You are returned to the Script versions tab in the robot. Every time that you commit and save a script, the saved version is added to this tab.
What you learned
Congratulations! You've used native scripting in Robots with the Python Requests library to extend your reach beyond Diligent One.
Specifically, you have learned how to:
-
use the Python Requests library as an alternate way for making HighBond API requests
-
use the Python Requests library to interact with a third-party API and bring real-world data into a Robots script
What's next?
Venture out into the world
Venture out into the world of publicly available APIs. You now know the essential process for connecting from Robots to a REST API. There are thousands of APIs out there and some of them are likely to interest you, or contain information that can help move your automation goals forward. Many APIs are fee-based, but many also offer a free option with reduced functionality so that you can try them.
Tip
Download an API client, such as Postman, or Insomnia, so that you can test API requests and responses independent of Diligent One. Postman includes a useful feature for auto-generating the Python code for a particular request.
Set up dynamic reporting
Some of your organization's enterprise systems may also have APIs that allow you to pull data for purposes such as reporting. The techniques you've learned in this tutorial provide a good starting point for setting up an automated reporting pipeline – one that runs from an enterprise system through an API to Robots, Results, and Storyboards.
Work collaboratively
If you don't currently have coding skills there may be others in your organization who do, and are willing to help. IT personnel and system administrators can often assist with the process of authenticating against enterprise systems. Build relationships with experts, and learn as you go.
Learn more
Learn more about HCL and scripting in Robots:
Troubleshooting
If you get a Python error (a red error block) when running a cell or the entire script, perform the troubleshooting procedure below.
-
In the script editor, click Manage variables and double-check the following:
-
Use the Python Requests library with the HighBond API
Check that the password variable v_hb_token exists and that you have supplied a token in the Value field.
The token must be a HighBond API token, not an Analytics token. To make sure, re-copy an appropriate token from the Manage API tokens screen in Launchpad and paste it into the Value field.
-
Use the Python Requests library with a third-party API
Check that the password variable v_oxr_token exists and that you have supplied a token in the Value field.
The token must be a valid Open Exchange Rates App ID (authentication token). To make sure, re-copy an appropriate token from the Open Exchange Rates App IDs page and paste it into the Value field.
Tip
Remember, if you exit or refresh the script editor, the value of any password variable is automatically deleted and must be re-specified.
-
-
If a password variable is completely absent, complete all required aspects of the password variable definition.
For more information, see:
- Click Save and close.
Result All variables are saved and you are returned to the script editor.
-
On the script editor toolbar, click Run all cells sequentially .
Result If no errors remain, the script runs from the first cell to the last.
Note
When you save and close the Variables window, the script editor session memory is cleared. Re-running all the cells sequentially ensures that all required Python libraries are imported, and that all prerequisite operations required by subsequent cells are performed.