Overview
The Stock Management API provides atomic operations for managing product stock levels. These endpoints support the complete lifecycle of inventory management from adding new stock to consuming it during order fulfillment.Stock Operations Workflow
Add Stock
/api/v1/inventory/{id}/addStock
Path Parameters
The unique identifier of the product to add stock to
Request Body
The quantity of stock to add. Must be zero or positive.
Request Example
Response
Returns HTTP 204 (No Content) on success
Example Scenario
Reserve Stock
/api/v1/inventory/{id}/reserveStock
Path Parameters
The unique identifier of the product to reserve stock from
Request Body
The quantity of stock to reserve. Must be zero or positive. Cannot exceed available stock.
Request Example
Response
Returns HTTP 204 (No Content) on success
Example Scenario
Release Stock
/api/v1/inventory/{id}/releaseStock
Path Parameters
The unique identifier of the product to release stock for
Request Body
The quantity of stock to release. Must be zero or positive.
Request Example
Response
Returns HTTP 204 (No Content) on success
Example Scenario
Consume Stock
/api/v1/inventory/{id}/consumeStock
Path Parameters
The unique identifier of the product to consume stock from
Request Body
The quantity of stock to consume. Must be zero or positive.
Request Example
Response
Returns HTTP 204 (No Content) on success
Example Scenario
The consume operation typically affects reserved stock, not available stock. The available stock was already reduced during the reserve operation.
Complete Order Lifecycle Example
- Happy Path
- Cancelled Order
- Restocking
Best Practices
Always reserve before consuming
Always reserve before consuming
In a typical order workflow, always call
reserveStock when the order is placed, then call consumeStock when the order is fulfilled. This prevents overselling and maintains accurate inventory counts.Release stock for cancelled orders
Release stock for cancelled orders
If an order is cancelled after stock has been reserved, always call
releaseStock to return the inventory to available status. Failing to do this will result in “phantom” reservations that reduce available stock indefinitely.Handle concurrent requests
Handle concurrent requests
The service implements proper concurrency controls, but applications should still handle potential race conditions when multiple orders are placed simultaneously for products with low stock.
Validate stock availability
Validate stock availability
Before attempting to reserve stock, check the
availableStock field to ensure sufficient inventory exists. This provides better user experience than waiting for a reservation failure.Error Handling
All stock management endpoints can return the following errors:| Status Code | Description | Solution |
|---|---|---|
| 400 | Invalid request (e.g., negative amount) | Ensure amount is zero or positive |
| 404 | Product not found | Verify the product ID exists |
| 409 | Insufficient stock for reservation | Check available stock before reserving |
| 500 | Internal server error | Retry the request or contact support |