Goal
Create a Subscription that sends a notification via email to your customer when an order is created.
Prerequisites
-
Credentials for an API Client with the
Manage Subscriptionsscope. If you don't have an API Client yet, create one. -
This tutorial covers three Destination types:
- AWS EventBridge: you need an AWS account Identifier and the region in which you want to receive the messages. For help getting started, see Getting started with Amazon EventBridge.
- AWS SQS: you need an AWS account and an SQS queue. For help getting started, see Getting Started with Amazon SQS.
- Azure Service Bus (ASB): you need an Azure account and a queue in Azure Service Bus. For help getting started, see Create a queue using the Azure portal.
Create a Subscription
Before creating a Subscription, you need to gather some data from the destination service you are using.
Access and queue
AWS EventBridge
AWS SQS
Azure
Messages
messages field is an array of MessageSubscriptions which require a resourceTypeId. For the full list of supported values, see MessageSubscriptionResourceTypeId.order as resourceTypeId in different languages.EventBridge Subscription
{
"destination": {
"type": "EventBridge",
"accountId": "<my-account-id>",
"region": "<my-region>"
},
"messages": [
{
"resourceTypeId": "order"
}
]
}
If the creation was successful, you will get a response similar to the following:
{
"changes": [],
"createdAt": "<timestamp>",
"destination": {
"accountId": "<my-account-id>",
"region": "<my-region>",
"source": "aws.partner/commercetools.com/<project-key>/eventbridge",
"type": "EventBridge"
},
"id": "<subscription-id>",
"lastModifiedAt": "<timestamp>",
"messages": [
{
"resourceTypeId": "order",
"types": []
}
],
"version": 1
}
SQS Subscription
{
"destination": {
"type": "SQS",
"queueUrl": "<url-to-my-queue>",
"authenticationMode": "IAM",
"region": "<my-region>"
},
"messages": [
{
"resourceTypeId": "order"
}
]
}
If the creation was successful, you will get a response similar to the following:
{
"changes": [],
"createdAt": "<timestamp>",
"destination": {
"accessKey": "<my-access-key>",
"accessSecret": "<my-access-secret>",
"queueUrl": "<my-queue-url>",
"region": "<my-region>",
"type": "SQS",
"authenticationMode": "Credentials"
},
"id": "<subscription-id>",
"lastModifiedAt": "<timestamp>",
"messages": [
{
"resourceTypeId": "order",
"types": []
}
],
"version": 1
}
ASB Subscription
{
"destination": {
"type": "AzureServiceBus",
"connectionString": "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<key-name>;SharedAccessKey=<key>;EntityPath=<queue-name>"
},
"messages": [
{
"resourceTypeId": "order"
}
]
}
If the creation was successful, you will get a response similar to the following:
{
"changes": [],
"createdAt": "<timestamp>",
"destination": {
"connectionString": "<my-connectionString>",
"type": "AzureServiceBus"
},
"id": "<subscription-id>",
"lastModifiedAt": "<timestamp>",
"messages": [
{
"resourceTypeId": "order",
"types": []
}
],
"version": 1
}
Receive a message using PHP and SQS
To receive a message from the queue and use its body to send a notification, you need to poll it from the queue. For the purpose of this tutorial, the following example prints the message body in the browser using PHP. You are free to use any language you prefer.
$client = SqsClient::factory(array(
'profile' => '<my-profile>',
'region' => '<my-region>'
));
$result = $client->receiveMessage(array(
'QueueUrl' => <my-queue-url>,
));
foreach ($result->getPath('Messages/*/Body') as $messageBody) {
// Do something with the message
echo $messageBody;
}
When you run this code, and if you have a message in your queue, you will see its body in the browser.