Inventory API

Webhooks

The Inventory API offers a webhook type called inventory.count.updated, which sends a notification every time the quantity is updated for a catalog item variation. Use the inventory.count.updated webhook to build real-time notifications and gain fast, reliable, and accurate insight to all relevant inventory items and events.

Backend
Inventory API

How Inventory notifications work
Permalink Get a link to this section

Each webhook notification includes basic information such as the event type and event ID. The data field of each webhook notification contains detailed information about the specific event. For inventory.count.updated notifications, the data field includes 1 inventory count for every item variation updated. For example:

{
   "type": "inventory.count.updated",
   "event_id": "123456",
   "created_at": "2019-05-14T17:51:44Z",
   "merchant_id": "MERCHANT123",
   "data": {
      "inventory_counts": [
         {
           "location_id": "locationToken",
           "catalog_object_id": "XYZ456",
           "catalog_object_type": "ITEM_VARIATION",
           "state": "IN_STOCK",
           "quantity": "12.375",
           "calculated_at": "2019-05-14T17:51:27Z"
         }
      ]
   }
}

State Adjustments
Permalink Get a link to this section

The inventory.count.updated webhook event also sends notifications when a quantity of item variations changes state. For state adjustments, inventory.count.updated sends a single notification containing 2 inventory counts: 1 count that describes the quantity and state before the state change, and 1 that describes the quantity and state after the state change.

Bulk Updates
Permalink Get a link to this section

A single webhook notification can contain up to 100 inventory counts. If a bulk update includes more than 100 updates, the event data spans multiple webhook notifications.

Process an inventory_count webhook
Permalink Get a link to this section

The example code below builds a webhook listener that receives an inventory.count.updated notification and prints a catalog_object_id for each inventory_count.

Step 1: Set your credentials
Permalink Get a link to this section

Load the Unirest library then set your credentials and request headers.


#Unirest PHP library. 
require 'vendor/autoload.php';

# Your application's access token
$accessToken = 'REPLACE_ME';

# Your webhook signature key
$webhookSignatureKey = 'REPLACE_ME';

# The URL that should receive webhook notifications
$notificationURL = 'REPLACE_ME';

# Headers to provide to Connect API endpoints
$requestHeaders = array (
  'Authorization' => 'Bearer ' . $accessToken,
  'Accept' => 'application/json',
  'Content-Type' => 'application/json'
);

Step 2: Create a webhooks listener function
Permalink Get a link to this section

Create a listener function to process the JSON body of the incoming POST request. The example code below simply prints a message to the screen. To retrieve more information about the updated catalog item, use the provided catalog_object_id and the Catalog.RetrieveCatalogObject endpoint.

function webhookListener($requestHeaders) {

  // Decode the notification body
  $notificationBody = json_decode($_POST, true);

  // Get the inventory counts
  $inventoryCounts = $notificationBody['data']['inventory_counts'];

  // Print results to the screen
  echo "The quantity has been updated for the following "
       . "item variations. You can use the catalog_object_id to look up "
       . "more information. \n";

  // Loop through all the inventory counts and prints the
  foreach ($inventoryCounts as $currentInventoryCount) {
      echo  "\n Catalog object ID: " . $currentInventoryCount['catalog_object_id'] .
          "\n Catalog object type:" . $currentInventoryCount['catalog_object_type'] .
          "\n";
}



// Run your code
webhookListener();