CRUD Methods in API integrations Clarified

16 August 2017 |

crud methods api integration

The product, customer, order and other data from web stores powers the functions of various B2B e-commerce software products. API integration is what enable such software to retrieve and manipulate these data. Typically, API allows performing CRUD operations, which are the following: create, read, update and delete. It is a bare minimum of options in most applications on the Internet. CRUD operations comprise fundamental methods used for data access and management via RESTful APIs. Basically, CRUD actions correspond with five major HTTP methods:

  • Create = POST
  • Read = GET
  • Update = PUT, PATCH
  • Delete = DELETE

However, these matches are not absolute and there are three methods that can be interchangeable in some cases and thus might cause confusion. These are POST, PUT, and PATCH. So let’s clarify the difference between them.

Creating a resource: POST vs PUT

Both verbs can be used for creating a resource. So what is the difference between them?

The POST option is typically used to create side resources. In that case, when you create a new resource, you POST it to the so-called “parent” resource. The server associates the new resource with “parent” resource and creates a URI for it itself.

If you want to create a resource via PUT, you need to provide a URI for it. PUT is more often used for updating. If you use PUT to the URI that represents the existing resource, it will just update it, if the URI you provided doesn’t exist, the server will create it.

Updating a resource: PUT vs PATCH

You can update the resource using either PUT or PATCH. So which one is better to use? The PUT method updates a resource by simply replacing it with a new version, while the body of the PATCH request contains instructions on how to modify a part of a resource. Also, PUT is idempotent (see next paragraph for the explanation), while PATCH is not. The latter has not yet gained wide adoption, even though PATCH is considered to be a RESTful method. Many popular web application frameworks don't support the PATCH method yet, and Ruby on Rails included PATCH only recently.

Idempotence and Safety: why do they matter?

Most HTTP methods have the property of “idempotence” and “safety”. Idempotent methods can be performed many times without different outcomes. Safe methods are those that do not modify resources. Let’s take PUT and POST as the example. The same PUT request with the same data can be sent multiple times and the result will be the same. It means that this method is idempotent. Multiple POST requests with the same data will create multiple new resources because POST is non-idempotent. CRUD includes only one safe method, GET.

As to the PATCH method, it is neither safe nor idempotent. However, it can be idempotent, depending on how the required modifications are communicated.

Defining the right verbs is an important part of designing a RESTful API or building a web application, that is why one need to take into account all methods’ specifics. Understanding them helps to design an API or an APP so that users will have an excellent experience with them.

In case you are a B2B e-commerce vendor planning on developing integration with shopping carts, consider integration via API2Cart. It offers a unified API that lets you integrate with many shopping carts at once. We also maintain all the connections, so you will not need to worry about further upgrades when new versions of e-commerce platforms come out.

If you have questions to ask, schedule a quick call with our representative or try out how API2Cart will work for your business.