System Design — How to write REST API Specification

System Design By CHK
5 min readJul 22, 2023

--

As technology continues to advance, Representational State Transfer (REST) has emerged as a widely adopted architectural style for designing web APIs due to its simplicity, flexibility, and scalability. This blog post explores the fundamental principles of REST API design, focusing on its benefits and providing examples of its implementation in the context of e-commerce platforms

To create an effective REST API, developers should adhere to specific design principles that govern its architecture. These principles ensure consistency, flexibility, and efficiency in communication between clients and servers

Stateless Communication

One of the key principles of REST is statelessness, meaning that each API request from a client to the server must contain all the information required to understand and process the request. In other words, the server should not store any client-specific data between requests. This approach simplifies server design, improves scalability, and enhances fault tolerance. In an e-commerce platform, a stateless API ensures that clients can make requests without being tied to a specific server instance, allowing for horizontal scaling as the application grows

Resource-Based URLs

REST APIs expose resources as URLs, making them the fundamental building blocks of the API. A resource is any piece of data that can be accessed and manipulated through the API. In an e-commerce platform, examples of resources include products, shopping carts, orders, and user accounts. Each resource should have a unique and intuitive URL that follows a consistent naming convention, enhancing the discoverability and ease of use for developers integrating with the API

Uniform Interface

The uniform interface principle defines a standard set of constraints that simplify communication between clients and servers. These constraints include the use of HTTP methods (GET, POST, PUT, DELETE) for different operations on resources, proper status codes for indicating the result of the request, and content negotiation to specify response formats (e.g., JSON, XML). By adhering to a uniform interface, e-commerce platforms can ensure a consistent and predictable experience for API consumers, regardless of the underlying server implementation

Hypermedia as the Engine of Application State (HATEOAS)

The HATEOAS principle involves including hypermedia links in API responses to guide clients through the application’s state transitions. In other words, the API should provide links to related resources or actions that a client can take, enabling a more dynamic and interactive user experience.

Examples of REST API Design in E-commerce Platforms

Retrieving Product Information

One of the core functionalities of an e-commerce API is providing product information to clients. Clients can retrieve a list of available products or fetch details of a specific product using a GET request to a resource URL like /products or /products/{productId}. The response can be in JSON or XML format and include hypermedia links for related actions, such as adding the product to the shopping cart or viewing reviews.

Example request:

GET /products HTTP/1.1
Host: api.flipkart.com

Example response:

{
"products": [
{
"id": "12345",
"name": "Smartphone",
"price": 499.99,
"description": "A high-end smartphone with advanced features.",
"links": [
{
"rel": "addToCart",
"href": "/cart/add/12345"
},
{
"rel": "reviews",
"href": "/products/12345/reviews"
}
]
},
// Additional product entries...
]
}

Placing an Order

When a customer is ready to place an order, they can submit a POST request to the orders resource URL, such as /orders. The request payload should contain the necessary information, including the products, quantities, shipping address, and payment details. The API may also support additional order options, such as gift wrapping or express shipping.

Example request:

POST /orders HTTP/1.1
Host: api.flipkart.com
Authorization: Bearer <access_token>
Content-Type: application/json

{
"items": [
{
"productId": "12345",
"quantity": 2
},
// Additional order items...
],
"shippingAddress": {
"street": "123 Main St",
"city": "Example City",
"zipCode": "12345",
"country": "Example Country"
},
"paymentDetails": {
"cardNumber": "1234-5678-9012-3456",
"expiryMonth": "12",
"expiryYear": "2025",
"cvv": "123"
}
}

Example response:

{
"message": "Order successfully placed.",
"orderNumber": "ORD12345",
"total": 999.98
}

Managing User Accounts

RESTful APIs can handle user account management, allowing clients to create, update, and delete user accounts. For example, a POST request to the users resource URL, such as /users, can be used to create a new user account. PUT and DELETE requests can be employed to update and delete user accounts, respectively.

Example request to create a user account:

POST /users HTTP/1.1
Host: api.flipkart.com
Content-Type: application/json

{
"username": "john_doe",
"email": "john.doe@example.com",
"password": "securepassword"
}

Example response:

{
"message": "User account created successfully.",
"userId": "USER12345"
}

Processing Payments

When it comes to processing payments, e-commerce platforms must handle sensitive financial information securely. A common approach is to use a third-party payment gateway, such as PayPal or Stripe. Clients can initiate a payment by sending a POST request to the payments resource URL, such as /payments, along with the necessary payment details.

Example request:

POST /payments HTTP/1.1
Host: api.flipkart.com
Authorization: Bearer <access_token>
Content-Type: application/json

{
"amount": 999.98,
"paymentMethod": "credit_card",
"cardNumber": "1234-5678-9012-3456",
"expiryMonth": "12",
"expiryYear": "2025",
"cvv": "123"
}

Example response:

{
"message": "Payment successful.",
"transactionId": "TRANS12345"
}

Best Practices for Implementing REST APIs

Use Nouns for Resources and Verbs for Actions —Follow the RESTful naming convention by using nouns to represent resources (e.g., /products, /users) and verbs to indicate actions (e.g., GET, POST, PUT, DELETE). This makes the API more intuitive and readable for developers

Versioning APIs — When making changes to the API that may affect existing clients, introduce versioning to ensure backward compatibility. Versioning can be achieved by adding a version number to the API URL (e.g., /v1/products).

Pagination and Filtering — For resource collections with a large number of items, implement pagination to limit the number of results per page. Additionally, provide filtering options (e.g., by category, price range) to help clients retrieve specific data efficiently.

Error Handling —Implement clear and consistent error handling to provide meaningful error messages and HTTP status codes. Proper error responses help developers identify and resolve issues quickly.

Rate Limiting — To prevent abuse and ensure fair usage, implement rate limiting to restrict the number of requests a client can make within a specific time period.

Happy Learning :)

--

--