Fetching Order Details
Fetching Order Details
Welcome back. In the previous lesson, Shopping Cart and Checkout, you completed the main purchase flow of the storefront. Shoppers could choose quantities, add products to the cart, review line items, apply a tax country, and finish checkout. At the end of that flow, the app redirected them to a success route.
That redirect was an important milestone, but the success experience still needs one more piece to feel complete: it should show the actual order that was created, not just a generic “thanks” message. In this lesson, you will build the final data path that powers that confirmation screen. You will add a focused API helper for reading one order, wrap that request inside a reusable hook, and use the hook in a dedicated success-page client component.
The central pattern here should feel familiar by now. The API layer exposes a small semantic helper. A hook hides the async mechanics and returns { data, loading, error } style state. And the UI component consumes that stable hook interface to branch cleanly between loading, error, empty, and success states. This is the same architecture you have been building throughout the course, and it is exactly what makes the storefront easier to read and extend.
Previously: Checkout Flow and Success Routing
In the previous lesson, the cart page completed checkout through the shared useCart() context and then redirected to /checkout/success/[id]. That meant the route already had access to the new order ID, and the shopper had a clear transition from cart review into a confirmation screen.
This lesson builds directly on that foundation. Instead of keeping the success page generic, you will now use that route parameter to load the specific order that was created. That makes the confirmation screen feel more trustworthy and complete, because the shopper can immediately see which order exists and what total was charged.
Adding a Focused Order API Helper
The first file in this lesson is src/lib/api/orders.ts. This file is intentionally tiny, and that is a good thing. The goal here is not to build a massive order SDK. The goal is simply to give the success page a clean, semantic helper for reading one order by ID.
Here is the complete file:
-
apiRequestis the shared low-level client helper you built earlier in the course. It already knows how to callfetch, parse JSON, understand the API envelope, and turn backend failures into a consistent error shape. -
Orderis the shared domain type that describes what an order looks like in the frontend. Importing that type here lets the helper communicate clearly what kind of data the UI should expect back.
Now look at the actual helper:
-
getOrder(id)is a very small helper, but it is doing important architectural work. Components and hooks should not build endpoint strings inline when the app can provide a meaningful helper likegetOrder()instead. -
The generic
apiRequest<Order>()tells TypeScript that this request resolves to a typedOrder. That means any code callinggetOrder()will get proper autocomplete and type safety for fields likeid,status,total_cents, andcurrency. -
The URL uses a template literal so the provided order ID becomes part of the path. This follows the same dynamic-path pattern you have already seen in product and cart helpers.
-
Keeping the file this small reduces noise and makes the data layer easier to understand at a glance. When a route only needs one action, a small focused module is often a sign of good design, not missing sophistication.
This helper is part of the frontend data layer, not a visual feature. Its only job is to give the UI a trustworthy way to request one order.
