Sub topic 6
Publishing events through the REST API
This chapter shows how an external system publishes a platform event over HTTP. It assumes you have already defined Order_Shipped__e with the fields Order_Number__c and Status__c. It also assumes you can obtain an OAuth access token for an integration user.
How REST publishing works
Salesforce exposes every platform event as an sObject. To publish an event, the external system creates a record of the event's API name with a POST to the standard sObject endpoint. Salesforce does not store the record. It places the event on the event bus, where subscribers receive it.
The caller needs these things:
- An access token for a user whose profile or permission set grants Create on the event object.
- The instance URL, such as
https://yourdomain.my.salesforce.com. - The event API name, which ends in
__e.
Send the request
The request body is a JSON object whose keys are the field API names.
POST /services/data/v60.0/sobjects/Order_Shipped__e/
Authorization: Bearer <access_token>
Content-Type: application/json
{
"Order_Number__c": "00012345",
"Status__c": "Shipped"
}Send it to your instance URL. For example, with curl:
curl -X POST "https://yourdomain.my.salesforce.com/services/data/v60.0/sobjects/Order_Shipped__e/" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Order_Number__c": "00012345", "Status__c": "Shipped"}'Read the response
A successful call returns HTTP 201 and a JSON body like this:
{
"id": "e00xx0000000001AAA",
"success": true,
"errors": []
}The id is not a record you can query. Treat success: true as "Salesforce accepted the event for publishing". It does not mean any subscriber has processed the event.
A failed call returns an error array. Typical causes are in the table below.
| Status | Typical cause | Fix |
|---|---|---|
| 400 | Unknown field, wrong data type, or a missing required field | Check the field API names and values against the event definition |
| 401 | Expired or invalid access token | Request a new token |
| 403 | The user lacks Create permission on the event, or a publishing limit is reached | Grant the permission, or check your allocations |
| 404 | Wrong event API name or API version | Confirm the name ends in `__e` |
Publish behavior applies here too
The event's publish behavior setting still controls timing. A REST call is its own transaction. With Publish Immediately, the event goes out as soon as the request is processed. With Publish After Commit, it goes out after the transaction commits. For a stand-alone REST call with no other DML, the two behave almost the same.
Publishing several events
The sObject Collections endpoint accepts many events in one request, which uses fewer API calls.
POST /services/data/v60.0/composite/sobjects
Authorization: Bearer <access_token>
Content-Type: application/json
{
"allOrNone": false,
"records": [
{ "attributes": { "type": "Order_Shipped__e" }, "Order_Number__c": "00012345", "Status__c": "Shipped" },
{ "attributes": { "type": "Order_Shipped__e" }, "Order_Number__c": "00012346", "Status__c": "Shipped" }
]
}The response lists a result for each record in the same order. Check each success value, because one event can fail while another succeeds when allOrNone is false.
Exercise
A REST call to publish an event returns HTTP 201 with "success": true. Does that confirm that your Apex trigger has already created its Task records?
Show answer
No. The response only confirms that Salesforce accepted the event for publishing. Subscribers receive the event from the event bus afterward, and they process it asynchronously, so the trigger may not have run yet.