HTTP API

Raw HTTP API を使用して、トリガー設定を管理できます。

API の使用

Triggers Admin API のドキュメントには、トリガーの作成、読み取り、削除などの管理操作の詳細な説明が記載されています。

ドキュメントには、認証、エンドポイント、およびリクエストとレスポンスの形式に関する情報が、例とともに記載されています。これらを OpenAPI 形式でダウンロードし、独自の自動化を設定するために使用できます。

API を使用するには、サービスアカウント を使用して認証します。

Authorization ヘッダー

リクエストを認証するには、Basic 認証 を使用します。サービスアカウントと base64 エンコード <KEY_ID>:<SECRET_KEY> を作成し、それを Authorization ヘッダーの値として使用します。

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

トリガーのデプロイ

以下のリクエストを送信することで、トリガー設定を設定できます。

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\""
}'

正常なレスポンスには、トリガー設定に関する情報が含まれます。例を次に示します。

{
  "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: 関連付けられているプロジェクト ID。
  • environmentId: 関連付けられている環境 ID。
  • eventType: トリガー設定をトリガーするイベントタイプ。
  • actionType: トリガーが発生したときに実行されるアクションのタイプ。現在、cloud-code のみがサポートされています。
  • actionUrn: トリガーが発生したときに呼び出す必要のあるものを定義します。例えば、urn:ugs:cloud-code:TestScriptTestScript Cloud Code スクリプトを呼び出し、urn:ugs:cloud-code:TestModule/TestFunctionTestModule Cloud Code モジュール内の TestFunction 関数を呼び出します。
  • filter: トリガーが起動するために満たす必要がある条件を定義する任意のフィールド。条件が満たされない場合、トリガーは起動しません。詳細については、フィルター を参照してください。

id を使用して、トリガーを削除できます。

トリガーを既存の Cloud Code スクリプトまたはモジュールに関連付けている場合、eventType を指定してイベントを発行するとトリガーが発生し、関連付けられている Cloud Code スクリプトまたはモジュールが実行されます。

トリガーの取得

トリガーの設定を調べるには、以下のリクエストを送信します。

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

レスポンスは以下のようになります:

{
  "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"
}

デプロイしたすべてのトリガーをプレビューするには、以下のリクエストを送信します。

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

レスポンスは以下のようになります:

{
  "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": ""
}

クエリパラメーターを設定してページ処理を使用することもできます。例えば、最初の 2 つの設定を取得するには、以下のリクエストを送信します。

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

レスポンスは以下のようになります。

{
  "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 トークンを使用して、次の結果ページを取得できます。例を次に示します。

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>'

最後のトリガーが返されます。

{
  "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": ""
}

トリガーの削除

以下のリクエストを送信することで、トリガー設定を削除できます。

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>'

空のレスポンスは削除に成功したことを示します。