In this video, learn how to design the specifications from the data we have and the requirements that we want.
- [Instructor] Hello and welcome to section three, Building a RESTful API. In the previous section, we learned how to consume our RESTful API. In this section, we will build our own RESTful API to give clients access to our data. In particular, we will design the API specification and build it. We will perform acceptance tests and load tests to ensure that our API is working as expected. Finally, we will look at ways to maximize our server's utilization. Now we move on to the first video of this section that deals with designing the API Specifications.
Our goal for this video is to plan what an API should look like before we start coding. In this video, we are going to take a look at how to define the resources using the data we have. First, we will look at how the data is stored in the database. Then we will choose the representation that we want the clients to use. Finally, we will define the best resource that our API will support. Before we start building our RESTful API, we should know the database by heart. Whether we are building an API for an existing application or building one for a new application, the API will always sit on top of the data layer.
Our goal should be to let the API abstract the complexity of the data layer. Our simple application uses two collections, a friend's collection, and a note's collection. A Friend Document has an ID, a friend's Twitter ID, the user who has a person as a friend, the friend's name, location, and profile image URL. A Note Document has an ID, an owner ID, a friend ID, and content. Once we know our data, the next thing that we should think about is the API's representation of the data.
To come up with a good representation, we should ask ourselves a question, what do you want our users to know? The sample application requirements show us that our users should be able to get the notes for a particular user, insert or delete individual notes, and update the note. Our users should not be able to move notes to another user account, or move a note to another friend. Using these requirements, we come to the conclusion that our API does not need to expose a friend's collection since the data in that collection is exclusively Twitter data.
Also, as per the requirements, we do not need the owner ID and friend ID from the note's collection to be in their presentation. The owner_id doesn't need to be in their presentation because it can not be modified by the user. The friend ID doesn't need to be in their presentation because our app does not let users move notes to other friends. So our representation is a simple object with an ID and content. After deciding the representation, we have only one more thing to worry about, what will the API's resources be? Since notes are tied to friends, a good solution would be to let the notes resource be part of the friend resource.
So we define a collection resource with the data URL /friends/:friend_id/notes. We let the GET method return the notes for that friend, and the POST method insert an email to that friend. We also want to be able to update or delete individual notes, so we add an elementary source for the notes collection. We let the PUT method update the note, and the DELETE method delete the note. In this video, we have learned how to design API specifications by knowing the data, choosing the representation, and defining the resources.
In the next video, we will use Express.js to build the API that we just designed.
This Node.js course gives you an overview of a RESTful API and the logical steps of creating one. It explores three different APIs, focusing on their similarities and differences and how to effectively implement one. Instructor Saleh Hamadeh starts off by defining APIs, showing how they can be built on top of HTTP and listing the properties that make an API RESTful. Learn how to develop Twitter Notes, a sample web application that lets users leave notes for their Twitter friends. Use Twitter's API to implement a login flow and then design a web API. Additionally, get a closer look at several other real-world APIs, and learn some best practices to keep APIs secure, maintainable, and efficient.
- Identifying REST resources
- Setting up the development environment
- Consuming a RESTful API
- Creating an OAuth login request
- Getting an access token
- Saving data in MongoDB
- Building a RESTful API
- Testing user-perceived performance
- Looking at APIs in the real world
- Best practices for building RESTful APIs