How to Implement APIs using Anypoint Code Builder

Sudheerkumar
Another Integration Blog

--

In the previous module Design the API, we learned how to design an API using Code Builder. In this module, we are going to learn how to implement an API using Code Builder.

What is Anypoint Code Builder?

Anypoint Code Builder is MuleSoft’s next generation Integrated Development Environment (IDE) that is part of the Anypoint Platform by MuleSoft. It allows developers to design, develop, and deploy APIs, integrations, and automations from a single environment.

How is Anypoint Code Builder useful for developers?

Using Anypoint Code Builder, developers can write, test, and deploy MuleSoft applications entirely in the cloud without the need for any local installations. The tool provides a visual interface for designing APIs and integrations and supports the creation of custom connectors, data transformations, and other components.

Anypoint Code Builder is designed to accelerate the development process by providing templates, pre-built connectors, and code snippets. It also provides collaboration features that allow multiple developers to work on the same project simultaneously, reducing the time and effort required to create complex integrations.

Features of Anypoint Code Builder

Anypoint Code Builder is used to design, develop and deploy APIs from a single environment.

  • Design APIs
  • Test and validate APIs using mocking service
  • Implement API logic from the specification
  • Graphical Flow Editor
  • DataWeave

Prerequisites

  • Create a test account on Anypoint Platform.

Installation:

Step-1:

To use Code Builder, first login to the Anypoint Platform. Click on Code Builder.

Step-2:

Accept the terms and conditions to get access.

Step-3:

Click Launch Web IDE.

Step-4:

Select the Implement an API to start our project.

Demo:

Now, Implement an API Using Anypoint Code Builder

Below are the steps we are using to implement an API in Anypoint Code Builder.

  1. Generate the flows
  2. Run the application
  3. Test the endpoints using Postman

Step-1: Implement an API to generate the flows.

Click on Implement An API.

Click Allow to sign in to the Anypoint Platform for the handshake.

Click Open and close the page.

Now enter the project name, project location, and asset name, and click on create project.

Now the project has been created.

The layout of the project looks like the one below, with two UI screens.

If we want to see a list of all the flows, click on the outline list.

testcodebuilder.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd ">
<http:listener-config name="testcodebuilder-httpListenerConfig">
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<apikit:config name="testcodebuilder-config" api="resource::ed9eae9b-4ffa-48df-be44-996c5c6953d2:TestCodeBuilder:2.0.1:raml:zip:testcodebuilder.raml" outboundHeadersMapName="outboundHeaders" httpStatusVarName="httpStatus" />
<flow name="testcodebuilder-main">
<http:listener config-ref="testcodebuilder-httpListenerConfig" path="/api/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:router config-ref="testcodebuilder-config" />
<error-handler>
<on-error-propagate type="APIKIT:BAD_REQUEST">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Bad request"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">400</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Method not allowed"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">405</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not acceptable"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">406</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unsupported media type"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">415</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not Implemented"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">501</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="testcodebuilder-console">
<http:listener config-ref="testcodebuilder-httpListenerConfig" path="/console/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:console config-ref="testcodebuilder-config" />
<error-handler>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="get:\Employees:testcodebuilder-config">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
[
{
EmpID: 1,
EmpName: "Sudheer",
EmpSalary: 12345,
EmpJoiningYear: 2023,
EmpDepartment: "IT"
},
{
EmpID: 2,
EmpName: "sai",
EmpSalary: 1234,
EmpJoiningYear: 2023,
EmpDepartment: "Network"
}
]]]></ee:set-payload>
</ee:message>
</ee:transform>

</flow>
<flow name="get:\Employees\(EmpID):testcodebuilder-config">
<ee:transform doc:name="Transform Message">
<ee:variables>
<ee:set-variable variableName="EmpID">attributes.uriParams.'EmpID'</ee:set-variable>
</ee:variables>
</ee:transform>
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
EmpID: 1,
EmpName: "Sudheer",
EmpSalary: 12345,
EmpJoiningYear: 2023,
EmpDepartment: "IT"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="post:\Employees:application\json:testcodebuilder-config">
<ee:transform doc:name="Transform Message">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
"output": "Employee has been added successfully"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>

Step-2: Run the application.

Now click on the Run and Debugging option to run the application.

The project has been deployed.

Once deployed, we will get a notification that the application is running on the available port.

Also, we can check the available port numbers under the ports.

Step-3: Test the endpoints using Postman.

Now it’s time to test the API using Postman.

Copy the address path under the ports to use for testing with Postman.

If we are getting this error message, we should add the cookies to the headers.

Check out the steps below to add cookies to the Postman headers.

Open Anypoint Code Builder in developer mode, then go to the application and copy the authentication proxy and signature cookies.

Now the header cookie should be like the one below.

Now that we have sent the request, we will receive the response successfully, as expected.

Conclusion:

In this article, we learned how to implement an API using Code Builder. We covered the following points.

  • How to generate the flows
  • How to run the application.
  • How to check the port and the address path
  • How to add the cookies

Thanks for reading my post and I hope it will be helpful.

-Sudheer Kumar N

--

--