MuleSoft reusable components, auto-generated documents and endpoint details in 10 minutes

Reading Time: 4 minutes

Create custom traits, responses, resource types, and libraries so that API-led connectivity has a consistent format, response, error type, etc.

Below are steps which will help to generate this custom framework:

  1. Traits (Headers, Responses)
  2. Responses (Specified HTTP status like 200, 201, 400, 500, etc.)
  3. Resource Types (Specific to HTTP methods like GET, POST / GET, PUT and DELETE / POST (with search criteria) and Publish as Custom Library (this can be reused in specific API ex. xAPI, pAPI or sAPI)
  4. Create specific APIs and reuse the library created in step 3.

1: Create Traits, Headers, Error Type

Create Traits for specific organisation-level headers, error types, etc.

  • apiHeaders (Org. level pre-defined headers ex: x-trace-id)
  • Error (Error Object/Data Type, which will be defined at Org. level)

Error Code Specific Response Traits

hasBadRequest (400), hasInternalProblem (500), hasNotFound (404), hasRequestItem (Input Request for single Object), hasResponseItem (Output response for single object), hasResponseCollection (Output response for multiple objects/ array of objects).

apiHeaders.raml (Trait)
error.raml (Trait)
hasBadRequest.raml (Trait)
hasRequestItem.raml (Trait) — Request Object
hasResponseItem.raml (Trait) — Response Object
hasResponseCollection.raml (Trait) — Response Object (with an Array)

Similar to these, we have other Traits like hasNotFound.raml (Code: 404), 409, etc., as needed.

2: Create responses against specific HTTP status codes

We do have an example for each specific Error Code: 

ex: 400 (ErrorType400.json), similar to this, we can generate 401, 500 related json files for respective error codes.

Also, Error Code has specific patterns (like 3 characters of API name and 5 digits error code — Org. level Error Code Standard.)

ErrorType400.json
ErrorType401.json
ErrorType403.json
ErrorType500.json
ErrorType405.json

3: Custom Library with Resource Types, importing traits and examples of HTTP status codes.

Create a custom library and publish it to Anypoint Exchange as a reusable component.

Import Traits and DataType
GET (get all objects) and POST (create a new object)
GET Method for the Collection object
POST operation for custom search details
collection-item (with GET, PUT and DELETE operations)

4: Create API and import library for reusability

Create an example-demo-api, which has all resource types and a couple of endpoints that have specific custom headers, query parameters and responses.

Import Library, data types and inline Search Object for Fruit Object with specific identifier and search condition
Search with specific requests and responses, collection only for GET.
Default Implementation of GET,POST and GET,PUT and DELETE
Resource with appending custom headers, query parameters and new response 401

Documentation (Auto Generated)

API Documentation for created API
Method-level description is being generated automatically (Ex: Search details for <<Specific Object>>)
GET /students/{id} contains custom header (h1) and query parameter (q1), Not highlighted response)
PUT /students/{id} contains HTTP status code 403 additional (default codes are 200, 400, 404 and 500)

Conclusion

This will be useful when you have green field implementation or are re-designing your APIs to format standardised consistency, reduce efforts for API governance, quick implementation, and Demo/PoC for API-led connectivity on the MuleSoft Anypoint Platform.

Let’s get started.
Reach out to us
for a conversation!

Fill in the form below and we will get back to you