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

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:

  • Requests

  • JSON

Cell 2

Define the variables used in the script

  1. In cell 2, update Test table 2 (line 16) to Test table 3 :

    v_table_name = f"Test table 3 ({current_month})"
  2. Add two new lines after v_collection_id = None (line 23), and define new variables for your HighBond API URL and your org ID. Put each variable definition on a separate line.

    HighBond API URL

    v_api_url = "your HighBond API URL"

    Replace your HighBond API URL with your actual HighBond API URL. For example:

    v_api_url = "https://apis-us.highbond.com"

    Note

    HighBond API URLs differ by Diligent One region. To find the URL that matches your region, see API regions in the HighBond API Reference.

    Org ID

    v_org_id = "your org ID"

    Replace your org ID with your actual org ID. For example:

    v_org_id = "11594"

    Tip

    In the URL in the address bar at the top of the script editor, your actual org ID is the first number in the URL, after /orgs/ .

  3. Run the cell.

Cell 3

Find or create a collection in Results

  1. Do not change anything.

  2. Run the cell.

Cell 4

Find or create an analysis in Results

  1. Do not change anything.

  2. Run the cell.

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:

  • requests.get – gets a list of all tables in the analysis

  • requests.post – if the table name you specified does not exist in the analysis, creates a table with that name

Read the comments in the code for a detailed explanation of how we're using the Python Requests library.

Cell 6

Perform some data analysis

  1. Do not change anything.

  2. Run the cell.

Cell 7

Save the output of the data analysis to the Results table

  1. Do not change anything.

  2. Run the cell.

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.

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

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.

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:

  • Requests

  • Pandas

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.

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.

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.

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.

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.

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.

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

Troubleshooting

If you get a Python error (a red error block) when running a cell or the entire script, perform the troubleshooting procedure below.