How to create an API in 15 minutes with Nodejs and Loopback4

Here we go again

Hi vatos ! today i’am gonna talk to you a little bit about loopback4, a not so popular framework created with NodeJs (only 3k stars for the version 4) by IBM, who allow you to create your API for prototype within a few minutes.

Why ?

As i said previously, loopback4 will allow you to create a nodejs API in a few minutes, but how can he do this ? dont we have to use an agnostic framework like expressjs in order to build a real personnalised architecture instead ? An architecture who suits perfectly to my company, my process, my wife and my dog ?

No. Really, you dont have to do that.

If you’re trying to prototype an app, you’re trying to convince people to believe in your project.

No matter the complexity of it, the goal here is to make thing simple and clear, before building a solid and robust architecture.

Loopback4 is pretty cool because it combines the both advantages about API generator and solid architecture building. it provide a standard nodejs API pattern with all that you’ll can find in a classic developed way API.

Did you say generator ?

Exactly. Loopback4 is built in the way that you can think directly about the data that you want to treat. It’s built around Domain Driven Developement (Aka DDD) a pattern that focus on the business logic of your application.

Theres loopback 4 generator for :

Show me what you got !

Okay, now that your convinced, lets create our first API using loopback4. The first step about that is to generate your API. For this, you will need :

Then, you can go to your terminal and generate your api by typing in your console : lb4 app

Loopback 4 application generator

Loopback4 will ask you multiple informations about your project like the title, the description, and the default options that you will use for your project.

Once everything is okay in terms of generation, you can access your api by :

Then, you can go to your http://localhost:3000/

1. Generating our model

Loopback model generator

Lets be simple and efficiant, for this tutorial, we’re gonna treat cars, so we will generate a car model with multiple informations inside by typing lb4 model car in our console

Once you enter this command, loopback will ask you about the property that you want inside this model. When everything is okay, the file will be generated and available inside the model folder in your generated API.

Loopback generated model

2. Generating our datasource

Datasource is the data connector to your DB system, loopback provide also a generator who allow you to generate the link to this datasource to persist your data in your API.

You can generate a datasource using the command : lb4 datasource car

for this example, we will use the in-memory DB. in-memory DB will continue to persist data while the API is running in our server. For production usage, i would recommend to use a classic database connector like MongoDB or SQL database.

Loopback datasource generated

Once you enter this command, loopback will ask you about the property that you want inside this datasource. When everything is okay, the file will be generated and available inside the datasources folder in your generated API.

Generating our repository

We use the repository concept in LoopBack 4 to refer to an object with methods to perform local or remote operations. Such objects are resolved from bindings within a LoopBack 4 context.

To generate a service, you can use the followed command : lb4 repository car

loopback respository generator

Once everthing is generated, a new repository file will be generated into our reposiroty folder. This file will contain all the mapping relation between our car model generated previsously.

loopback generated repository

Generating our controllers

Last thing, in order to make all this previously generated component work with each other, you’ll need to create a controller. The controller is the orchestrator of your API call actions.

It will allow you to do operation related to the linked model / repository that you’ve generated before.

Loopback 4 generated controller allow you to have a fully CRUD REST file with all the classic operations that you’ll do on an application or a website such as GET, POST, PUT, PATCH, DELETE

loopback controller generator

Once its generated, you’ll see a new file created in the controller folder of your Loopback 4 API with all the actions listed before.

Controller genereted from both car model / car repository

Launch it !

You can now start your new fresh API by running a yarn / npm start and see whats happen.

If you go to your http://localhost:3000 you’ll see the default API page with two links (to openapi.json and generated documentation about your API)

Loopback api default page
Loopback genereted documentation

Test it !

If you want to test a request to the API, you should use http client in order to see whats really happen during the call. We are gonna use Postman for this (but other client will suits well too)

/POST a new car :

Loopback API get call using postman

/GET cars list :

Loopback API post call using postman

As you can see, everything seems to be runing. If we want to go further, we can also add a generated ID propertys instead of specifying it at each /post request.

Conclusion & usefull links

As you saw, Loopback 4 is a very nice framework with an incredibely complete generator in order to go straight to the point. In the next article, i’ll talk about my personal experience using it and how you can implement an authentication system on your API.

If you got questions, i got answers, feel free to contact me.

And if you want to implement it in your company, feel free also to contact us at this website : https://grow-your-business.fr/ (this is our agency)

You can also find the github source of this article here :

https://github.com/lulul62/medium-loopback4-article

Have a nice day ! :-)

CEO and technical architect at https://grow-your-business.fr my job is to develop and maintain various application in javascript and implement coding rules.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store