v2 launch promo! Get a one-off 50% discount on PRO yearly using V2LAUNCHPROMO. Get started

Build A RESTful API With AdonisJS

Build A RESTful API With AdonisJS

1hr 25mins
19 lessons
Beginner
Start course

In this course, I will be showing you how to build a RESTful API with AdonisJS 5. If you are new to building APIs with AdonisJS, then this course is for you. Together, we will build an API for a forum. We are going to look at things like authentication, pagination, filtering and sorting. Along the line, we will cover some best practices such as structuring endpoints, using correct HTTP methods, using correct HTTP status codes as well as response formats.

Getting Started

7m 35s

Welcome to the build a RESTful API with AdonisJS course, where we will build an API for a forum. In this lesson, we introduce the course and go over what we'll be building in the course.

We start off the course by creating a new AdonisJS 5 API server.

In the last lesson, we created our API server. Now, let's start configuring the sever by setting up a database.

Forum threads will be categorized into categories, but to keep things simple and pretty straightforward we won't be adding implementation for creating categories, instead we will seed the database with some categories so we can have something to work with. In this lesson, we cover exactly how to go about that.

Authentication

Since REST APIs are stateless, we set up the API server to make use for API tokens for authentication.

In this lesson, we look at how to allow users to register on our API, and generate API token upon registering.

In the last lesson we allow users to register for an account and upon registering we generated an API token for the user. In this lesson, we are going to add the ability for returning users to login.

Threads

In this lesson, we move on to at the core of the forum API by looking how to create threads. Also, we cover associating users and categories to threads.

So we have created a thread, how do we retrieve it subsequently? Well, we are going to look at how to do just that in this lesson.

In the last lesson, we saw how to fetch a single thread. Now, let's take a look at how to fetch a list of threads.

Now, let's move on to updating threads. When it comes to updating records, there are typically 2 HTTP methods that are mostly used to handle that, which are PUT and PATCH. They both tend to do the same thing but in slightly different ways.

To complete the CRUD flow, let's implement deleting threads.

Now, let's tackle replying threads. There are two ways to go about this in terms of the database structure. In this lesson, I go over each of them briefly then end up going with one approach.

Pagination, filtering and sorting

Retrieving all records at once can have an effect on an API's performance especially when dealing with a large number of records. To mitigate that, we should only retrieve a specific chunk of records at a time and subsequently retrieve the rest. This lesson covers how to do that using pagination.

Let's make the API flexible by allowing users to be able to filter the API to some certain degree.

3m 38s

Let's take the flexibility a little further by allowing users to be able to sort threads based on certain criteria.

Refactoring

As it stands you will already notice some similarities in the routes pattern. In this lesson, we are going to refactor them to make use of resourceful routes.

As it stands, we have a potential loophole in the API. Anyone can update or even delete threads, which ought not to be. Users should only be able to update or delete their threads. Let's fix that in this lesson.

So far we've just been returning a resource or resources directly inside the controller. In this lesson, we are going to refactor that to make use of a much more descriptive response format.

Chimezie Enyinnaya

Hi, I'm Chimezie Enyinnaya, your teacher.

I’m a self-taught software developer based in Lagos, Nigeria, with over 8 years of experience. In the last couple of years, I've been doing more teaching because I enjoy sharing my knowledge so that others can benefit from it. Come along as I teach you all I know about AdonisJS, Node.js, and JavaScript at large!