DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • MuleSoft Integrate With ServiceNow
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • Exploring Hazelcast With Spring Boot
  • Design, Develop, Deploy and Manage a RESTful Application With MuleSoft

Trending

  • ChatGPT Code Smell [Comic]
  • Securing Cloud Storage Access: Approach to Limiting Document Access Attempts
  • Secure Your API With JWT: Kong OpenID Connect
  • Exploring the Frontiers of AI: The Emergence of LLM-4 Architectures
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Build RAML-Based API Specification Using MuleSoft Platform

Build RAML-Based API Specification Using MuleSoft Platform

RAML is a powerful tool used nowadays to create APIs. We can create our own RAML using the design center provided by the MuleSoft platform.

By 
Amrutha TESR user avatar
Amrutha TESR
·
Jun. 28, 23 · Tutorial
Like (3)
Save
Tweet
Share
2.7K Views

Join the DZone community and get the full member experience.

Join For Free

In earlier blogs, we saw many languages like Python, C++, Java, and so on that developers use to develop applications that we use to retrieve and insert data. In this blog, we shall learn about RAML which is a language used to create API specification.

Introduction to RAML

RAML stands for RESTful API Modelling Language. It is a YAML /JSON-based modeling language. The language is simple and structured, which is readable by both humans and computers. We use HTTP methods to call the resources while creating API specifications. 

RAML is very helpful as it allows the developers to define API endpoints, payloads, query parameters, HTTP methods, and responses to the request sent. These in-built features help the developers to design, and test APIs easily. MuleSoft is one such platform that provides anypoint platform, in which we can access the Design Center, wherein we could build APIs using RAML. 

Firstly, we should sign in/ sign up to Anypoint platform by MuleSoft. The account is accessed for a limited time period, say 1 month.

The Design Center in the platform looks like this:

Design Center in the platform

Now that we know the components of a design center, let us discuss the components in detail. 

  • File browser usually contains the documents, mostly RAML/JSON schemas and examples which are used during the construction of an API specification. The path of these files is copied and added to the resources using a valid syntax '!include'.
  • The Shelf contains in-built features like parameters, docs and HTTP methods, that are used while creating APIs. We can easily use these functions.
  • The Editor is the main area where the whole process of API creation takes place. We shall use the functions from the shelf accordingly.
  • API console helps us to test the API specifications using the mocking service. 

Getting Started With RAML

Let's now discuss how to create an API to retrieve and add data using an example of employees details in an IT company using RAML in the design center.

First, go to the design center in anypoint platform and create a new API specification with a proper name related to the API you are creating.

We need to create a separate RAML file in file browser that contains data types of the details we are going to access in the API. 

reate a separate RAML file in file browser that contains data types of the details we are going to access in the API

Now from the above example, we will create a datatype file for the details of the employees.

NOTE: Data types file is usually written in RAML so that we can give specifications.

JSON
 
#%RAML 1.0 datatype

properties:
  ID?:
    type: integer
  name:
    type: string
  city:
    type: string
  designation:
    type: string
  phonenumber:
    type: integer
  address:
    type: string


Attach the above data types file using types by copying the path of the file. 

NOTE: Use '!include' to attach the file.

DESIGN: Moving on, we can add the resources name with a forward slash(/) (for example: /employee). We can use the HTTP methods to request the response from the portal. If we want to retrieve data, we use the get method. Or else if we want to create data, we use the post method. Later, we can call functions appropriately from the shelf like adding query parameters to check the responses for that request. Enum is used for choosing between options. Responses are obtained using an HTTP method code 200 which is used to receive data and HTTP method code 201 used to add data to an API. In the end, add example files to the API for the display of details.

MOCKING: This mocking service is used to test the methods by sending a request. If the response is 200 ok, then the request is successful. The API specification is tested using this.

Mocking

NOTE: If the response is 4xx series then the request sent is an error. Please check the code.

Let's now check the example RAML code of employees in an IT company:

Example RAML

In the above RAML code, we are calling a resource employee under which we give a get method to retrieve the information of the employees working in that company. We are also calling the resource employee using UI parameter ID {id}. Under this resource we are giving two methods, get and post.

JSON
 
#%RAML 1.0
title: employees-sys-api

types:
  employeeData: !include/employee-datatypes/employee-datatype.raml

/employees:
  get:
    queryParameters:
      city:
        required: false
        enum:
          - LAX
          - TX
          - NYC
    responses:
      200:
        body:
          application/json:
            type: employeeData
            examples:
              output: !include /examples/employee-examples.json
      
  /employee/{id}:
      get:
        responses:
          200:
            body:
              application/json:
                type: employeeData
                examples:
                  output: !include /examples/employee-examplesNoID.json
            
      post:
        body:
          application/json:
            example: !include /examples/employee-examplesNoID.json
        responses:
          201:
            body:
              application/json:
                example:
                  message: Employee added but not really


As we discussed earlier, we can add examples for each HTTP request. A sample is shown below, follow that for better understanding.

NOTE: Use JSON language but not RAML for examples.

JSON
 
{
  "id": "345",
  "name": "john",
  "city": "LAX",
  "designation": "manager",
  "phonenumber": "8765987698",
  "address": "45-street,Califonia-54"
}


You have successfully designed your API specification using RAML!! You can now publish it in exchange which is a portal to display all APIs both privately and publicly. 

Conclusion

RAML is a powerful tool used nowadays to create APIs. We can create our own RAML using the design center provided by the MuleSoft platform. In my next blog, you will see how this API specification can be published in exchange so that the API is discoverable. Hope you like my blog. Do leave a comment about your views on this topic and share it. Happy knowledge.

Thank you.

API MuleSoft REST YAML

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Integrate With ServiceNow
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • Exploring Hazelcast With Spring Boot
  • Design, Develop, Deploy and Manage a RESTful Application With MuleSoft

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: