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
Please enter at least three characters to search
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Trending

  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  1. DZone
  2. Data Engineering
  3. Databases
  4. Mule 4: Using API Notebooks

Mule 4: Using API Notebooks

This article explains how to use API Notebooks.

By 
Ruchi Saini user avatar
Ruchi Saini
DZone Core CORE ·
Dec. 01, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
12.4K Views

Join the DZone community and get the full member experience.

Join For Free

This article explains how to use API Notebooks.

Let’s first understand what is an API Notebook and how does it help?

API Notebook is a web-based tool that is helpful in creating Interactive Tutorial, Examples, and showing a specific scenario/use case.

In the API notebook, an API client is generated using RAML definition.API Notebook uses the base URI from the RAML definition to generate the output.JavaScript is used in API Notebook to call the API resources and methods.

To understand how to write API Notebooks, lets take a use case.

Use Case

Head Office of an IT company MyInternetSolutions wants to interact with different LOBs (Line of Business) of its company to get the list of projects .Head Office would like to view the project details like - the headcount on the project, cost of the project , revenue of the project and profit/loss of the project.

An Intranet web application will be created. This web application will be used to add the projects by the project managers and project details will be viewed by the higher management /Head Office of MyInternetSolutions.

A Mule integration application will integrate with all the LOBs.

API Design 

To design an API for this use case, we will first identify the resources.

Resources as per our use case are – projects and project. The projects resource is a collection resource with resource path /projects and the project resource is a nested resource with a resource path /projects/{projectId}.

For projects resource (/projects), we use the following methods:

GET – to get a list of all projects

POST – to create a new project

For project resource (/projects/{projectId}, we use following methods:

GET – to get a specific project using project id

DELETE – to delete a specific project using project id

PATCH – to update some details of a specific project using project id

A Simple RAML file for the use case is –

Two datatype fragments are used in above RAML – project and status

Publish the RAML Specification to Exchange

Publish this API to exchange.

Initially, the API version is v1 with asset version 1.0.0.

If the API is created in Anypoint designer, click on Publish button for publishing API to exchange:


Creating an API Notebook

Once we publish the API to Exchange, we go to the Exchange Page of our API. Here we click on Edit.


Click on Add new Page - Specify the Page Name as API Notebook (any name would do)

Click the API Notebook icon and a global method API.createClient is generated automatically. This method is used to create an API Client in the Notebook. Below screenshot shows that an API client with the name “client” is created for projectsapi - RAML API Specification(version 1.0.0). 

The client name will become a global variable with the client object.

The path segments of an API resource, separated by slashes, become nested JavaScript objects. So projects Resource with resource path - /projects is now a projects nested object and it will be used as {clientName}.projects and we can call methods like get, put, post, delete, etc. on these objects.

To get a specific project using project id , i.e. project resource with resource path /projects/{projectId} , we will use:

JavaScript
 




xxxxxxxxxx
1


 
1
client.projects.projectId('proj1').get();



To delete a specific project using project id:

JavaScript
 




xxxxxxxxxx
1


 
1
client.projects.projectId(‘proj1’).delete();



Request parameters and headers can also be passed while calling methods and the response consists of Response Body, status, and headers.

Anatomy of API Notebook

A notebook consists of a text cell and a code cell.

Text Cell — The text cell accepts markdown. Use the text cell to write some documentation

Code Cell — Use the API Notebook icon to create a code cell and write the JavaScript code in it.

API Notebooks for Different Scenarios

Lets create API Notebooks for different scenarios of our use case -

Scenario 1 - Get a list of all projects


Create a new API Notebook.

In this API Notebook,when we click on API Notebook icon for the first time then there is an automatic generation of API.createClient. The client name can be changed and here the name is changed to "projectClient".

Add some documentation for example - Get a list of all projects (as shown in above notebook).

Click on API Notebook Icon again and this time it will create an Empty code cell. Add the java script code which will use the projectClient to access projects object and calls get() method over it.

Once API Notebook is created, save as draft :-


You will be forwarded to another page .


Click on Play Notebook to play this and see the results –


In the above screenshot, the response object is returned consisting of body, status, and headers. The body has a JSON Array, status is returned as 200, and the headers are returned as an object with all the response headers. The body, status, and headers can be accessed which we will see in the upcoming examples.

Scenario 2: Get a List of Projects for a Specific LOB 

Create a new API Notebook with name - GetProjectsOfLOB.

Create API Client using API Notebook Icon and then use this client to get list of projects by passing the LOB query parameter.

Save the draft and click on Play Notebook to test this notebook.

Scenario 3: Get Profit or Loss of the Project in the Company


Create a new API Notebook with name - GetProfitLoss.

First, API.createClient is used to create the client

Next, we use this API client to access the objects that represents /projects/{projectId}. We use the objects to get a specific project by passing the project id. The response is stored in a variable with the name myproject.

This variable "myproject" is used to calculate the Profit Or Loss by subtracting the Actual Cost from Actual Revenue. Actual Cost and Actual Revenue are accessed from the response body i.e. {variableName}.body.propertyName.

Scenario 4: Add a New Project and Get the Location of New Project

Create a new API Notebook with name - AddProject.

Again first, API.createClient is used to create the client. Using this client, we access the projects object and use the post method on projects object. The request body is passed as the argument of the post method and the response is stored in the variable newProject.

Using the response stored in the newProject variable, we access the location header.

Publish API Notebook

Once you are satisfied with your notebook, publish it.

My Udemy Course on Mule4: API Design using RAML 1.0

You can go through my Udemy course — Course Link.

This course covers — Design (Identify resources, methods, query params, headers, data types, examples), Reusability with ResourceTypes and Traits, Modularization with Library, Documentation, Testing, Publishing to exchange/portal, Creating API Notebook, and Versioning APIs.

Hope this article helps you!

API

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!