Bonus Exercise 1: Integrating via APIs
Duration: 45 minutes
In this bonus exercise, we will use a real webservice API to update our list of airports which we previously imported via an excel spreadsheet. This will ensure that the locations all remain up to date with some additional data fields for tracking.
If you noticed in exercise 1, when we imported the Airports from the excel sheet, the Lat and Lon data is empty, we will go ahead and fix that.
Note: This session will require you to use a 3rd party API service to get data. Your personal data will be required to sign up to this service. If you are not comfortable with exposing this data, skip this exercise or let your instructor know.
Part 1: Create an Integration Action
Go to https://airlabs.co/ and click Sign Up
Fill in the relevant details to sign up for an account
Verify your account with the email sent to the email address sent to your email account
Sign in to your airlabs account on the next page
Click Copy next to API Key
Paste your copied API Key somewhere that you are able to retrieve later
Click Documentation, then Airports
Quickly read through this API documentation, notice that the API request has already been populated for you - We will rebuild this
Navigate back to your main ServiceNow interface, and search and click Action Designer under All
You are brough to the Flow Designer interface
Click the New button on the top right, then click Action
In the pop-up, enter Get Airport Details under Action name. For description, enter Calls the Airlabs API to retrieve airport details
Click Submit
Click Create Input
Under Label, enter IATA Code, then press enter
Click on the + icon in between Inputs and Error Evaluation on the left sidebar
In the pop-up box, scroll down and click REST
Change Connection to Define Connection Inline
Copy this URL and paste it under Base URL:
https://airlabs.co/api/v9
Enter /airports under Resource Path
Click on the + icon for Query Parameters
Enter api_key under Name, then paste your previously copied API Key under Value
- Click on the + icon for Query Parameters
Enter iata_code under Name, then drag and drop the IATA Code data pill on the right sidebar onto the Value field
Click on the + icon for Headers
Enter User-Agent under Name, and XXXXXX under Value
Your form should now look like this:
Click Save at the top right
Click Test at the top right
Enter SIN under the IATA Code field, then click Run Test
Click Your test has finished running. View the action execution details.
The new tab shows every step of the execution so far. Expand Steps and scroll down to the line that shows Response Body
Click on the corresponding value and copy the entire block of text
Navigate back the the main Action Get Aiport Details tab
Close the pop-up
Click on the + icon on the left sidebar after REST step
Scroll down and click JSON Parser
Drag and drop the Response Body data pill from the right sidebar onto the Source data field
Paste what you copied onto the main body
Toggle the Structured Payload View
Click Generate Target
The Target on the right should be generated
Here are all the steps in sequence:
Click Outputs on the left sidebar
Click Create Output
In the new row, change Label to Output, Name to output and Type to Array.Object
Click Exit Edit Mode
On the right sidebar, expand root under JSON Parser Step
Drag and drop the response data pill onto the Output Value field
Click Publish on the top right
Click Test
Enter BKK and run test, then view execution details
Ensure that the Output is defined, and when clicking on it you see a result similar to what is shown below
- You’ve completed integration! Now let’s use it in a workflow
Part 2: Using our integration action
Close all the pop-up boxes and click on the + tab
Click Flow
Under Flow name, enter Update airport information
Set Run As to System User
Click Submit
Click Add a trigger, then select Daily under date. This will run this workflow everyday
Click Done
Click Add an Action, Flow Logic, or Subflow
Click Action
Search and select Look Up Records - Pay special attention to selecting Look Up Records and not Look Up Record
Search and select Airport under Table
Click Done
Click Add an Action, Flow Logic, or Subflow
Click Flow Logic
Click For Each
Drag and drop the Airport Records data pill from the right sidebar onto the Items field
Click the + icon under the For Each Item in step
Click Action
Search and click Get Airport Details (this was the API integration action we just created)
Expand Airport Record on the left sidebar under the 2 - For Each section
Drag and drop the Code data pill onto the IATA Code field
Click Done
Add a new For Each Flow Logic under Get Airport Details
Drag and drop the Output data pill onto Items
Click the + under the new For Each Item in step
Click Action, then search and select Update Record
Drag and drop the Airport Record data pill under 2 - For Each onto the Record field
Click + Add field value
Search and select Lat, expand the response_object data pill under 4 - For Each
Drag and drop the lat data pill on the empty field
Click + Add field value
Search and select Lon, expand the response_object data pill under 4 - For Each
Drag and drop the lng data pill on the empty field
Refer to the full animation below:
Click Done
Click Activate on the top right
Click Test
Click Run Test
This will take a few minutes to run as we have 24 airports to update. Once done, click Your test has finished running. View the flow execution details.
Review the execution, take some time to expand each step to understand what has happened in this flow
Go back to App Engine Studio and preview the Airport table
This will open in a new tab, verify that all the Lat and Lon fields are now populated
This flow should now run everyday to update the latitude and longitude of each airport in your table. In a real life scenario, you could do so much more with the API, but this is just an example to show you the possibilites. For example, you could integrate with a complex booking API like Amadeus, and your users could search for flights and book flights and hotels directly from your custom application!