DEV Community

MendixWithMe
MendixWithMe

Posted on

How to consume a REST API | Mendix 10

API consumption is common in high-level or low-code programming. In this blog post, I will teach you how to Consume a REST API in Mendix!

Prerequisites to this tutorial

RapidAPI Website

Step by Step

1º Let's start by entering the website https://rapidapi.com/martin.svoboda/api/quotes15

Quotes API

2º Click on the button "Test Endpoint" and see the answer on the "Results" tab.

Image description

Image description

3º Click on the "Example Responses" tab and press the button "Copy"

Image description

Image description

4º In Studio, go to your module and right click on it. Select "Add other" and choose "JSON Structure". As a name write "JSON_Quotes".

Image description

Image description

Image description

5º Paste the JSON structure you copied before and click on "Refresh"

Image description

6º On the Structure, scroll to the right and change the "Custom Name" of the first object from "Root" to "Quote".

Image description

7º Go to your module, right click on it and select "Add other" then choose "Import Mapping". For the name write "IMP_Quotes"

Image description

8º To the schema source, select "JSON structure" and choose "JSON_Quotes"

Image description

9º Check the following attributes: "language_code", "content", "url" and "name"

Image description

10º Click in "Map automatically"

Image description

11º Create a blank new page with the name "Quotes_Overview"

Image description

12º Inside the page, add a data view and as a "data source", select Microflow. Create a new microflow with the following name: "DS_Quotes_GetData"

Image description

13º In the microflow, drag and drop a "REST Call service" action.

Image description

14º Double click on the action, press edit near the Location input. In "template" paste the url of the service. You can find it under the tab "Code Snippets" with the key "url".

Image description

Image description

15º Change to the "HTTP Headers" tab and press the button "New" under "Custom HTTP Headers" subtitle.

Image description

16º Create 2 HTTP Headers. 
First Header:
 - Key: X-RapidAPI-Host
 - Value: 'quotes15.p.rapidapi.com'
Second Header: 
 - Key: X-RapidAPI-Key
 - Value: 'Your API Key'

Image description

Image description

To get your API Key go to your profile by click on your avatar on the top right of the page -> Personal Billing -> My Apps (Left Side Menu) -> default-application -> Authorization. 
If you dont have any Authorization, create one by clicking on the button "Add authorization".

Image description

Image description

Image description

Image description

17º Implement an error handling by right clicking in the "Call REST Service" action and selecting "Set error handling". A popup will open and give 3 options to select. In our case, we are going to select "Custom without rollback".

Image description

Image description

18º Add an "end event" action below the "Call REST service" activity. Between both, add a "Show Message" action. Select as "Type" the value "Warning" and as template write "{1}" to be possible to define a parameter.
Click in new parameter and insert as a value "$lastestError/Message".

Image description

Image description

19º Return to the "Call REST service" activity, click in the tab "Response" and for the "Response handling" choose "Apply import mapping". Select the import mapping that we created before "IMP_Quotes" and store in a variable with the name "Quote"

Image description

20º Right click on the REST Service and select "Set $Quote as return value". Go to the "end event" below and set the value as "empty".

Image description

21º Give access permission to the microflow "DS_Quotes_GetData" and to the page "Quotes_Overview".

Image description

22º On the page "Quotes_Overview", add text inputs for "Quote" entity attributes

Image description

23º Go to the project navigation and add the "Quotes_Overview" page to the menu.

Image description

24º Inside the domain mode, double click on the "Quote" entity. Select access rules and create a new one. Choose the module role "User" and give "Read, Write" access to the attributes and permission to create and delete an object.

Image description

25º Run the project and test the REST call by clicking on the Quotes page.

Image description


This is the end of the tutorial.
I'm creating a mendix community on discord to make life easier for devs. You can ask questions, help other people and have access to all the tutorials.
If you want to be part of this community, you can do so through this invite.
Discord: https://discord.gg/YHre8dXz3q


From the writer
If you enjoyed this article you can find more on our Medium page. For great videos, you can visit our Youtube page.
Are you interested in getting more involved with our community? Join us in our Discord Community Channel.

Top comments (0)