Create API in Mulesoft
Mulesoft is an integration tool that is used to create APIs. Mulesoft recommends following API-led connectivity to create APIs. This connectivity consists of 3 layers which are described below
- System API: This layer connects to the applications like databases, and file systems for reading or writing data. The system layer can be reused across multiple integrations.
- Process API: The process layer contains the business logic where the requirements are specific to a business use case.
- Experience API. This experience layer is the presentation layer. The data returned from the process layer can be presented in different ways based on the consuming applications like mobile, desktop, etc.
In this article, we will see how to create a simple API in the Mulesoft Integration tool.
Creating API in Mulesoft Integration Tool
We will create a basic API to pass the payload and get the same payload as a response. Here, we will create the Integration application without using Maven.
- The first step is to download and configure the Anypoint studio on your PC. I will write another article on how to set up the studio. Once, everything is configured open the tool.
- Click on the File on the toolbar -> New -> Mule Project. Enter the Project Name and click on Finish. The studio will create a project with all the required folders, files, and jars.
- By default, the studio will open the project-name.xml file. If not you open it by double-clicking on the file in the Package explorer window.
- In the mule palette, please search for the listener, and drag and drop it on the Message Flow window. This will automatically create the flow with the HTTP listener component.
- Now configure the HTTP component. Double-click on the HTTP component, This will open the config window. Now click on the + symbol beside the connector configuration and click on Ok. In the Path text field, enter /test.
- In the mule palette, search for the transformation component. Drag and drop it beside the HTTP one. Now change the output type to application/json and remove the curly braces. Now in place of curly braces write payload.
- Drag and drop the logger component from the palette beside the transformation component. In the logger message enter "Log of request #[payload]".
The following image shows you an overview of the flow in Mule 4 any point studio.
Run the Mule 4 API
On the toolbar, click on the Run -> Run configurations -> General -> select this API and run it. This will deploy the API in your local machine.
Once the API is deployed, you can test the application through the postman client app.
Testing Mule API through PostMan
After deploying the API, you can test it through the postman tool. In the postman, enter localhost:8081/test in the address bar, change the method type to POST and enter the below request in the body of the payload
Hit Send button in the postman and get the same response as the request. The postman curl command is shared here for your reference
curl -X POST
-H "cache-control: no-cache"
-H "content-type: application/json"
-H "postman-token: 8c75bc25-a339-f225-29fc-9cbf24ea6637"
I hope, this article will help you in creating a basic mule application in any point studio, running it, and testing it using postman. In case you get any error during deployment, then you can contact me.
This API was created without using the maven. In real-time projects, all mule applications use Maven for project dependencies, building and deploying applications. We will cover maven in future articles. In my next article, I will show you how to use the transform component with multiple examples.