How to Build REST API Using Node Js Feathers Framework
Tags: webdev, beginners, javascript, node
Published: 2021-07-26 15:31:15

This article will create a simple REST API vehicle system with full authentication and CRUD (create, read, update, delete) capabilities.

We will be creating a simple vehicle API support.

**Our Use Cases:**

- As a user, I should be able to register.
- As a user, I should be able to login.
- As a user, I should be able to perform any CRUD operations on the vehicle data.
    - create
    - read
    - update
    - delete

**Project Setup**

```livescript
mkdir vehicle-api
cd vehicle-api
feathers g app
```

![running command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqc5o04kyyhchei30oy1.PNG)
 

As you're answering the above questions, you'll notice that you can create your application using TypeScript and not JavaScript, or we can select any other database type.

For simplicity, I went with yarn, JavaScript, and NeDB, but if you're planning to use npm, TypeScript, or Postgres/MongoDB, you can do so. 

Next, open your project with an editor of your choosing (I'm personally going with VS Code)

![openProject](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0c9t6mllwzx30ipmx2d.PNG)
 

To run the application, run the appropriate package manager command that you've selected. In this case:

```livescript
yarn dev
```

and you should be able to see this page in your browser:

![running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eca8h3l4c4s5hzr9dteg.PNG)
 

**Congratulations!** 
You've set up your first Feathers application, and here is the tree structure of our project:

![tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvpabq9uqf8qt45qzvhi.PNG)
 

You can find more information how what do this files do in here: [https://docs.feathersjs.com/guides/basics/generator.html#the-generated-files](https://docs.feathersjs.com/guides/basics/generator.html#the-generated-files)

Notice how under 1 minute, we've generated an application with a full authentication implementation and our first user service has full CRUD capabilities.

So how do we communicate with our backend if we don't have a frontend?

Whenever we work on the API, engineers would use a tool called Insomnia or Postgres (in this example, I'll be using insomnia). Overall it will enable us to send and get a response from our API without running the frontend.

![insomnia-0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54s1xag9itjo6086fwki.PNG)
 

What we want to do next is to create "register" and "login" requests in our insomnia workspace.

In my workspace, I'll be creating a new folder, "Authentication," that will contain "register" and "login" requests.

**Register**

![register](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phkyv7w8m1os13r5o6c3.PNG)
 

After sending the request, we should see the following:

![register-complite](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yng5om0vzpnb75x429m.PNG) 

**Login**

![login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbwzd2m99gp8tp7hn87w.PNG)
 

After sending the request, we should see the following:

![login-final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7wbs4dw23ovhj51ntnd.PNG)
 

If you see the same thing, then your authentication is set up correctly!

Since the basics of our applications are set up and running, it's time to create our vehicle service.

To do that, run the following command and answer all of the questions in the same way as we did it right from the beginning:

```livescript
feathers g service
```

![vehicles-setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e3fg81njh8ui2rocsq2.PNG) 

And if you look under the services/model folder, we now have a vehicle implementation that we can perform entire CRUD operations on.

![vehivle-service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt81hehqecb2121plj2g.PNG) 

At this point, our REST API is fully ready to take in some data and perform the entire CRUD operation on our new vehicle service!

Let's create a few records to demonstrate that.

![create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbv6gwch73dmc9n884kd.PNG) 

Oh no, what happened here!? We're getting an error message "Not authenticated."

That's the expected behavior because our system is saying that "you're trying to access an endpoint when you are not authenticated." Translation, not logged in.

So let's send the request with the generated token from before and try to resent the request 

![token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksre4sv9ki6rln2zkjqn.PNG) 

![create-success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3c4sm8kadbv85k70bs2.PNG) 

At this point, we can now save a record into our DB. 

![data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe9qnp6s9sn02x6eo74c.PNG) 

Let's create few more records and get all vehicles:

![all](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wlz338c1eg90f4etqly.PNG) 

At this point, our Update and Delete will look very similar.

**Additional Recourses:**

If this was your first RESTful API build, unfortunately, I didn't explain models, why we named our folders "Authentication" or "vehicles" in our insomnia, and what some of the things are. Thankfully, I do have a video where I give more information for beginners as I'm building out the API:

[https://youtu.be/H0KhcB3D8aQ](https://youtu.be/H0KhcB3D8aQ)

Feathers Documentation:


[https://docs.feathersjs.com/guides/basics/generator.html](https://docs.feathersjs.com/guides/basics/generator.html)

**Conclusion** 

We just created our first RESTfull API using FeatherJs framework WooHoo! I hope this tutorial was helpful for some of you. We did many things, and it could be a lot at first and could be confusing at the beginning. But to be honest, once you master this, you will realize that we have created an amazing backend with many complex pieces, and we can create all of it unless 2 minutes. So big congrats to you on getting to the end of this tutorial!  

If you have any questions, feel free to leave a comment or you can find me on my YouTube channel DevPool

[YouTube - DevPool](youtube.com/channel/UC03vw5F2isFkbJhyEZU5bvg?sub_confirmation=1 "DevPool")