Newer Version Available

This content describes an older version of this product. View Latest

Publish Event Messages with Salesforce APIs

External apps use an API to publish platform event messages.

Publish events by inserting events in the same way that you insert sObjects. You can use any Salesforce API to create platform events, such as SOAP API, REST API, or Bulk API 2.0.

When publishing an event message, the result that the API returns contains information about whether the operation was successful and the errors encountered. If the success field is true, the publish request is queued in Salesforce and the event message is published asynchronously. For more details, see High-Volume Platform Event Persistence. If the success field is false, the event publish operation resulted in errors, which are returned in the errors field.

The returned result also contains the Id system field. The Id field value is not included in the event message delivered to subscribers. It is not used to identify an event message, and is not always unique. Subscribers can use the ReplayId system field, which is included in the delivered message, to identify the position of the event in the stream.

The examples in the following sections are based on a high-volume platform event.

REST API

To publish a platform event message using REST API, send a POST request to the following endpoint.

Example

If you’ve defined a platform event named Low Ink, publish event notifications by inserting Low_Ink__e data. This example creates one event of type Low_Ink__e in REST API.

REST endpoint:

Request body:

After the platform event message is published, the REST response looks like this output. Headers are deleted for brevity.

REST API Composite Resource

To publish multiple platform event messages in one REST API request, use the composite resource. Send a POST request to the following endpoint.

Add each platform event as a subrequest in the composite request body.

Example

This composite request contains two platform events in the request body.

After the platform event messages are published, the REST response looks like this output. Headers are deleted from this sample response.

The allOrNone header in the composite REST request and in SOAP API applies only to platform events defined with the Publish After Commit option. For more information, see Platform Events and Transactions.

Note

SOAP API

To publish a platform event message using SOAP API, use the create() call.

Example

This example shows the SOAP message (using Partner API) of a request to create three platform event messages in one call. Each event has one custom field named Printer_Model__c.

The response of the Partner SOAP API request looks something like the following. Headers are deleted for brevity.