What is CRUD? CRUD Operations in APIs

What is CRUD? CRUD Operations in APIs

by | 3 min read
Published:
Updated:

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:

  • INSERT - Create
  • SELECT - Read
  • UPDATE - Update
  • DELETE - Delete

It isn’t just SQL that follows the CRUD model, so do most key-value based databases such DynamoDB which has:

  • PutItem — Create
  • GetItem — Read
  • UpdateItem — Update
  • DeleteItem — Delete

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.

🚀 Are you looking to level up your engineering career?

You might like my free weekly newsletter, The Curious Engineer, where I give career advice and tackle complex engineering topics.

📨 Don't miss out on this week's issue

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:

  • Users
  • Products
  • Reviews
  • Shopping basket
  • Orders

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 /users and another to get just 1 user /user/{id}.
  • PUT or PATCH - PUT is used to update a whole record, whereas PATCH will just update a few of the fields but leave the rest unchanged. Most applications just use PUT.
  • 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 IsDeleted field to the record and set it to true when 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.


🙏 Was this helpful? If you want to say thanks, I love coffee ☕️ , any support is appreciated.


ALSO ON ALEXHYETT.COM

Idempotency - What it is and How to Implement it

Idempotency - What it is and How to Implement it

  • 22 September 2023
When designing an API it is easy to think about the happy path of our applications but if you want to build a robust application and keep…
5 Design Patterns That Are ACTUALLY Used By Developers

5 Design Patterns That Are ACTUALLY Used By Developers

  • 08 September 2023
High-level programming languages have been around since the 1950s and since then programmers worldwide have been using code to solve all…
Domain-Driven Design: Simple Explanation

Domain-Driven Design: Simple Explanation

  • 28 April 2023
When you are trying to build complex software it is important that everyone is on the same page. Even though most of us prefer to work alone…
Monolithic vs Microservice Architecture - Which Should You Use?

Monolithic vs Microservice Architecture - Which Should You Use?

  • 17 March 2023
If you are starting to build a new application or you are working on an existing one you may be wondering whether you should be building a…
Hexagonal Architecture: What Is It and Why Do You Need It?

Hexagonal Architecture: What Is It and Why Do You Need It?

  • 17 February 2023
We all do our best to try and write clean code that is going to be easy to maintain in the future. As time goes on and the application gets…
Snake Case vs Camel Case vs Pascal Case vs Kebab Case

Snake Case vs Camel Case vs Pascal Case vs Kebab Case

  • 06 February 2023
Coming up with names for things when writing software is hard. You need to come up with a few words that neatly describe everything you have…
Why Developers Should Embrace No-Code Solutions

Why Developers Should Embrace No-Code Solutions

  • 09 January 2023
The best line of code is the one you didn’t have to write. As developers, we love writing code. At the end of the day, it is what we are…
Adding Inline Javascript to a React App

Adding Inline Javascript to a React App

  • 11 February 2022
Let me start this with a disclaimer, I am not a React expert and I am sure there are probably other ways to do this. If you know of a better…