Decrement Item's Stock

Decreases the stock of an item.

Use this endpoint in your API integration to decrease an item's in_stock value by the specified quantity.

This API reference is for developers. Call PUT /item/{id}/decrement when fewer items are available, such as after a customer purchases the item. The {id} notation is an API path template: replace it with your item's ID when sending a request, as shown in the example below.

Decrease stock by one

  1. Prepare your merchant API key to authenticate the request with a Bearer token (your API key sent in the Authorization header).
  2. Call Retrieve All Catalog Items and copy the id of the item you want to update from the response's data array. Replace the example UUID (a unique identifier formatted as a hyphenated string) below with that value.
  3. Run the following example with Python 3. It uses the standard library and sets quantity to 1 to decrease stock by one. Running it changes the selected item's inventory; do not repeat the request unless you intend to decrease stock again.
import getpass
import json
import sys
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

# Copy the item's id from the data array returned by GET /item.
item_id = "0a6c966c-6a70-44eb-aa26-673310ca65c0"
quantity = 1
api_key = getpass.getpass("Merchant API key: ").strip()
if not api_key:
    sys.exit("Enter a merchant API key before sending the request.")

query = urlencode({"quantity": quantity})
url = f"https://apiprod.fattlabs.com/item/{item_id}/decrement?{query}"
request = Request(
    url,
    method="PUT",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Accept": "application/json",
    },
)

try:
    with urlopen(request, timeout=30) as response:
        status = response.status
        item = json.load(response)
except HTTPError as error:
    body = error.read().decode("utf-8", errors="replace")
    sys.exit(f"HTTP {error.code}: {body}")
except (URLError, TimeoutError, OSError) as error:
    sys.exit(f"Request failed: {error}. Check stock before retrying.")
except (ValueError, UnicodeError) as error:
    sys.exit(f"Invalid JSON response: {error}. Check stock before retrying.")

if status != 200:
    sys.exit(f"Unexpected HTTP {status}. Check stock before retrying.")
if not isinstance(item, dict) or not isinstance(item.get("in_stock"), int):
    sys.exit("Response is missing an integer in_stock. Check stock before retrying.")

print(f"HTTP {status}")
print(f"Updated in_stock: {item['in_stock']}")
if item["in_stock"] < 0:
    print("Stock is negative. Review the item's inventory before further changes.")

Check the updated stock

On success, the API returns HTTP 200 with the updated item, including its in_stock value. The script prints that value. For example, the documented response has in_stock set to -9, which produces:

HTTP 200
Updated in_stock: -9
Stock is negative. Review the item's inventory before further changes.

Your stock value depends on the selected item's inventory before the request. The documented response demonstrates that stock can be negative; do not assume the API prevents it.

If the API cannot find the item, it returns HTTP 400 with this response:

{
  "id": [
    "The item was not found"
  ]
}

Copy the correct item ID from your merchant's catalog response before trying again. For other HTTP errors, the script prints the status and response body. If a request times out or the response cannot be read, retrieve the item with Retrieve an Item by ID before retrying: the stock change may already have succeeded.

Path Params
string
required

ID of the item.

Query Params
int32
required

The amount to reduce the in_stock value.

Responses

Language
Credentials
Bearer
JWT
LoadingLoading…
Response
Click Try It! to start a request and see the response here! Or choose an example:
application/json