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.When you create a Type on a supported data type, the new fields are not automatically attached to all instances of that data type. Instead, you're creating the possibility to define those values when it makes sense to you. To define the values, you use Custom Fields.
loyaltyPoints field.Extend Project with custom resources
For example, if you needed to keep information about your individual retail stores, you could create a Custom Object for that use case. Within the Custom Object, you could define the store location, contact details, and any other store-specific information.
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.
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.