On this situation, we proceed with our hands-on exploration of the 2 remaining API design examples. We’ll discover learn how to construct a buying cart API and research the Stripe cost API redesign.
On this part, we design a easy buying cart.
A buying cart must help the next core options:
-
Making a cart
-
Viewing a cart
-
Including an merchandise to a cart
-
Viewing objects inside a cart
We’re defining our sources as cart and merchandise. Beneath, we’ve outlined the corresponding APIs:
Notice that we use mine as a particular cart identifier as a result of a person has just one buying cart.
After we add an merchandise to a cart, a Google-style API may specify the verb within the URL, like so:
POST /v1/carts/mine/objects:add
There are some who aren’t followers of colons within the URLs. In our view, builders ought to comply with their group’s API specs. After we all converse the identical language, we’re much less susceptible to errors.
Take into account a buying cart full of many objects. To boost our question expertise, we’ll introduce filtering, sorting, and pagination.
-
Filtering
Let’s take into consideration querying all pink objects in our cart. Right here’s what the API would appear like:
GET /v1/carts/mine/objects?filter=pink
Notice that we have to be cautious about offering broad filtering capabilities. This might probably influence efficiency adversely. As a greater different, we must always supply structured filtering which limits the choices to sure parameters like colour and weight.
For product search pages, structured filtering turns into much more important. They require further filters like worth, class, location, and extra.
-
Sorting
Think about…