# HTTP API

> Manage trigger configurations using raw HTTP APIs and the Triggers Admin API.

您可以使用原始 HTTP API 来管理触发器配置。

## 使用 API##using-the-api

[Triggers Admin API](https://services.docs.unity.com/triggers-admin/v1/) 文档包含管理操作（例如创建、读取和删除触发器）的详细描述。

文档中包含有关身份验证、终端以及请求和响应格式的信息，并提供相关示例。您可以采用 OpenAPI 格式下载它们，并参考它们来设置您自己的自动化过程。

要使用 API，请使用[服务帐户](../../authentication)进行身份验证。

### Authorization 标头##authorization-header

要对请求进行身份验证，请使用[基本身份验证](../../authentication#authenticating-trusted-clients-basic-authentication)。创建一个服务帐户，对 `<KEY_ID>:<SECRET_KEY>` 进行 base64 编码，然后将其用作 `Authorization` 标头的值。

```text
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
```

### 部署触发器##deploy-triggers

您可以通过发送以下请求来设置触发器配置：

```bash
curl 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
--data '{
  "name": "example-trigger-name",
  "eventType": "com.unity.services.scheduler.example-event.v1",
  "actionType": "cloud-code",
  "actionUrn": "urn:ugs:cloud-code:MyTestScript"
  "filter": "data[\"parameter\"] == \"value\""
}'
```

成功的响应包含有关触发器配置的信息。例如：

```json
{
  "id": "12675a97-5166-4a7e-bee4-6c3a856026a8",
  "createdAt": "2023-08-25T14:01:16Z",
  "updatedAt": "2023-08-25T14:01:16Z",
  "name": "example-trigger-name",
  "projectId": "0ea7b997-b8bc-4125-97f9-e082e0cbf355",
  "environmentId": "de4504b7-70ca-475a-8862-00b5150f1d56",
  "eventType": "com.unity.services.scheduler.example-event.v1",
  "actionType": "cloud-code",
  "actionUrn": "urn:ugs:cloud-code:MyTestScript",
  "filter": "data['parameter'] == 'value'"
}
```

* `id`：触发器配置 ID。
* `createdAt`：创建触发器配置的日期和时间。
* `updatedAt`：上次更新触发器配置的日期和时间。
* `name`：触发器配置名称。
* `projectId`：关联的 Project ID。
* `environmentId`：关联的环境 ID。
* `eventType`：触发器配置触发的事件类型。
* `actionType`：触发器触发时要执行的操作的类型。目前仅支持 `cloud-code`。
* `actionUrn`：定义触发器触发时应调用的内容。例如，`urn:ugs:cloud-code:TestScript` 调用 `TestScript` Cloud Code 脚本，或者 `urn:ugs:cloud-code:TestModule/TestFunction` 调用 `TestModule` Cloud Code 模块中的 `TestFunction` 函数。
* `filter`：可选字段，定义触发器触发所必须满足的条件。如果条件不满足，触发器就不会触发。如需了解更多信息，请参阅[过滤器](/triggers/tutorials/define-triggers/filters.md)。

您可以使用 `id` 删除触发器。

如果您已将触发器与现有 Cloud Code 脚本或模块相关联，则发出指定 `eventType` 的事件会使触发器触发，进而执行关联的 Cloud Code 脚本或模块。

### 获取触发器##retrieve-triggers

要检查触发器配置，请发送以下请求：

```bash
curl 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs/<CONFIG_ID>' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
```

响应可能如下所示：

```json
{
  "id": "ad06a027-d0e6-47d9-bf84-ad7d20ed5a66",
  "createdAt": "2024-07-30T15:59:08Z",
  "updatedAt": "2024-07-30T15:59:08Z",
  "name": "test-leaderboard-trigger",
  "projectId": "0ea7b997-b8bc-4125-97f9-e082e0cbf355",
  "environmentId": "c9acf5d7-d53b-4bc7-b622-7b63c1ee6dc7",
  "eventType": "com.unity.services.leaderboards.score-submitted.v1",
  "actionType": "cloud-code",
  "actionUrn": "urn:ugs:cloud-code:test-score-script",
  "filter": "data['score'] > 1000 || data['score'] > 56"
}
```

要预览已部署的所有触发器，请发送以下请求：

```bash
curl 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
```

响应可能如下所示：

```json
{
  "configs": [
    {
      "id": "c5cc255e-93e5-4ac8-9b9a-f16a94892fff",
      "createdAt": "2023-08-25T14:07:56Z",
      "updatedAt": "2023-08-25T14:07:56Z",
      "name": "example-trigger-name-3",
      "eventType": "com.unity.services.scheduler.other-event.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:TestModule/TestEndpoint"
    },
    {
      "id": "acf63347-4523-427b-937b-d73943d542dd",
      "createdAt": "2023-08-25T14:07:31Z",
      "updatedAt": "2023-08-25T14:07:31Z",
      "name": "example-trigger-name-2",
      "eventType": "com.unity.services.authentication.signed-up.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:TestModule/TestEndpoint"
    },
    {
      "id": "12675a97-5166-4a7e-bee4-6c3a856026a8",
      "createdAt": "2023-08-25T14:01:16Z",
      "updatedAt": "2023-08-25T14:01:16Z",
      "name": "example-trigger-name",
      "eventType": "com.unity.services.scheduler.example-event.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:MyTestScript",
      "filter": "data['parameter'] == 'value'"
    }
  ],
  "limit": 100,
  "after": ""
}
```

您还可以通过设置查询参数来使用分页。例如，要获取前两个配置，请发送以下请求：

```bash
curl 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs?limit=2' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
```

然后，响应将如下所示：

```json
{
  "configs": [
    {
      "id": "c5cc255e-93e5-4ac8-9b9a-f16a94892fff",
      "createdAt": "2023-08-25T14:07:56Z",
      "updatedAt": "2023-08-25T14:07:56Z",
      "name": "example-trigger-name-3",
      "eventType": "com.unity.services.scheduler.other-event.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:TestModule/TestEndpoint"
    },
    {
      "id": "acf63347-4523-427b-937b-d73943d542dd",
      "createdAt": "2023-08-25T14:07:31Z",
      "updatedAt": "2023-08-25T14:07:31Z",
      "name": "example-trigger-name-2",
      "eventType": "com.unity.services.scheduler.another-event.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:TestModule/TestEndpoint"
    }
  ],
  "limit": 2,
  "after": "<TOKEN>"
}
```

使用 `after` 令牌，您可以获得下一页结果。例如：

```bash
curl 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs?limit=2&after=<TOKEN>' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
```

返回最后一个触发器：

```json
{
  "configs": [
    {
      "id": "12675a97-5166-4a7e-bee4-6c3a856026a8",
      "createdAt": "2023-08-25T14:01:16Z",
      "updatedAt": "2023-08-25T14:01:16Z",
      "name": "example-trigger-name",
      "eventType": "com.unity.services.scheduler.example-event.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:MyTestScript",
      "filter": "data['parameter'] == 'value'"
    }
  ],
  "limit": 2,
  "after": ""
}
```

### 删除触发器##delete-triggers

您可以通过发送以下请求来删除触发器配置：

```bash
curl --request DELETE 'https://services.api.unity.com/triggers/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/configs/<CONFIG_ID>' \
--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
```

空的响应表示删除成功。
