External resources
How to manage resources via external serverless functions
Commerce Layer lets you manage some resources (i.e. prices, promotions, payment gateways, tax calculators, and more to come) from the outside of the platform itself, using an external service of your choice or a serverless function.
In order to get the data from the external source, when needed, we trigger a POST
request to the endpoint that you specified. The request contains a JSON payload that will be detailed case by case, along with the successful response (or error) format we expect to receive back.
When you create a new external resource, a shared secret is generated. It will be used each time we send data to the specified endpoint to sign the payload. The signature will be stored in the X-CommerceLayer-Signature header so that you can verify the callback authenticity and consider it reliable.
Callbacks securityCircuit breaker
All the external resources are subject to a circuit breaker check: if the call to your external endpoint fails consecutively more than 30 times, the circuit breaker opens and any further request to the resource will be skipped.
An open circuit for an external resource might result in a 422
error in some specific cases (e.g. external prices), but it is usually ignored with a 200 OK
response status to avoid accidental side effects on the order lifecycle.
Resetting the circuit
The circuit is automatically reset anytime a call to your external endpoint succeeds before reaching the counter's threshold.
You can also reset the circuit manually by passing the _reset_circuit
trigger attribute (you must use integration API credentials). This is also your only option in case the circuit is stuck in the open state:
You'll be notified via email every time the circuit breaker counter reaches 10 consecutive failures, as an early warning. Another notification is sent when the circuit breaker opens.
Payload format
The request payload sent to an external endpoint has the same format that you get when fetching a resource through the REST API, with some relevant resources included.
Response format
The response we require from the external endpoint is a JSON featuring the outcome of the operation, along with some data and error information (if any), based on what type of external resource is involved.
In case of success, you can add additional info by populating the response metadata
object and messages
array (that information will be stored respectively in the resource's metadata
attribute and associated notifications
relationship):
Any external endpoint has 3 seconds to respond before being timed out.
Notifications
The notifications data must be specified inside the messages
node of the data
key of a succeeded response, and passed as an array of objects each one containing at least least the name
and the body
attributes (pass the flash
flag if you want mark the notification as temporary):
name
The internal name of the notification.
body
All the information to be notified.
flash
Set it to true
to mark the notification as temporary (default is false
).
You can add notifications just upon a succeeded response of your external endpoint.
Request headers
Any request sent to an external endpoint is enriched with the following headers:
User-Agent
The type of external request, in the format CommerceLayer {{ClassName}}
(e.g. CommerceLayer ExternalGateway
).
X-CommerceLayer-Signature
The encrypted signature you need to verify the callback authenticity.
X-CommerceLayer-TraceId
The ID of the API request that triggered the call to external service (i.e. the trace_id
exposed in the meta
object of the response to that API request — useful for debugging purposes, and more).
X-CommerceLayer-Topic
The topic of the event that triggered the call to the webhook callback URL (e.g. orders.place
— present only in that specific case).
Additionally (e.g. if you need to trace the calls to your external endpoints on your APM) you can pass the following optional Trace Context headers:
traceparent
Uniquely identifies the request in a tracing system.
tracestate
Extends traceparent
with platform-specific span or trace IDs, providing additional vendor-specific trace identification information across different distributed tracing systems.
Last updated