Overview of the concepts related to extending the functionality of your Composable Commerce Project.
commercetools Composable Commerce provides out-of-the-box data structures and behavior that covers the needs of modern commerce solutions. However, your Project may have unique requirements which require these data structures and behavior to be extended.
We've collected a number of common use cases and the recommended extensibility options you can use to achieve them.
Extend resources with Custom Fields
loyaltyPoints.Every customizable resource can be extended with only one Type at a time. However, a Type can contain any number of different fields. For example, the Customer resource can be extended with only one Type, but this Type could consist of multiple different fields that capture the customer's shoe size, nationality, email preferences, and so on.
Extend Project with custom resources
Similar to Custom Fields, creating Custom Objects is an API-only operation.
Extend API calls
Good use cases for API Extensions are calculating custom shipping costs or adding mandatory items, like insurance, to a Cart.
API Extension within the flow of the API call
- If the Extension finds the result to be valid, and does not want to perform any changes, it can return a success. The system will persist the result.
- If the Extension finds that the result is not valid (for example the particular Customer may not purchase this Product), it can return a list of errors. The system will not persist the result and return the errors to the original API Client.
- If the Extension wants to perform additional changes, it can return a list of update actions (for example if it wants to add mandatory insurance to the Cart, it can return an
AddLineItemupdate). The system will validate that the update actions are valid for the given resource type and will try to perform the updates. Should that fail, the original API Client will receive the errors. Otherwise, the original API Client will receive the final result (for example the Cart including the mandatory insurance).
If an API Extension fails to respond properly
504 Gateway Timeout HTTP status code. If the API Extension returns a response, but it could not be parsed properly, the original API Client will receive a 502 Bad Gateway HTTP status code.In both cases, additional information on the cause of the failure is returned to the original API Client.
In terms of Service Level Agreement (SLA), a failure caused by an API Extension does not count as a failure of the commercetools Composable Commerce API. For example, if an API Extension for the Carts is down and causes downtime for your shop, the SLA won't cover that.
Multiple API Extensions in a single API call
If multiple API Extensions are triggered by an API call, they will be called in parallel. Their responses will be merged, but without a guaranteed order (for example, if two Extensions each return an error, their order in the error list is undefined. If two Extensions return updates, the order in which the updates are performed is undefined).
Responses are merged based on their priority or severity:
- A failure to respond properly by any API Extension will cause the whole request to fail with a
502or504. - Otherwise, if any API Extension finds that the result is not valid, the result will not be persisted and an error will be returned to the original API Client.
- Otherwise, if any API Extension returns updates, the result will be modified before it is returned to the original API Client.
API Extensions should operate on separate concerns. For example for a Cart, it is fine to have an Extension each for validating that a Customer is allowed to purchase age-restricted Products, calculating shipping costs, and adding mandatory insurance. A counter-example is two API Extensions changing the Price of a Line Item: One for adding extra costs for optional gift wrapping, the other reducing the Price if an externally defined Discount applies - If both Extensions want to change the same Line Item, one will overwrite the result of the other. For this use case, the whole Price calculation for Line Items should be performed inside a single Extension.
You also need to find a balance between clean separation and the increasing latency risk when calling many parallel Extensions.
Extension Chaining BETA
By declaring dependencies on an Extension, you instruct the platform to run the specified Extensions first. A dependent Extension then receives the resource after its prerequisite Extensions' update actions have been applied, so it can build on their results. Extensions without dependencies continue to run concurrently.
Extension Chaining is well suited for checkout flows where order matters. For example, you can apply discounts before tax calculation, or validate addresses before charging a payment.
dependencies field in the ExtensionDraft when creating an Extension, or use the Set Dependencies update action on an existing Extension. The maximum dependency chain depth is three layers, and each Extension can declare at most five direct dependencies. Circular dependencies are not allowed.Previous state of the updated resource in API Extensions BETA
By default, the payload sent to an API Extension contains only the resource as it is after the update. If your business logic needs to know what changed, you can configure the Extension to also receive the state of the resource before the update. The Extension can then compare the previous and current states to process only the delta, without external lookups.
additionalContext.includeOldResource to true in the ExtensionDraft. The payload then includes an oldResource field alongside resource for Update actions. For Create actions, there is no previous state, so oldResource is not included.Reference Expansion in API Extensions BETA
customerGroup. The Extension payload then contains the full Customer Group object, not just its reference.3 expansion paths per API Extension. To configure expansion paths when creating an API Extension, set the expansionPaths field in the ExtensionDraft. To update the expansion paths on an existing API Extension, use the Set Expansion Paths update action.Subscribe to notifications
Composable Commerce provides three notification payload types through Subscriptions:
- Messages represent changes or actions performed on Project resources, such as Orders, Products, or Customers. They are associated with API queryable resources and include comprehensive metadata such as
versionandsequenceNumberfields to maintain consistency and ordering. - Changes represent updates to a subscribed resource. The payload includes details about whether the resource was created, updated, or deleted.
- Events represent changes or actions that occur across a broader range of services and use cases. Unlike Messages, Events are not associated with API queryable resources. This design allows Events to support notifications for services beyond the HTTP API, such as Import API, Checkout, and Merchant Center. Events typically contain fewer required fields than Messages, making them simpler and more flexible for diverse notification scenarios.
Use Messages when you need notifications about changes to core Composable Commerce resources. Use Changes when you need a concise payload that indicates whether a resource was created, updated, or deleted. Use Events when you need notifications from services like Import API or for use cases that extend beyond traditional Project resources.