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 /menucreates a new menu item called Cold Coffee. The response is201 Createdand includes aLocationheader.GET /menureturns all three menu items.PUT /menu/3updates the price to ₹190.DELETE /menu/2removes Masala Chai. The response is204 No Content.- The final
GET /menushows 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 usually201 Createdwith aLocationheader. - Read uses
GET /thingsorGET /things/{id}. The response is usually200 OK, or404 Not Foundif the item does not exist. - Update uses
PUTfor replacing a complete item orPATCHfor changing part of an item. The response is usually200 OK, or404 Not Foundif the item does not exist. - Delete uses
DELETE /things/{id}. The response is usually204 No Content, or404 Not Foundif the item does not exist. - Use
MapGroupin 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:
- The five CRUD endpoints and the status codes they use.
- How to organize related endpoints under one path.
- How to create an order with multiple items and a server-calculated total.
- How to protect status changes using
409 Conflict. - 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:
receivedpreparingreadydelivered
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 → preparingis allowed.preparing → deliveredskips thereadystage, so the server returns409 Conflict.preparing → readyis 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.csconnects 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:
- Read the menu.
- Create an order.
- Try creating an invalid order.
- Read the order.
- Delete the order.
- 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
.httpfile 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
| Symptom | Cause | Fix |
|---|---|---|
| Updating a missing item creates it or crashes | There is no 404 check in PUT | Look the item up first, and return 404 if it is missing |
| Delivered orders go back to "preparing" | There is no status rule | Allow only the next step, and otherwise reply 409 |
| Wrong order totals | The total is sent by the client | Compute it from the lines and the prices on the server |
Program.cs is 800 lines long | All the logic is in the endpoints | Split it into services and endpoint classes |
Exercises
What to Remember
- CRUD is POST, GET, PUT/PATCH and DELETE on one resource.
- Use
201+Locationon create,204on delete, and404for 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.