Trigger structure
Understand the structure of a trigger configuration that defines what to invoke when an event fires.
Read time 3 minutesLast updated 3 hours ago
A trigger configuration defines what should be invoked when an event is fired. The structure differs depending on whether the action is Cloud Code or a webhook.
Cloud Code trigger
A Cloud Code trigger invokes a script or module when the event fires. It usesactionType: cloud-codeactionUrn{ "name" : "example-trigger", "eventType" : "com.unity.services.{{SERVICE_NAME}}.{{EVENT_NAME}}.v{{EVENT_VERSION}}", "actionUrn" : "urn:ugs:cloud-code:{{SCRIPT}}", "actionType" : "cloud-code", "filter": "data['parameter'] == 'value'"}
- : The name of the trigger. It must be unique per project.
name - : Matches the trigger to an event. It is composed of the
eventTypeandeventNamefields, corresponding to theeventVersionandeventNamefields in the event configuration.payloadVersion - : What is invoked when the trigger fires. For example,
actionUrninvokes theurn:ugs:cloud-code:TestScriptCloud Code script, orTestScriptinvokes theurn:ugs:cloud-code:TestModule/TestFunctionfunction in theTestFunctionCloud Code module.TestModule - : Set to
actionTypefor Cloud Code triggers.cloud-code - : Optional. A condition that must be met for the trigger to fire. For more information, refer to Filters.
filter
Webhook trigger
A webhook trigger sends an HTTP request to an external URL when the event fires. The webhook trigger configuration requires the following fields:actionType: webhookactionUrn: urn:ugs:webhook- : An object containing the request details:
webhook- (Required): The destination URL.
url - (Optional): The HTTP method (for example
methodorPOST). Defaults toGET.POST - (Optional): Custom key-value pairs for the request.
headers - (Conditional): The request body template. Required for all content types except
payloadTemplate. Forapplication/json, you may omit it to forward the full event payload as-is.application/json
For full webhook configuration and template syntax, refer to Webhooks.{ "name" : "example-webhook-trigger", "eventType" : "com.unity.services.leaderboards.score-submitted.v1", "actionUrn" : "urn:ugs:webhook", "actionType" : "webhook", "webhook": { "url": "https://example.com/events", "method": "POST", "headers": { "Content-Type": "application/json" }, "payloadTemplate": "{\"event\": \"score-submitted\", \"data\": {{.}}}" }}
Overlap with events
Triggers are associated with events. When an event is fired, the associated trigger is invoked. The event and the trigger must share the sameeventNamepayloadVersionScheduled events
Given the following schedule configuration:And the following trigger configuration:{ "name" : "recurring-schedule", "eventName": "example-event", "type": "recurring", "schedule": "0 0 * * *", "payloadVersion": 1, "payload": "{\"someBoolean\": true, \"someString\": \"something\"}"}
You can associate the trigger with the event type by setting the{ "name" : "example-trigger", "eventType" : "com.unity.services.scheduler.example-event.v1", "actionUrn" : "urn:ugs:cloud-code:my-script", "actionType" : "cloud-code"}
eventTypeeventNamepayloadVersionUGS events
Given you want to associate a trigger with an event type from UGS, you need to specify the event type in the trigger configuration. For example, if you want to fire a trigger on the Authentication: Signed Up event, you can create the following trigger configuration:Every time a user signs up, the trigger is fired, invoking the{ "name" : "example-trigger", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:my-script", "actionType" : "cloud-code"}
my-scriptMultiple events per trigger
You can associate the same event with multiple triggers.
For example, you can create two triggers that invoke when a user signs up.
The first trigger invokes the
add-cloud-save-dataThe second trigger invokes the{ "name" : "auth-cloud-save", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:add-cloud-save-data", "actionType" : "cloud-code"}
add-economy-itemsEconomyEvery time a user signs up, both triggers fire and invoke the associated Cloud Code resources.{ "name" : "auth-economy", "eventType" : "com.unity.services.player-auth.signed-up.v1", "actionUrn" : "urn:ugs:cloud-code:Economy/add-economy-items", "actionType" : "cloud-code"}