loading...

BigCommerce – Creating a WebHook

ijason profile image Jason Originally published at blog.ijasoneverett.com on ・1 min read

I couldn’t find any reliable C# examples on how to create a webhook using BigCommerce’s API so I thought I’d share my solution. The example code below will create a webhook when an order is created in your BigCommerce store.

//BigCommerce Authorization
string clientID = "<your_client_id>";              
string token = "<your_token>";
string storeHash = "<your_store_hash>";
string resourcePath = "hooks";

string baseURL = "https://api.bigcommerce.com/stores/" + storeHash + "/v2/" + resourcePath;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURL);
req.AllowAutoRedirect = true;
req.ContentType = "application/json";
req.Accept = "application/json";
req.Method = "POST";

req.Headers.Add("X-Auth-Client", clientID);
req.Headers.Add("X-Auth-Token", token);             

//send scope and destination as json
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
    string json = "{\"scope\":\"store/order/created\"," +
                  "\"destination\":\"https://app.example.com/orders\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

string jsonResponse = null;
using (WebResponse resp = req.GetResponse())
{
    if (req.HaveResponse && resp != null)
    {
        using (var reader = new StreamReader(resp.GetResponseStream()))
        {
            jsonResponse = reader.ReadToEnd();
        }
    }
}

Response.Write(jsonResponse);

Posted on by:

ijason profile

Jason

@ijason

I've been a .NET developer for over 10 years. I also code in PHP and Obj-C on my own free time. Probably hopped up on caffeine right now.

Discussion

pic
Editor guide
 

Hello Jason, I'm working with BigCommerce WebHooks and I was wondering if you know if the body message of a product update change or not.

I mean, many events can trigger store/product/updated how do I know which information has changed?

{
"scope": "store/product/updated",
"store_id": "123456",
"data": {
"type": "product", <------ (Does this change????)
"product_id": 185
},
"hash": "dc475e1059f2a67a55818bea29bf6b23ebbda707",
"created_at": 1535480603,
"producer": "stores/abcdefg"
}

 

I don't work with BigCommerce anymore but in my experience their web hooks are always lightweight meaning they only ever send you the id of the object you subscribed to. Its up to you to pull the details of that object via their API and compare the changes, if any, yourself.