A clean and practical REST API for restaurant flow management: products, tables, table sessions, and orders.
Built to be simple enough for beginners, but structured enough to scale.
This API solves a real restaurant workflow:
- Register and manage products.
- List available tables.
- Open and close table sessions.
- Create orders linked to active sessions.
- Calculate the total bill for a table session.
In short: it helps you track what was ordered, where, and how much to charge.
- Product CRUD with validation using Zod.
- Table listing endpoint.
- Session control per table (prevents opening multiple active sessions for the same table).
- Order creation with business validations:
- Session must exist.
- Session must be open.
- Product must exist.
- Bill total calculation by table session.
- Centralized error handling for business and validation errors.
src/
controllers/ # Request handlers and business flow
routes/ # API routes grouped by resource
database/
migrations/ # Database schema history
seeds/ # Initial/demo data
types/ # Repository type definitions
knex.ts # Knex client instance
middlewares/ # Express middlewares
utils/ # Shared utilities (AppError)
server.ts # App bootstrap- Node.js
- TypeScript
- Express
- Knex
- SQLite3
- Zod
git clone https://github.com/diegodevtech/API-restaurant.git
cd API-restaurantnpm installnpm run knex -- migrate:latestnpm run knex -- seed:runnpm run devServer will be running at:
http://localhost:3333Base URL: http://localhost:3333
GET /products-> list all productsGET /products?name=brownie-> filter products by namePOST /products-> create productPUT /products/:id-> update productDELETE /products/:id-> delete product
Example payload for POST /products:
{
"name": "Brownie with Pistachio Cream",
"price": 14.5
}GET /tables-> list all tables
GET /tables-sessions-> list sessionsPOST /tables-sessions-> open a session for a tablePATCH /tables-sessions/:id-> close a session
Example payload for POST /tables-sessions:
{
"table_id": 1
}GET /orders-> list all ordersGET /orders/table-session/:table_session_id-> list orders for one sessionGET /orders/table-session/:table_session_id/total-> get total amount for one sessionPOST /orders-> create order
Example payload for POST /orders:
{
"table_session_id": 1,
"product_id": 2,
"quantity": 3
}The API returns structured errors for:
- Business rules (for example: opening an already open table).
- Validation failures from Zod.
- Unexpected server errors.
Run these commands after starting the server:
# 1) See tables
curl -s http://localhost:3333/tables | jq
# 2) Open table 1
curl -s -X POST http://localhost:3333/tables-sessions \
-H "Content-Type: application/json" \
-d '{"table_id":1}'
# 3) Add one order to session 1
curl -s -X POST http://localhost:3333/orders \
-H "Content-Type: application/json" \
-d '{"table_session_id":1,"product_id":1,"quantity":2}'
# 4) Check total
curl -s http://localhost:3333/orders/table-session/1/total | jqIf jq is not installed, remove | jq and read raw JSON.
npm run dev-> run API in watch modenpm run knex -- migrate:latest-> run migrationsnpm run knex -- migrate:rollback-> rollback last migrationnpm run knex -- seed:run-> run seeds
- Same Node.js major version across machines.
- Run migrations before seeds.
- Keep database file generated in
src/database/database.db. - Use local port
3333(default in project).
This project is intentionally straightforward: no mystery layers, no magic tricks, and no "works only on my machine" drama.