BEGINNER • API Foundations and HTTP
API Operations Playbook #20
This lesson targets protect sensitive operations in a realistic payments status gateway workflow. You will practice commands: curl http://localhost:3000/health | res.status(200).json(...) | req.params / req.query / req.body. The code example is specific to backend API engineering tasks for this lesson objective.
Code Example
import express from "express";
const app = express();
app.use(express.json());
app.get("/health", (_req, res) => {
res.status(200).json({
status: "ok",
service: "backend-api-mastery",
objective: "protect sensitive operations",
});
});
app.post("/orders/preview", (req, res) => {
const { items = [] } = req.body as { items?: Array<{ qty: number; price: number }> };
const total = items.reduce((sum, item) => sum + item.qty * item.price, 0);
return res.status(200).json({ scenario: "payments status gateway", total });
});
app.listen(3000, () => console.log("Run:", "curl http://localhost:3000/health"));Commands & References
- curl http://localhost:3000/health
- res.status(200).json(...)
- req.params / req.query / req.body
Lab Steps
- Prepare environment using: curl http://localhost:3000/health
- Implement endpoint or middleware for this scenario.
- Test positive/negative cases and inspect response contracts.
- Document one reliability or security improvement.
Exercises
- Add one edge-case validation and return clear error details.
- Improve one endpoint to be idempotent and test retries.
- Write one test case proving the expected API behavior.