Metadata API Example: Create a Custom Channel and Add Platform Events

Instead of Tooling API, you can use Metadata API to create a channel and channel member. We recommend using Metadata API as part of the application lifecycle management process to develop, test, deploy, and release your apps to production. If you want to only configure the channel, we recommend using Tooling API with REST.

User Permissions Needed
To deploy and retrieve metadata types: Customize Application
To update metadata types: Modify Metadata Through Metadata API Functions
To use Metadata API: API Enabled

In this example, you create a channel for orders named Order_Channel__chn, and you add two platform events as members: Order_NorthAmer__e and Order_EMEA__e.

To create a channel and channel member with Metadata API, you can use tools such as Visual Studio Code with the Salesforce Extension pack or Salesforce CLI. For more information, see Metadata API Developer Tools and Quick Start: Metadata API in the Metadata API Developer Guide.

This sample custom channel definition is for the Order_Channel__chn channel. The file name is Order_Channel__chn.platformEventChannel. To have this channel accept platform events, event is specified for channelType.

<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <channelType>event</channelType>
    <label>Custom Channel for Orders</label>
</PlatformEventChannel>

The sample channel member definition associates the custom platform event to the channel. This channel member specifies the platform event, Order_NorthAmer__e, and the channel, Order_Channel__chn. The file name is Order_Channel_chn_Order_NorthAmer_e.platformEventChannelMember.

<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
    <eventChannel>Order_Channel__chn</eventChannel>
    <selectedEntity>Order_NorthAmer__e</selectedEntity>
</PlatformEventChannelMember>

This channel member specifies the custom platform event, Order_EMEA__e, and the channel, Order_Channel__chn. The file name is Order_Channel_chn_Order_EMEA_e.platformEventChannelMember.

<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
    <eventChannel>Order_Channel__chn</eventChannel>
    <selectedEntity>Order_EMEA__e</selectedEntity>
</PlatformEventChannelMember>

This package.xml file references the channel and its two channel members.

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>Order_Channel__chn</members>
        <name>PlatformEventChannel</name>
    </types>
    <types>
        <members>Order_Channel_chn_Order_NorthAmer_e</members>
        <name>PlatformEventChannelMember</name>
    </types>
    <types>
        <members>Order_Channel_chn_Order_EMEA_e</members>
        <name>PlatformEventChannelMember</name>
    </types>
    <version>64.0</version>
</Package>