What is CRUD? CRUD Operations in APIs

What is CRUD? CRUD Operations in APIs

by Alex Hyett | 3 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:

  • 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.

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 need more help, I also have a private Discord community for my fans where they can ask me questions. As a fan, your name is included at the end of my YouTube videos as well getting exclusive content and discounts to my future courses.
🌟 Find out more about becoming a Fan
Or you could buy me a coffee ☕️ to say thank you, any support is appreciated.


ALSO ON ALEXHYETT.COM

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…
Styling Components in React

Styling Components in React

  • 16 November 2020
As I mentioned in my previous posts there are quite a few ways to create components in React.js. As it happens there are also quite a few…
Different Ways to Create Components in React

Different Ways to Create Components in React

  • 10 November 2020
As they say there is more than one way to skin a cat. As it happens there is also more than one way to create a React component, which is…
14 SQL Query Performance Tuning Tips

14 SQL Query Performance Tuning Tips

  • 11 January 2017
I am going to start this post with a bit of a disclaimer. At heart, I am a .NET developer and not a SQL expert so if there is anything below…