Sync product stock

Send product stock with an external service using product webhooks.

Step 1. Make initial sync of product stock

Before you start processing webhooks, you need to make the initial sync.

Step 2. Connect order webhooks

To start receiving automatic notifications about new orders placed in your store, you need to set up webhooks in your store: Set up webhooks

Request only one product.updated event required for this flow.

Step 2. Parse webhooks for order ID

With webhooks set up, when a new order is placed in the store, Ecwid automatically sends a POST request to your server. It contains the order ID and its payment and fulfillment statuses in a body JSON.

For further automation, you need to automate webhook processing. Check out the following articles to get started:

Code example for webhook processing

Event data in webhooks

Get the order ID from the webhook body and save it as a variable.

Step 3. Automate REST API calls

Now you have everything to automate making a REST API call upon receiving a webhook. The Get order call requires 3 variables:

  • Store ID

  • Order ID

  • App's secret_token

You can "hardcode" the store ID and secret_token on your server's backend as they are constants (as long as the app is not uninstalled/reinstalled in the store). This leaves only one variable – order ID – that you can get from the webhook body.

Additionally, you can pre-define the data you want to receive by limiting Ecwid API's response with specific fields. If not limited, you'll receive full order details with hundreds of fields, which can slow down your application.

<?php

$storeId = '1003'; // your store ID
$orderId = 'K8XTQ'; // order ID from webhook body
$rfields = urlencode('responseFields=id,total,email,paymentMethod'); // Specify fields for the response, changes ',' to %2C
$sToken = 'secret_J9fKJHgiw5PPNJdcLUvucV6gvtXmScjP'; // your app's secret_token
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://app.ecwid.com/api/v3/$storeId/orders/$orderId?$rfields',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer $sToken'
  ),
));

$response = curl_exec($curl);

curl_close($curl);

// $response variable is a JSON with order details

Step 4. Parse the REST API response and send the data

Now you have the details of the new order on your server. Parse the $response variable from the example above and send the details to an external service of your choice.

Last updated

Was this helpful?