Over a million developers have joined DZone.

Customization of REST APIs in LoopBack Applications

LoopBack is a great way to make REST APIs for custom purposes. Check out this overview of customized REST APIs made with LoopBack apps.

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

LoopBack creates REST APIs for custom models (business objects) that are persisted. While this is helpful to get started quickly (e.g. proof-of-concepts and first iterations), the final APIs often have to be customized. LoopBack provides various ways to achieve this.

When I looked briefly at LoopBack for the first time a couple of weeks ago, my initial concern was that I don’t want to expose the ‘schema’ of my databases as REST APIs via the out of the box CRUD operations. However since then I’ve learned that LoopBack offers several mechanisms to define REST APIs exactly as you want to expose them.

Only for built-in models, e.g. User, and the custom models that extend ‘PersistedModel’ REST APIs for CRUD operations are generated. When you choose the model ‘Model' as the base type, only the methods that you define and implement yourself (via remote methods) are exposed as REST APIs. This gives you maximal flexibility in terms of the design of your public interfaces. In the implementation of these methods, you can use the Node.js APIs of the persisted models to manipulate the data in the underlying databases.

{
  "name": "MyModel",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

In addition to this you can customize the generated APIs in many ways:

I’ve implemented a sample with an additional method in a persisted model. I wanted to use most of the out of the box REST APIs for CRUD operations but needed an additional method which returns not only one business object ‘ApprovalRequest’ but also the requester and approver ‘Person’ objects to minimize the amount of network requests between clients and the server.

Download the sample application from GitHub.

In approval-request.js the additional method is added, the actual implementation is done and the output model is defined. Here is a screenshot of the additional method in the API explorer.

Image title

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
loopback ,rest api

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}