How to consume a REST API | Mendix 10
Tags: webdev, mendix, restapi, api
Published: 2023-08-22 12:03:22

{% youtube xEYi-BN11Gg %}

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](https://rapidapi.com/hub)

![RapidAPI Website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s3zm8q7rka8edpxmx1g.png)

## Step by Step
1º Let's start by entering the website [https://rapidapi.com/martin.svoboda/api/quotes15](https://rapidapi.com/martin.svoboda/api/quotes15)

![Quotes API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pynj5mk246o6febgp63.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sadnwey1jc3j8a2igh2v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dbepn1f5oumm1tkyo1h.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifp97zwqeb8672yajlbh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0amtclcfesredtr135g.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbr1fb28udr48fpleq7x.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9shczpbr00bgtvs5hoyj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/im5dfn64zq3wl6l8j5m9.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk0x17te0n8bpt3iuiwt.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93fnh9283cvnnkzi9eps.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2l2n8rwucip3bfjfm626.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/df5ewdq4ib24nsrbk9lj.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgyaz4nuimppbsiz5voe.png)

10º Click in "Map automatically"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4frtjpqlqzeuxdbt5n4u.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkaccejv50w0snvphvsg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3i5af8v3sfbw20uxei6l.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42y4p72zrwxfe3j75mu4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7mninmcqayfy3uwej0l.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnfijci8hqi01bpmuqqd.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d9yq0i7plyj7ftdn47w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cefmx71358g40pz7t7p4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d56habfm0pk9ai0euegg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/020mu5d98e9dits809cn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkvid1qznhhfdnlz7fe4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agv60k6b7o3f5kphl8zk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zugaw5bhe7ua6ype4s8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgnbf2dh3zxx0yxt174y.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oft7duzulv110ox1mcs3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/supqbdvz31ebebtoaqnl.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6aijputfj5isacpz3wx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01e0xt577emmqnnsxe2y.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s369fk6e49mv0fx4gly1.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iarhvmerdk9a5ku29kjk.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9smc0jb5l01kmson8u3.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epo8e22llc94a9ajay5g.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7juu09k1j4r10caiu45n.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kltv5oosr1epk3gdlkzg.png)

---

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](https://discord.gg/YHre8dXz3q)

---

**_From the writer_**
If you enjoyed this article you can find more on our [Medium page](https://medium.com/r/?url=https%3A%2F%2Fmendixwithme.medium.com%2F). For great videos, you can visit our [Youtube page](https://medium.com/r/?url=https%3A%2F%2Fwww.youtube.com%2Fchannel%2FUCtRcKyh_4dvK-uCBtnbUWFw).
Are you interested in getting more involved with our community? Join us in our [Discord Community Channel](https://discord.gg/YHre8dXz3q).