Webhooks

Webhooks are used to send real time events to external listeners. When a webhook is triggered, Commerce Layer sends a POST request to the webhook's callback URL. The object in the request body has the same format that you get when fetching the resource through the API.

The webhook object

A webhook object is returned as part of the response body of each successful create, list, retrieve or update API call. The following table contains the list of all its fields along with their type, description and example values.
Object fields:
topic
string
The identifier of the resource/event that will trigger the webhook.
Example:
orders.place
callback_url
string
URI where the webhook subscription should send the POST request when the event occurs.
Example:
https://yourapp.com/webhooks
include_resources
array
List of related resources that should be included in the webhook body.
Example:
[customer, shipping_address, billing_address]
id
integer
Unique identifier for the resource.
Example:
1234
created_at
datetime
Time at which the resource was created.
Example:
2018-01-01T12:00:00.000Z
updated_at
datetime
Time at which the resource was last updated.
Example:
2018-01-01T12:00:00.000Z
reference
string
A string that you can use to add your own identifier to the resource. This can be useful for intergrating the resource to an external system, like an ERP, a marketing tool or a CRM.
Example:
ANYREFEFERNCE
metadata
object
Set of key-value pairs that you can attach to the resource. This can be useful for storing additional information about the resource in a structured format.
Example:
{"foo":"bar"}

Create a webhook

To create a new webhook, send a POST request to the /api/webhooks endpoint, passing the resource arguments in the request body. The following table contains the list of all the possible arguments, along with their type, description and examples values. All the arguments marked as required must be present in the request.
Arguments:
topic
required
The identifier of the resource/event that will trigger the webhook.
Example:
orders.place
callback_url
required
URI where the webhook subscription should send the POST request when the event occurs.
Example:
https://yourapp.com/webhooks
include_resources
optional
List of related resources that should be included in the webhook body.
Example:
[customer, shipping_address, billing_address]
reference
optional
A string that you can use to add your own identifier to the resource. This can be useful for intergrating the resource to an external system, like an ERP, a marketing tool or a CRM.
Example:
ANYREFEFERNCE
metadata
optional
Set of key-value pairs that you can attach to the resource. This can be useful for storing additional information about the resource in a structured format.
Example:
#
Example request:
POST /api/webhooks HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "webhooks",
    "attributes": {
      "topic": "orders.place",
      "callback_url": "https://yourapp.com/webhooks"
    }
  }
}
Example response: 201 Created
HTTP/1.1 201 Created
Content-Type: application/vnd.api+json

{
  "data": {
    "id": "1234",
    "type": "webhooks",
    "links": {
      "self": "https://your-brand.commercelayer.io/api/webhooks/1234"
    },
    "attributes": {
      "topic": "orders.place",
      "callback_url": "https://yourapp.com/webhooks",
      "include_resources": "[customer, shipping_address, billing_address]",
      "id": "1234",
      "created_at": "2018-01-01T12:00:00.000Z",
      "updated_at": "2018-01-01T12:00:00.000Z",
      "reference": "ANYREFEFERNCE",
      "metadata": {
        "foo": "bar"
      }
    },
    "relationships": {
    },
    "meta": {
      "mode": "test"
    }
  }
}

List all webhooks

To fetch a collection of webhooks, send a GET request to the /api/webhooks endpoint.

Example request:
GET /api/webhooks HTTP/1.1
Accept: application/vnd.api+json
Example response: 200 OK
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": [
    {
      "id": "1234",
      "type": "webhooks",
      "links": {
        "self": "https://your-brand.commercelayer.io/api/webhooks/1234"
      },
      "attributes": {
        "topic": "orders.place",
        "callback_url": "https://yourapp.com/webhooks",
        "include_resources": "[customer, shipping_address, billing_address]",
        "id": "1234",
        "created_at": "2018-01-01T12:00:00.000Z",
        "updated_at": "2018-01-01T12:00:00.000Z",
        "reference": "ANYREFEFERNCE",
        "metadata": {
          "foo": "bar"
        }
      },
      "relationships": {
      },
      "meta": {
        "mode": "test"
      }
    },
    {
      "other": "... 24 webhooks (first page)"
    }
  ],
  "meta": {
    "record_count": 125,
    "page_count": 5
  },
  "links": {
    "first": "/api/webhooks?page[number]=1&page[size]=25",
    "prev": "/api/webhooks?page[number]=2&page[size]=25",
    "next": "/api/webhooks?page[number]=4&page[size]=25",
    "last": "/api/webhooks?page[number]=5&page[size]=25"
  }
}
Available filters
idtopicreferenceidscreated_at_fromcreated_at_toupdated_at_fromupdated_at_to
Sortable attributes
idcreated_atupdated_atreference

Retrieve a webhook

To fetch a single webhook, send a GET request to the /api/webhooks/{{id}} endpoint, where {{id}} is the id of the resource that you want to retrieve.
Example request:
GET /api/webhooks/1234 HTTP/1.1
Accept: application/vnd.api+json
Example response: 200 OK
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": {
    "id": "1234",
    "type": "webhooks",
    "links": {
      "self": "https://your-brand.commercelayer.io/api/webhooks/1234"
    },
    "attributes": {
      "topic": "orders.place",
      "callback_url": "https://yourapp.com/webhooks",
      "include_resources": "[customer, shipping_address, billing_address]",
      "id": "1234",
      "created_at": "2018-01-01T12:00:00.000Z",
      "updated_at": "2018-01-01T12:00:00.000Z",
      "reference": "ANYREFEFERNCE",
      "metadata": {
        "foo": "bar"
      }
    },
    "relationships": {
    },
    "meta": {
      "mode": "test"
    }
  }
}

Update a webhook

To update an existing webhook, send a PATCH request to the /api/webhooks/{{id}} endpoint, where {{id}} is the id of the resource that you want to update. The following table contains the list of all the possible arguments that you can pass with the request body, along with their type, description and examples values. Please note that all arguments are optional.
Arguments:
topic
optional
The identifier of the resource/event that will trigger the webhook.
Example:
orders.place
callback_url
optional
URI where the webhook subscription should send the POST request when the event occurs.
Example:
https://yourapp.com/webhooks
include_resources
optional
List of related resources that should be included in the webhook body.
Example:
[customer, shipping_address, billing_address]
reference
optional
A string that you can use to add your own identifier to the resource. This can be useful for intergrating the resource to an external system, like an ERP, a marketing tool or a CRM.
Example:
ANYREFEFERNCE
metadata
optional
Set of key-value pairs that you can attach to the resource. This can be useful for storing additional information about the resource in a structured format.
Example:
#
Example request:
PATCH /api/webhooks/1234 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "webhooks",
    "id": 1234,
    "attributes": {
      "topic": "orders.place"
    },
    "relationships": {
    }
  }
}
Example response: 200 OK
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": {
    "id": "1234",
    "type": "webhooks",
    "links": {
      "self": "https://your-brand.commercelayer.io/api/webhooks/1234"
    },
    "attributes": {
      "topic": "orders.place",
      "callback_url": "https://yourapp.com/webhooks",
      "include_resources": "[customer, shipping_address, billing_address]",
      "id": "1234",
      "created_at": "2018-01-01T12:00:00.000Z",
      "updated_at": "2018-01-01T12:00:00.000Z",
      "reference": "ANYREFEFERNCE",
      "metadata": {
        "foo": "bar"
      }
    },
    "relationships": {
    },
    "meta": {
      "mode": "test"
    }
  }
}

Delete a webhook

To delete a webhook, send a DELETE request to the /api/webhooks/{{id}} endpoint, where {{id}} is the id of the resource that you want to delete.
Example request:
DELETE /api/webhooks/1234 HTTP/1.1
Accept: application/vnd.api+json
Example response: 204 No Content
HTTP/1.1 204 No Content

Get our machine-readable JSON schema that follows the OpenAPI Specification (formerly Swagger).

Get our Postman collection in one click and start making real calls to Commerce Layer API in minutes.

Get in touch with our support team if you have any questions or want to learn more about Commerce Layer.