Exploring Mulesoft API Contracts and Rest endpoint and methods

Ashish Bania
4 min readJun 11, 2022

--

Credit: Unsplash

Last week, we spent some time creating a REST API to fetch random activities from Bored API. The project was a fun way to foray into the Anypoint platform. One might argue we jumped the gun and went straight to creating APIs.

Credit: Tenor

I decided to visit the API design first and then use the API spec file to create a Mule application. This is absolute magic as all we have to do is refer to the RAML file and the flow, REST API, and methods are all created OOTB. Lots to unpack here but let's go ahead with an actual project.

We will first use the Anypoint Platform to create an API spec. Head to the Design center and create the below API spec.

#%RAML 1.0
title: ecommerce-api
protocols:
- HTTPS
- HTTP
version: 1.0
traits:
errorResponse : !include /traits/errorTrait.raml
/orders:
get:
is:
- errorResponse
description: get account for customer
queryParameters:
customerId:
type: string
required: true
example:
"cust12345"
BuyerEmail:
type: string
required: false
example:
"abc@xyz.com"
CreatedAfter:
type: date-only
required: false
example:
'2022-06-10'
responses:
200:
body:
application/json:
example:
!include examples/orders.json
post:
is:
- errorResponse
description: "Create orders as per order Id"
body:
application/json:
responses:
204:
/{orderId}:
get:
is:
- errorResponse
description: "Fetch single order information for orderID"
responses:
200:
body:
application/json:
example:
!include examples/orders.json
delete:
is:
- errorResponse
description: "Delete order based on orderId"
responses:
204:
/shipment:
get:
is:
- errorResponse
description: "Get shipment data based on orderId"
responses:
200:
body:
application/json:
example:
!include examples/orders.json
post:
is:
- errorResponse
description: "Update the shipment status"
body:
application/json:
example:
!include examples/products.json
responses:
204:
/prices:
get:
is:
- errorResponse
description: "Fetch cost for the productID"
responses:
200:
body:
application/json:
example:
!include examples/prices.json
uses:
custType: customerLibrary.raml
/customers:
get:
is:
- errorResponse
description: "Fetch all customers"
queryParameters:
CustomerEmail:
type: string
required: false
example:
"abc@xyz.com"
CreatedAfter:
type: date-only
required: false
example:
'2022-06-10'
responses:
200:
body:
application/json:
type: custType.customerType
post:
is:
- errorResponse
description: "Create customer"
body:
application/json:
type: custType.customerType
responses:
204:
/{customerId}:
get:
is:
- errorResponse
description: "Fetch by customer id"
responses:
200:
body:
application/json:
type: custType.customerType
delete:
is:
- errorResponse
description: "delete customer based on customer id"
responses:
204:
/baskets:
get:
is:
- errorResponse
description:
post:
is:
- errorResponse
description: "Create baskets"
body:
application/json:
example:
!include examples/baskets.json
responses:
204:
delete:
is:
- errorResponse
description: "delete customer based on customer id"
responses:
204:

We can see the many resources and methods available now.

- errorResponsedescription: "delete customer based on customer id"responses:204:

We can see the many resources and methods available now.

Orders resource and associated operations
Customers and baskets resources and operations

The API designer allows you to mock the API endpoint. This is very useful if you want to see the expected output. The mocked endpoints can be shared with client developers. Developers consuming the resources have a fair idea of what to expect.

Mocking configuraiton
Try it feature on mocked API endpoints

We will publish the API spec to the exchange as we are done with the API design.

Publish to exchange
Credit: Tenor

The API design creation was fairly simple. The whole process took around 10–15 mins. This completes the process of API design. This spec can be used to create a local Mule application on your system.

--

--

No responses yet