RAML: A Comprehensive Guide to MuleSoft’s API Specification

Sri Uday Kumar Dhanala
5 min readSep 7, 2023

--

Break it down in simple terms

Imagine you have a big box of storybooks. Each book has a title, some characters, and a storyline. Now, you want to lend these books to your friends, but you need a way to keep track of which book is with whom, which books are available, and how long they’ve had the book.

So, you decide to make a catalog or a list of all your books. In this catalog, you write down the title of each book, a brief description, and maybe even a picture of the cover. This way, when a friend wants a book, they can look at the catalog first to see what’s available and what each story is about.

Now, in the world of computers, this catalog is like MuleSoft RAML. It’s a way of describing and listing all the “services” or “features” a computer system can offer, just like your catalog lists all the books you can lend out. RAML helps other computer systems (or your friends, in our example) know what’s available and how to ask for it, without having to dig through the whole box of books. It gives them a clear picture of what they can expect and how to interact with it.

Let’s start by understanding what we want to achieve with our API. Let’s say we want to create a simple Books API, which will support operations like:

  • Fetching a list of all books
  • Fetching details of a specific book by ID
  • Adding a new book

For our demonstration, we’ll be using RAML 1.0.

RAML File: books-api.raml

#%RAML 1.0
title: Book Store API
version: v1
baseUri: http://api.bookstore.com/v1

types:
Book:
type: object
properties:
id: string
title: string
author: string
publishedDate: date-only
isbn: string
summary: string
example: |
{
"id": "1",
"title": "Moby Dick",
"author": "Herman Melville",
"publishedDate": "1851-10-18",
"isbn": "978-1234567890",
"summary": "Story of a ship captain's quest for revenge."
}
schema: |
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {"type": "string"},
"title": {"type": "string"},
"author": {"type": "string"},
"publishedDate": {"type": "string", "format": "date"},
"isbn": {"type": "string"},
"summary": {"type": "string"}
},
"required": ["id", "title", "author", "publishedDate", "isbn", "summary"]
}

/books:
get:
description: Retrieve a list of books.
queryParameters:
author:
description: Filter books by author name.
type: string
publishedBefore:
description: Filter books published before a certain date.
type: date-only
responses:
200:
body:
application/json:
type: Book[]
example: |
[
{
"id": "1",
"title": "Moby Dick",
"author": "Herman Melville",
"publishedDate": "1851-10-18",
"isbn": "978-1234567890",
"summary": "Story of a ship captain's quest for revenge."
}
]
post:
description: Add a new book.
body:
application/json:
type: Book
example: |
{
"title": "Pride and Prejudice",
"author": "Jane Austen",
"publishedDate": "1813-01-28",
"isbn": "978-0987654321",
"summary": "A tale of love and manners set in early 19th century England."
}
responses:
201:
body:
application/json:
type: Book
example: |
{
"id": "2",
"title": "Pride and Prejudice",
"author": "Jane Austen",
"publishedDate": "1813-01-28",
"isbn": "978-0987654321",
"summary": "A tale of love and manners set in early 19th century England."
}

/{bookId}:
get:
description: Retrieve a specific book by ID.
responses:
200:
body:
application/json:
type: Book
example: |
{
"id": "1",
"title": "Moby Dick",
"author": "Herman Melville",
"publishedDate": "1851-10-18",
"isbn": "978-1234567890",
"summary": "Story of a ship captain's quest for revenge."
}
404:
description: Book not found.
delete:
description: Delete a specific book by ID.
responses:
204:
description: Book successfully deleted.
404:
description: Book not found.

Here’s a breakdown of the above RAML:

  1. Metadata: The RAML starts with metadata that defines the version of RAML, the title, version, and base URI of the API.
  2. Types: This section defines the data structures used in the API. In this case, we’ve defined a Book type.
  3. Resources: The /books and /books/{bookId} are resources. Each resource can have multiple HTTP methods (get, post, delete, etc.).
  4. Query Parameters: The get method under /books accepts query parameters to filter books by author and publishedBefore.
  5. Responses: Each HTTP method has potential responses. For example, the get method can return a 200 OK response with a list of books, and the post method can return a 201 Created response with the created book’s details.
  6. URI Parameters: The /books/{bookId} resource uses a URI parameter {bookId} to represent a specific book ID.

With this RAML definition, you’ve set the foundation for the API’s design, and you can proceed with implementation using a platform like MuleSoft.

MuleSoft’s RESTful API Modeling Language (RAML) is a YAML-based language for describing RESTful APIs. It provides a structured approach to design, build, and document APIs. Following best practices while using RAML ensures that your API design remains consistent, maintainable, and easily understandable. Here are some best practices to consider when working with RAML:

  1. Use Versioning: Always version your APIs. This helps consumers know which version they are working with and allows for backward compatibility. E.g., /v1/users
  2. Consistent Naming Conventions: Use consistent naming conventions for resource names, URLs, and query parameters. Use plural nouns for resource names: /orders, /users. Avoid verbs in resource names: Use /orders/{orderId} instead of /getOrder/{orderId}.
  3. Use HTTP Status Codes Properly: Make sure to return the appropriate HTTP status code for each operation to provide more context about the result.
  4. Use Descriptive Properties: Provide detailed descriptions for resources, methods, parameters, etc. This aids in self-documentation.
  5. Modularize Your RAML: Split your RAML into multiple files using !include for better organization and maintainability. For example, you can have separate files for schemas, traits, resource types, etc.
  6. Use Traits and Resource Types: Reuse common patterns across your API by defining traits (common characteristics of methods like pagination or searching) and resource types.
  7. Security Schemes: Clearly define and document your security schemes in your RAML, be it OAuth, Basic Auth, or any custom scheme.
  8. MIME Types: Clearly specify supported MIME types for your API using the mediaType property.
  9. Provide Examples: Include sample requests and responses. This aids in understanding and provides a mockup for consumers to test against.
  10. Error Handling: Define common error structures and use them consistently across your API. Make sure to provide meaningful error messages.
  11. Pagination: If your API returns a list of items, consider implementing pagination and clearly documenting how it works.
  12. Filtering, Sorting, and Searching: If applicable, provide and document mechanisms for filtering, sorting, and searching resources.
  13. Linking and Relationships: Use hypermedia as the engine of application state (HATEOAS) to guide users through the possible actions they can take on a resource.
  14. Document Query Parameters: Clearly document the purpose, type, and any constraints on query parameters.
  15. Versioning of RAML: Use a version control system like Git to manage and track changes in your RAML files.
  16. Continuous Validation: Integrate RAML validation into your CI/CD pipeline to ensure that any changes to the API specification are valid.
  17. Feedback Loop: Allow and encourage consumers of your API to provide feedback. This will help in refining and improving your API over time.

Remember, the primary goal of RAML is to provide a clear and understandable contract for your API. Following best practices ensures that the contract is well-defined and consistent, making it easier for both developers and consumers.

--

--

Sri Uday Kumar Dhanala

I write to educate, inspire, and connect with diverse readers. Dive into my articles to experience a blend of expertise, innovation, and the art of teaching.