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
- Have a RapidAPI account https://rapidapi.com/hub
Step by Step
1º Let's start by entering the website https://rapidapi.com/martin.svoboda/api/quotes15
2º Click on the button "Test Endpoint" and see the answer on the "Results" tab.
3º Click on the "Example Responses" tab and press the button "Copy"
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".
5º Paste the JSON structure you copied before and click on "Refresh"
6º On the Structure, scroll to the right and change the "Custom Name" of the first object from "Root" to "Quote".
7º Go to your module, right click on it and select "Add other" then choose "Import Mapping". For the name write "IMP_Quotes"
8º To the schema source, select "JSON structure" and choose "JSON_Quotes"
9º Check the following attributes: "language_code", "content", "url" and "name"
10º Click in "Map automatically"
11º Create a blank new page with the name "Quotes_Overview"
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"
13º In the microflow, drag and drop a "REST Call service" action.
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".
15º Change to the "HTTP Headers" tab and press the button "New" under "Custom HTTP Headers" subtitle.
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'
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".
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".
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".
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"
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".
21º Give access permission to the microflow "DS_Quotes_GetData" and to the page "Quotes_Overview".
22º On the page "Quotes_Overview", add text inputs for "Quote" entity attributes
23º Go to the project navigation and add the "Quotes_Overview" page to the menu.
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.
25º Run the project and test the REST call by clicking on the Quotes page.
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)