Designing REST APIs
- A REST API (API) exposes your application as resources. A resource is usually a noun in your business domain.
SuperMarket – Inventory
- Identify possible things or nouns in supermarket inventory
| Business thing/noun | REST Resource |
| ——————- | ————– |
| Product | /products |
| Supplier | /suppliers |
| Store/Branch | /stores |
| purchase order | /purchase-orders |
| Inventory stock | /inventory-items |
- API design starts by asking
what are important things in my system users want to create, read, update or delete
- Examples: In supermarket we need to track
- Products: rice, milk, shampoo
- Categories: groceries, dairy, bakery
- Suppliers: Amul, ITC, local bakery (Iyengars Bakery)
- Stores: Hyderabad branch, Bangalore Branch, Noida Branch
- Current Stock quantity
- Stock coming in & going out
- Expired/Damaged items
Identifying Main resources
- Product: A product is the master item
{
"id": 101,
"name": "Amul Milk 1L",
"sku": "MILK-AMUL-1L",
"category-id": 5,
"unit": "litre",
"mrp": 75
}
- Operations – Product
GET /products # list all products
GET /products/101 # Give me info about Amul Milk 1 lt
POST /products # create a product
PUT /products/101 # update
DELETE /products/101 # delete
-
/products refers to a collection and /products/101 refers to one item
-
Operations – Store
GET /stores
GET /stores/1
POST /stores
DELETE /stores/1
- Data
{
"id": 1,
"name": "Hyderabad Main Branch",
"location": "Ameerpet"
}
Identfying Resources for Redbus – Bus Ticketing platform
- Things
- Bus Operator
- Routes
- Seats
- Bookings
- Passengers
- City
- Bus
- Seat
- Payment
Identifing Resources for Swiggy
- Things:
- Customer
- Restaurant
- Menu
- Menu Item
- Delivery Partner
- Address
- Coupons
- Complaint
- Order
- Payment
- On each thing what are possible actions
- Create a customer
- Update a customer
- Delete a customer
- Get all customers
- REST
/customers
GET /customers => get all customers
POST /customers => create
PUT /customers => update
GET /customers/1234 => get specific customer
-
API Design is generally done in openapi format
-
Exercise:
- Understand REST APIs of
- any weather providers
- any bulk sms provider
- Understand REST APIs of
