What is CRUD? CRUD Operations in APIs
4 min read
90% of software that you use every day are what we call CRUD applications. Most of the time we are using them without even realising it.
Most functions in software can be broken down into these 4 operations. Once you have the CRUD operations in your head, it can really help you to design your own software.
What does the acronym CRUD stand for?
CRUD stands for Create, Read, Update and Delete.
It corresponds to the operations that you do to a record on a database.
If you are using an application that stores some information and allows you to read and update it, then the chances are that it is following the CRUD model.
It is actually very hard to think of an application that doesn’t use CRUD.
Twitter - Tweets are created, read by others, edited (if you have Twitter Blue) and can be deleted when you accidentally said something stupid, and you hope no-one will notice.
Microsoft Word - You create documents, read them, edit them and delete them.
Instagram is the same as Twitter, but with pictures.
Amazon - When you buy something, you are adding the item to your shopping cart (create). You can see what is in your cart (read). You can change the quantity (update) and you can delete it.
How to use CRUD in Programming?
So now you know what CRUD stands for, but how do we actually use it in programming?
When you store data anywhere, then the chances are that you are going to be using the CRUD model.
If you are using SQL for example, the following operations match to the CRUD operations:
It isn’t just SQL that follows the CRUD model, so do most key-value based databases such DynamoDB which has:
If you are dealing with any data, then it is good to think about how you can model it with CRUD operations.
What is CRUD in an API?
It is all very good storing your data in a database, but you need to be able to access it somehow. Most web applications are going to use APIs to do this.
APIs also follow the CRUD model, especially if you are creating a RESTful API.
When people are talking about REST APIs you will hear the term “resources” which is really just a fancy word for the data that you are storing.
When you are building a REST API, you build out an endpoint for each resource.
If you are building a shopping website, for example, your resources will likely be:
In the relational database model, each of these resources is going to be represented by a table in your database. Therefore, you are going to want to Create, Read, Update and Delete from each of them.
To do this, we create an endpoint for each operation for every resource.
For users, we might have a
/users endpoint. Then we have the following endpoints that make use of the HTTP verbs.
POST- Used to create users, usually by sending a JSON body in the request.
GET- Used to retrieve the users. You will usually have 2 endpoints, one to get all users
/usersand another to get just 1 user
PUTis used to update a whole record, whereas
PATCHwill just update a few of the fields but leave the rest unchanged. Most applications just use
DELETE- Used to delete the user. In a lot of applications, we never actually delete records, as it would break the references to other records. Usually we just add a
IsDeletedfield to the record and set it to
truewhen we want to delete a record.
These endpoints are then used by the frontend, whether it be a web frontend or a mobile application.
If you are struggling to work out how to build an application from scratch, then start with the different data sets in your application and create endpoints for each of them that the frontend can use.
CRUD is one of the programming mental models that is essential for creating software.