FREE OutSystems Workshops - Build an OutSystems App in 1 hour | ENROLL NOW

Tech Bites - How to integrate with a REST API

< Back to Blog

In OutSystems, it's possible to integrate with a REST API in order to call an external service.

First, a quick recap: a REST API is a web service provided by an agnostic technology somewhere over the internet. A web service has methods, and methods can be called and can have inputs. A call to a method has a response, which varies depending on the method's type. 


For this scenario, we want to get the current weather data into an OutSystems application. We need to find an external service provider such as openweather.org to do that.

Here, it's required to have an account and generate an API key as a security measure to use the open weather services. 

We can now choose the service method that most fits our needs. In this case, the current weather service seems to do the trick.

So let's start!


In the Service Studio, go to the Logic tab, where you will find the Integrations folder. 

Right-click the REST option and select consume REST API.

There are two options to choose from: Add a single method or add multiple methods.  Since the latter needs a swagger file that we do not have or need at this point, let's use the first approach. 


We need to set the URL to call the service: let's get it from the openweather.org website.


Notice that some optional inputs can be used, such as the units. We will add it manually to the call, using a similar syntax as for the other inputs.

Once you paste it, notice the words between curly brackets: those are input variables of this service method.

To test this service method call, we use the city name and the API key as inputs. Let’s provide values for the inputs: Lisbon as the city name and the previously generated API key from the website.



Once you click the test button, the response will appear below, and you can check the OK status returned by the service.

So, it seems like the call was executed successfully.

Next, you can check the elements that are part of the response and will be used by the platform to generate the required structures holding the response at runtime. Copy this code to the response body of the configuration window so that the platform can do its magic. 

After you click that button, finish the process, and the service is ready to be used. 

Notice all the structures created to allow the service to provide and hold its response.


How to use it? Call it like any other OutSystems action. 

Let's go ahead and call this method in a previously created screen.

Once we drag and drop the REST API method and define its inputs, we display the attributes' values on the screen.

So in this data action, we will set the city name to Lisbon and paste the API key again. These are the values used at runtime. 

After the call to the GetWeather service, just set the required outputs to later use on the screen. In this case, we will display the icon and the temperature, which are the outputs being set.

Then, we can use the icon URL described in the openweather.org website documentation on the screen. 

Click the one-click publish button and open it in the browser. 

Now we can see information about the current weather that is coming from an external service. We are all set.



Turn Up Academy

Apply to our Turn Up Boot Camp

AND BECOME THE NEXT OUTSYSTEMS CERTIFIED DEVELOPER
(
Senior Instructor
)

Latest Articles

How to process data with Light BPT?

How to process data with Light BPT?

Synchronously uploading and processing data sometimes leads to a timeout. And, if not dealt with, timeouts can result in incomplete or duplicated data. We could solve this using different approaches, but let’s see how to use light BPT to process large amounts of data.

DevZone
Tech Bites - How to process data with Light BPT

How to process data with Light BPT

Synchronously uploading and processing data sometimes leads to a timeout, as we see in the following scenario.

Tech Bites
Tech Bites - How to quickly create elements using only drag and drop

How to quickly create elements using only drag and drop

Let’s go over a few tricks to create new elements and automatically relate them to other existing elements.

Tech Bites