---
title: Publish events to Triggers
product: Cloud Code
version: 3.1.0
latest_version: 3.1.0
status: This page documents version 3.1.0. The latest version is 3.1.0.
canonical: https://docs.unity.com/en-us/cloud-code/3.1.0/modules/how-to-guides/publish-trigger-events
latest_url: https://docs.unity.com/en-us/cloud-code/3.1.0/modules/how-to-guides/publish-trigger-events.md
last_updated: 2026-11-30
generated_at: 2026-12-03T05:26:58.224Z
locale: en-us
---

# Publish events to Triggers

> Run scripts, module functions, or webhooks through Triggers when your Cloud Code C# module publishes a custom event.

A Cloud Code C# module can publish its own events, called custom events, to the [Triggers](/triggers.md) service. You choose the event name and payload, and a trigger on that event runs a Cloud Code script, a module function, or a webhook.

Custom events let a function move follow-up work to a separate call so the player doesn't wait for that work to finish. For example, a function that ends a match can publish a `match-ended` event. A trigger on that event can then grant rewards in a separate call.

> **Note:**
>
> Players never receive events that you publish to Triggers. To send a message to connected players, use [push messages](./push-messages.md).

## Prerequisites

Your module needs one of the following, depending on how you build it:

* For Unity Cloud Code modules (.ccmu), the Cloud Code package (`com.unity.services.cloudcode`) version 3.1.0 or later.
* For modules in a separate C# solution, for example a solution linked through a [module reference file](./module-reference-assets.md), the [`Com.Unity.Services.CloudCode.Apis`](https://www.nuget.org/packages/Com.Unity.Services.CloudCode.Apis) package version 0.0.29 or later. To check the version, open the module's .csproj file.

## Publish an event

Cloud Code provides the `ITriggersEventClient` interface, so you don't need to register the interface in your module setup.

When a function in a [stateful module](../stateful-cloud-code) publishes an event, the event has the module's scope, such as `Player` or `MultiplayerSession`. For how scope affects which module functions a trigger can call, refer to [Scoped trigger events](/triggers/tutorials/define-triggers/trigger-structure.md#scoped-trigger-events).

To publish an event, follow these steps:

1. Add an `ITriggersEventClient` parameter to a `CloudCodeFunction` method.
2. Call `PublishEventAsync` with the execution context, a short event name, and an optional payload. For the rules that the name and payload follow, refer to [Name the event and set the payload](#name-the-event-and-set-the-payload).

   The following example publishes a `match-ended` event with the match ID and score.

   ```csharp title="Publish a match-ended event"
   using System.Collections.Generic;
   using System.Threading.Tasks;
   using Unity.Services.CloudCode.Apis.Triggers;
   using Unity.Services.CloudCode.Core;

   namespace MatchExample
   {
       public class MatchModule
       {
           [CloudCodeFunction("EndMatch")]
           public async Task<string> EndMatch(IExecutionContext context, [CloudCodeService] ITriggersEventClient triggersEventClient,
               string matchId, int score)
           {
               var payload = new Dictionary<string, object>
               {
                   { "matchId", matchId },
                   { "score", score }
               };

               return await triggersEventClient.PublishEventAsync(context, "match-ended", payload);
           }
       }
   }
   ```

After you call `PublishEventAsync`, the returned task completes when Cloud Code accepts the event and returns the full event type, such as `com.unity.services.cloud-code.custom.match-ended.v1`. Triggers receives the event asynchronously, so a completed task doesn't mean a trigger has run.

If `PublishEventAsync` throws an `ArgumentException`, check the event name and payload against the [rules](#name-the-event-and-set-the-payload). An `InvalidOperationException` means Cloud Code refuses or can't send the event, for example because the event exceeds the [event chain limit](#respond-to-the-event).

Cloud Code builds every event type in the following format.

```text
com.unity.services.cloud-code.custom.<event-name>.v1
```

### Name the event and set the payload

The event name and payload follow these rules:

* The event name can have up to 64 characters and can contain only lowercase letters from `a` to `z`, digits from `0` to `9`, hyphens (`-`), and underscores (`_`).
* Pass the short event name only, such as `match-ended`. Cloud Code adds the namespace and version.
* Cloud Code serializes the payload to JSON. The JSON can be up to 10 KB.
* To publish an event without data, pass `null` as the payload or omit the payload.

A trigger that uses an event depends on the event's payload keys. You can add keys at any time. To remove or rename a key, publish the event under a new name.

## Respond to the event

To run an action when a module publishes the event, create a trigger on the event's full event type.

The following trigger configuration runs the `GrantMatchRewards` function of the `MatchRewards` module each time a module publishes `match-ended`. For the fields and the ways to create a trigger, refer to [Trigger structure](/triggers/tutorials/define-triggers/trigger-structure.md) and [Define triggers](/triggers/tutorials/define-triggers.md).

```json title="Trigger on the match-ended event"
{
  "name": "grant-match-rewards",
  "eventType": "com.unity.services.cloud-code.custom.match-ended.v1",
  "actionType": "cloud-code",
  "actionUrn": "urn:ugs:cloud-code:MatchRewards/GrantMatchRewards"
}
```

The Triggers service passes the payload to the action as parameters, so `GrantMatchRewards` receives `matchId` and `score` by name. A JavaScript script receives only the payload keys that it declares as parameters.

To run the trigger for some events only, add a [filter](/triggers/tutorials/define-triggers/filters.md) on the payload. A filter fails on an event that doesn't contain the key that the filter reads, so check for the key first. Add the following `filter` field to the trigger configuration.

```json title="Filter on the score"
"filter": "'score' in data && data['score'] >= 100"
```

> **Warning:**
>
> A function that a trigger runs can publish another event, and a trigger on that event can run another function. Each time an event runs a trigger, and each time a function calls another module or script, the call depth increases by one. Cloud Code limits the call depth to five, so a chain that starts from a player's call can run up to five triggers. When publishing an event goes past that depth, `PublishEventAsync` throws an `InvalidOperationException`. Design your triggers so that an event never leads back to the function that published the event.

After you create the trigger, the Triggers service runs `GrantMatchRewards` each time a module publishes `match-ended` with a score of 100 or more.

To create the trigger in the Unity Dashboard, refer to [Add a Cloud Code custom event](/triggers/tutorials/define-triggers/unity-dashboard.md#add-a-cloud-code-custom-event).

## Test locally

From Cloud Code package version 3.1.1, the [local Cloud Code server](./local-cloud-code-server.md) supports `ITriggersEventClient`. The local server checks each event the same way Cloud Code does, then logs the event instead of sending the event to Triggers.

To test that a function publishes a valid event, follow these steps:

1. Run the module on the local server. For more information, refer to [Execute modules on the local server](./local-cloud-code-server.md#execute-modules-on-the-local-server).
2. Call the function that publishes the event.
3. Open the local server's logs. For more information, refer to [Find the local server's logs and data](./local-cloud-code-server.md#find-the-local-server's-logs-and-data).

After you open the logs, the log entries show the full event type, scope, and payload of each event that passes the checks. If the event isn't valid, the function throws the same exception that it throws in the cloud.

The local server doesn't run triggers. To test the trigger itself, use a cloud environment.

## Additional resources

* [Events](/triggers/manage-events/events.md)
* [Trigger structure](/triggers/tutorials/define-triggers/trigger-structure.md)
* [Debug and monitor events](/triggers/manage-events/debug-events.md)
