Build a Mulesoft API Step By Step — Part 1
Build a Mulesoft API Step By Step — Part 1
This blog will take guide you to build a Mulesoft API from scratch step by step. An end-to-end tutorial.
Join the DZone community and get the full member experience.Join For Free
Hi All, it has been sometimes I have not come up with any blog. Sorry about that. Today, I thought to write something for the beginners. So, let us get going. Now, MuleSoft claims itself to be the #1 integration and API platform. So, today I will write about a Step-By-Step guide to create a Mule API. This blog would be more for the beginners and those who would like to learn MuleSoft and will walk you through a step by step process of building an API using the various tools and applications provided by MuleSoft at your disposal. If you are already aware of some steps and want to skip some steps, feel free to do so.
I will build an API on a couple of tables in Maria DB. You can use any other backend of your wish with your changes.
So, without any further ado, let us get started with the API building.
- Access to the Anypoint Platform. This is also called Cloudhub. You can create your training account for 2 months. You can get the access here <https://anypoint.mulesoft.com/login/signup>
- Anypoint Studio. I am using the latest version of 7.5. Download it here <https://www.mulesoft.com/lp/dl/studio>
- Understanding of YAML is good but not necessary.
Step 1: Define the API Contract using RAML
With the API First building approach, the first step in building an API is to create the contract. So, we will take our first step in building the API contract using RAML. MuleSoft supports both RAML and OAS.
To do this we will go to the Anypoint platform and Sign in.
Next, we go to the menu on the top left and select the Design Center.
Click on Create New > Create API specification.
Give a Name and leave other settings as is. We will name this as Mule Demo Database API.
Click Create API Specification.
You will reach the API specification page. Now, this is where we will build our API.
Now we do not want to create one monolithic RAML and put everything in there. Instead, I will create some folders and link the various files from there. So, we will create some folder and change some names, and this is what your explorer should look like:
Now I would start writing my RAML and as an when I have to create any additional files, I will create them. To start with I will create some common traits. These are properties that I would like to put to every endpoint in my API.
Create a file called common-traits.YAML inside the common folder with the following lines
I will also create a documentation file inside the documentation folder. This should be a file in markdown language. For now, I will write the file as below
At this point, my API in the design would look something like this.
Next, let us create some example files. Let us have 2 endpoints. One called /job and the other called /employee. I will create 4 Json in the example folder 2 objects and 2 arrays for each type. I am putting the JSON below, but this is on you as to what data you want.
Now we would need to create the schema in RAML format. I will create 2 schema files, one for the employee and one for the job. They would like as below:
Finally coming back to my API, I will now create all the necessary endpoints.
In this case, I will have 4 endpoints, GET, and POST for each employee and job. You can add other endpoints like PUT, DELETE or PATCH, etc.
Once you are done with the API, it should look like this.
Now, you can immediately, click on the Mocking Service at the Right top corner and your services are up and running. This is what you can call from the Postman or any other Rest Client.
This completes our step 1 which would now take us to the second step of importing this API into our Studio and starting to write the interface.
Step 2: Import the API into Anypoint Studio
As for Step 2, We will switch on the Anypoint Studio and select a workspace where all our Mule Project will be stored.
Now select Create a Mule Project or click on File > New > Mule Project.
Give the project a name. Note here you cannot put a name with space. I will use mule-demo-database-app. Now in the API Implementation window, select Import From Design Center.
Click the browse button and a popup window will come asking for your credentials. Give the credentials which you used to login to Cloudhub. Note that you can deploy this application later into another Cloudhub or customer. This does not make a connection to the API, it simply pulls down the API to your application.
This should take you to a Browse Window where you should be able to see all APIs created in the design center of this account.
Click on the API which you created and click OK. If there is no error in your API you should see a Green Tick mark saying you are importing the following API.
Now click Finish and your API will be scaffolded, and the Skeleton Application will be automatically created for you.
This would now help you in the implementation of your API as per your need. If just at this point, you do run your application, you should be able to test your application with the dummy data.
Click on Run > Run > Run as Mule Application.
Your application will be deployed in the Mule runtime embedded in the Anypoint Studio and you can go to a browser and go to the endpoint http://localhost:8081/console to test your application. You should see a screen like this where you can TEST your application. Note, for now, you can use anything in client id and secret since the application does not verify the client authentication.
Now let us start building the API Implementation to execute the back-end code for this API.
Step 3: Build the API Implementation
In this step, we would create the Application Implementation. Before moving ahead, I like to create 3 files in the code. Go to the folder src/main/mule. Right-click > New > Mule Configuration File and type "global". Same way create a file for employee and job each. The global file will hold all global configurations whereas the job and employee should hold implementation for each of the individual endpoints. Move the Global Elements HTTP Listener Config and Router into the global.xml file.
After this, your workspace should look like this.
Now I will create the connectivity to my back end Maria DB using a database connector. The database here is in AWS so, it will be accessible from my computer as well as from the cloudhub. Note that if you have a local database, you may not be able to access it from the cloudhub.
Following are the content of each file
Go ahead and again run the application and you should be able to go to the console in http://localhost:8081/console and you should be able to test all the endpoints that we created.
Step 4: Deploy the API into Cloudhub and Test
As the last step, we will deploy this application into Cloudhub and run the endpoint. There are various ways to deploy the application into Cloudhub like using the maven or CI/CD pipeline. But we will simply deploy the app using Studio.
It should ask you a couple of information. For now, we will just put the application name with a suffix for the environment and deploy the app.
We can now go to the Cloudhub to check the status of our app. Once it is deployed it should look something like this:
Click on the app name and you should see various details. We need the domain name as mentioned below in blue.
Navigate to a web browser and use the endpoint HTTP://<domainname>/console. This should take you to the same Console as above and you will be able to test out the application.
With this, we come to the end of this blog. If you see this page, it says the application is not secure, this is because we have used HTTP and not HTTPS as our listener. In my next blog, I will write how to use the make the application secure and get more out of API Manager.
For more blogs like this and the application that I built, please visit https://www.wedointegration.com/post.php?posturl=build_a_mulesoft_api_step_by_step_part_1. Also, you can find the Github link there.
Published at DZone with permission of Anupam Chakraborty . See the original article here.
Opinions expressed by DZone contributors are their own.