How to consume a REST API | Mendix 10 (Banner Image)
How to consume a REST API | Mendix 10

How to consume a REST API | Mendix 10

Mendix With Me
Mendix Community
Published in
7 min readAug 22, 2023

--

Prerequisites to this tutorial

RapidAPI Website

Step by Step

1) Let’s start by navigating to rapidapi.com and creating an account:

2)After logging in, you will be directed to a free suggested API - the Quotes API, which is described as a “Free multilingual API for random famous quotes in many languages.”. Click on the button “Test Endpoint” and see the answer on the “Results” tab.

Quotes API
API Query Results

3) Click on the “Example Responses” tab and press the button “Copy

Example Responses tab
Copy JSON Structure

4) In Studio, go to your chosen module and right-click on it. Select “Add other” and choose “JSON Structure”. As a name write “JSON_Quotes”.

Right Click on the module
JSON Structure Button
JSON Structure Name

5) Paste the JSON structure you copied before and click on “Refresh

Paste the JSON Structure and click in “Refresh

6) On the Structure, scroll to the right and change the “Custom Name” of the first object from “Root” to “Quote”.

Write a Custom Name

7) Go to your module, right-click on it, select “Add other” then choose “Import Mapping”. For the name write “IMP_Quotes

Create an Import Mapping

8) To the schema source, select “JSON structure” and choose “JSON_Quotes

Schema Source as JSON Structure

9) Check the following attributes: “language_code”, “content”, “url” and “name”

Schema Elements

10) Click on “Map automatically

Map automatically button

11) Create a blank new page with the name “Quotes_Overview

New Blank page

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

Create a DS Microflow

13) In the microflow, drag and drop a “REST Call service” action.

REST Call service action

14) Double-click on the action, and 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”.

Service URL under Code Snippets tab
Location in Studio

15) Change to the “HTTP Headers” tab and press the button “New” under the “Custom HTTP Headers” subtitle.

HTTP Headers tab

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’
Headers Parameters to authorization
Create HTTP Headers in Studio

To get your API Key go to your profile by clicking on your avatar on the top right of the page -> Personal Billing -> My Apps (Left Side Menu) -> default-application -> Authorization.

If you don't have any Authorization, create one by clicking on the button “Add authorization”.

Avatar in RapidAPI
Personal Billing button
My app-side menu
Authorization area

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 will select “Custom without rollback”.

Right Click on the “Call REST service” action
Error Handling

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 a template write “{1}” to be possible to define a parameter.

Click on the new parameter and insert as a value “$lastestError/Message”.

Error handling path
Show Message popup

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 it in a variable with the name “Quote

Response properties

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”.

Return $Quote as return value

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

Entity Attributes on the page

23) Go to the project navigation and add the “Quotes_Overview” page to the menu.

Project Navigation

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.

Access Rules

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

Quotes Overview Page

This is the end of the tutorial.

I hope you enjoyed this tutorial on how to implement a REST API in Mendix 10. For more content like this make sure to check out my YouTube channel “Mendix With Me” for the video that inspired this blog and other tutorial videos.

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.

Read more

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 on our Discord Community Channel.

From the Publisher -

Inspired by this article to bring your ideas to life with Mendix? Sign up for a free account! You’ll get instant access to the Mendix Academy, where you can start building your skills.

For more articles like this one, visit our Medium page. And you can find a wealth of instructional videos on our community YouTube page.

Speaking of our community, join us in our Slack community channel. We’d love to hear your ideas and insights!

--

--

Mendix With Me

Ricardo Pereira: Certified Mendix Expert from Portugal 🇵🇹 | Founder of Mendix With Me community | Join us on Discord: discord.gg/YHre8dXz3q