Building a CRUD API

By the Syncfusion DevTutorials team · Last updated

Build create, read, update and delete endpoints for the menu and orders, guard status changes with 409, and move logic into services.

CRUD stands for Create, Read, Update, and Delete.

CRUD represents the four most common actions that applications perform when working with data. Whether you are building a menu system, an online store, a contact list, or a banking application, you usually need to create new data, read existing data, update information, and delete data that is no longer needed.

In APIs, CRUD actions are usually mapped to HTTP methods. Clients send requests to create, view, modify, or remove resources, and the server performs the requested action and returns a response.

In this lesson, you will learn how to build all four CRUD operations for the Brew & Bytes menu and orders. By the end, you will understand the basic structure used by most real-world APIs.

Watch a Menu Change Live

The coffee shop adds a new item called Cold Coffee, changes its price, and removes Masala Chai.

Press Run and watch the five responses.

Program.cs — menu CRUD

  • POST /menu creates a new menu item called Cold Coffee. The response is 201 Created and includes a Location header.
  • GET /menu returns all three menu items.
  • PUT /menu/3 updates the price to ₹190.
  • DELETE /menu/2 removes Masala Chai. The response is 204 No Content.
  • The final GET /menu shows the updated menu.

In simple words: these five endpoints handle all the common actions for a resource. Most APIs follow this same pattern.

TL;DR

  • Create uses POST /things. The response is usually 201 Created with a Location header.
  • Read uses GET /things or GET /things/{id}. The response is usually 200 OK, or 404 Not Found if the item does not exist.
  • Update uses PUT for replacing a complete item or PATCH for changing part of an item. The response is usually 200 OK, or 404 Not Found if the item does not exist.
  • Delete uses DELETE /things/{id}. The response is usually 204 No Content, or 404 Not Found if the item does not exist.
  • Use MapGroup in C# or a sub-app in Hono to organize related endpoints.
  • Keep business rules, such as order status changes, on the server.
  • If a request breaks a rule, return 409 Conflict.
  • Move data and business logic into a service to keep endpoints simple.

A Real-World Example

Think about the contacts app on your phone.

You can:

  • Create a new contact.
  • Read a contact or view the contact list.
  • Update a phone number.
  • Delete an old contact.

These are the same four CRUD actions. Many applications work in exactly the same way, whether they manage contacts, products, orders, or customers.

CRUD: the four actions map to five endpoints on one resource, each with its usual status code.

What You Will Learn

In this lesson, you will learn:

  1. The five CRUD endpoints and the status codes they use.
  2. How to organize related endpoints under one path.
  3. How to create an order with multiple items and a server-calculated total.
  4. How to protect status changes using 409 Conflict.
  5. How to move business logic into a service class.

The Five Endpoints

MapGroup in C# and new Hono() plus app.route("/menu", menu) in TypeScript do the same job: they put related endpoints under one path. When the API grows, each resource gets its own group.

Orders with Multiple Items

In a real application, an order can contain several items. Each order line contains:

  • A menu item.
  • A quantity.

The server looks up the item prices, calculates the line totals, and then calculates the final order total.

Program.cs — orders with lines

For example:

  • Two lattes cost ₹440.
  • One muffin costs ₹150.

The total becomes ₹590. The client only sends the item names and quantities. The server calculates all prices and totals.

Protect the Order Status

An order moves through these stages:

  1. received
  2. preparing
  3. ready
  4. delivered

The order must move one step at a time. The client uses PATCH to change the status, and the server checks whether the change is allowed.

An order moves forward one step at a time. A request that skips a step gets 409 Conflict.

Program.cs — guard status changes

Examples:

  • received → preparing is allowed.
  • preparing → delivered skips the ready stage, so the server returns 409 Conflict.
  • preparing → ready is allowed.

The 409 Conflict response includes a message explaining the problem.

Never trust the client to follow the rules. If the app hides the "Delivered" button, someone can still send the PATCH directly. The rule must live in the server.

Services and Endpoints

As an API grows, Program.cs can become large and difficult to manage.

A common solution is to split the code into separate parts:

  • A service class contains the data and business rules, such as MenuService.
  • An endpoints class defines the API routes and calls the service, such as MenuEndpoints.
  • Program.cs connects everything together.

Program.cs — a service and an endpoints class

builder.Services.AddSingleton<MenuService>() registers the service.

This feature is called Dependency Injection.

ASP.NET Core creates one instance of the service and provides it automatically to endpoints that need it.

Use the file tabs to explore each part of the application.

Keep endpoints thin. An endpoint reads the request, calls a service, and picks a status code. The rules live in the service, where they are easy to test without HTTP.

The Whole App

The final example shows the complete flow:

  1. Read the menu.
  2. Create an order.
  3. Try creating an invalid order.
  4. Read the order.
  5. Delete the order.
  6. Try reading the deleted order.

Program.cs — the whole app

The invalid order uses a menu id that does not exist, so the API returns a validation error.

After the order is deleted, the final request returns 404 Not Found.

Take It Home

Download the finished API from the final example and run it on your own computer.

  • The C# version is an ASP.NET Core 10 project.
  • The TypeScript version is a Node.js project that uses Hono.
  • Both projects include a .http file with sample requests.

Brew & Bytes menu and orders API

Where You See This

CRUD is used almost everywhere:

  • Admin dashboards let users create, view, edit, and delete records.
  • Shopping carts add items, update quantities, and remove products.
  • Food delivery apps update order statuses and prevent invalid changes.

Common mistakes

SymptomCauseFix
Updating a missing item creates it or crashesThere is no 404 check in PUTLook the item up first, and return 404 if it is missing
Delivered orders go back to "preparing"There is no status ruleAllow only the next step, and otherwise reply 409
Wrong order totalsThe total is sent by the clientCompute it from the lines and the prices on the server
Program.cs is 800 lines longAll the logic is in the endpointsSplit it into services and endpoint classes

Exercises

What to Remember

  • CRUD is POST, GET, PUT/PATCH and DELETE on one resource.
  • Use 201 + Location on create, 204 on delete, and 404 for a missing id.
  • The server works out totals and enforces state rules (409).
  • Group endpoints, and move logic into services.

Next: API Documentation with OpenAPI. You will describe this API in one file that tools can read.