API Design and MongoDB – Part 1

Author: Jeetan Madaan

Preface

Let’s try to understand the API Design & Development Cycle using Anypoint Platform, and the target audience is developers & architects in the organization.

Developers & Architects in the mule ecosystem refers to the Mulesoft Training course “Mulesoft.U Development Fundamentals” and this article focuses on “how to approach” an API Design & Development Cycle. 

User Story

Create a Restful API that uses APIKit and MongoDB connector to retrieve and create customer documents.

Design with API Designer

When we as a team gets the product specifications, there are design discussions, working sessions, and in that phase, one has to get hold of Design Center and start prototyping the API specs.

Resources: Entities modelled around business domains are mapped with URI paths in a RAML file. Let’s say we have to make system calls to retrieve & create customers, then we will be defining a resource mapped to a /customers URI.

Methods/Verbs: Restful APIs are modeled with Standard Http Verbs, so use GET to retrieve customers and POST to create customers. CRUD APIs should also support Put and Delete.

Types: RAML supports validating the request & responses by defining inline types & library types, and modeled API shows the inline type of Contact.

Query Params, Responses: Use query params to filter collection based resources and responses map to status code output in the desired mime / type with application/json usually the norm for Restful APIs. 

Connect with MongoDB

  1. Create an account on free-tier MongoDB Atlas Cloud using your email address and create a customers collection in CustomerDB.
  2. Create  a user as ‘testuser’ and allow it to read/write any database.
  • Install a mongo shell and connect to MongoDB with the connection string.

mongo mongodb+srv://username:password@clusterURL/customerDB

  • Create records in the customers collection with the insert command from mongo shell.

Develop with Anypoint Studio

There may be few ways to scaffold a mule application, but for API Development, an initial step is to import / or use the API RAML spec created in the design phase in a mule studio application.

RAML, though being a specification of the API,  has support for server side code generators in different languages like NodeJS, Java, Python, Mule.

Apikit: It is a declarative toolkit that parses the RAML API definition, and creates an HTTP Requester to expose the API Resources & APIKit Router that routes the request to auto-generated flow templates. It also creates Error Handling in the main flow that references exception types like  [BAD_REQUEST, NOT_FOUND, METHOD_NOT_ALLOWED, NOT_ACCEPTABLE ,UNSUPPORTED_MEDIA_TYPE, NOT_IMPLEMENTED].

Connectors: In an integration scenario, there is always a target system, and multiple enrichment components. Connectors are reusable extensions to mule runtime and imported as a maven dependency in the mule application. It enables you to integrate with third party APIs and databases. Connectors have operations, namespace & connection configuration based on the target application. 

Test with Rest Client

Initial Data Setup in Customer Collection has 3 documents.

Retrieve documents from customer collection with restful get call on customer prototype api

Create document in customer collection with restful post call on customer prototype api

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.