HTTP API
Manage Cloud Code scripts using raw HTTP APIs and the Admin API.
読み終わるまでの所要時間 3 分最終更新 23日前
Raw HTTP API を使用して、Cloud Code スクリプトを管理できます。
API の使用
- Admin API のドキュメントには、スクリプトの作成、読み取り、更新、削除などの管理操作の詳細な説明が記載されています。
- Client API のドキュメントには、スクリプトの実行などのクライアント側操作の詳細な説明が記載されています。
Authorization ヘッダー
リクエストを認証するには、Basic 認証) を使用します。サービスアカウントと base64 エンコード<KEY_ID>:<SECRET_KEY>Authorization--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
スクリプトのデプロイ
以下のリクエストを送信することで、スクリプトを作成できます。スクリプトをゲームクライアントから使用可能にするには、それを公開する必要があります。curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \--data '{ "name": "test-script", "type": "API", "code": "module.exports = () => {return \"Result from standalone API script\";}", "language": "JS"}'
公開するたびに、スクリプトバージョンが増加します。正常なレスポンスは以下のようになります。curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
最後の公開以降に変更されていないスクリプトは公開できません。{ "version": 1, "datePublished": "2023-06-23T10:39:20Z"}
スクリプトの更新
以下のリクエストを送信することで、スクリプトコード、パラメーター、またはその両方を更新できます。スクリプトの更新されたバージョンをゲームクライアントから使用可能にするには、それを再度公開する必要があります。この結果として、バージョン 2 が作成されます。curl --request PATCH 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \--data '{ "params": [ { "name": "parameter", "type": "STRING", "required": false } ], "code": "module.exports = () => {return \"Updated result from standalone API script\";}"}'
{ "version": 2, "datePublished": "2023-06-23T10:40:00Z"}
スクリプトのロールバック
スクリプトを公開し、新しいバージョンが気に入らない場合は、バージョン番号を指定してリクエストを送信することで、前のバージョンにロールバックできます。これで、最初のスクリプトバージョンがバージョン 3 として再公開されます。curl --request POST 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>/publish' \--header 'Content-Type: application/json' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'--data '{ "version": 1}'
{ "version": 3, "datePublished": "2023-06-23T10:50:00Z"}
スクリプトの取得
スクリプトのコンテンツを確認するには、以下のリクエストを送信します。レスポンスは以下のようになります:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
レスポンスには以下の情報が含まれます。{ "name": "test", "type": "API", "language": "JS", "activeScript": { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "version": 2, "datePublished": "2021-07-23T16:08:35Z" }, "versions": [ { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": true, "version": null, "dateUpdated": "2021-07-23T15:59:32Z" }, { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": false, "version": 2, "dateUpdated": "2021-07-23T16:08:35Z", "dateCreated": "2021-07-23T16:08:35Z" }, { "code": "module.exports=async (cloudCode) =>{cloudCode.logger.info('this message confirms that the logging client is functional!'); return cloudCode.params.someThing;}", "params": [ { "name": "someThing", "type": "STRING", "required": true } ], "isDraft": false, "version": 1, "dateUpdated": "2021-07-23T15:59:58Z", "dateCreated": "2021-07-23T15:59:58Z" } ], "params": [ { "name": "someThing", "type": "STRING", "required": true } ]}
- : スクリプトの名前。
name - : スクリプトのタイプ。Cloud Code スクリプトでは、API スクリプトのみがサポートされます。
type - : スクリプトの言語。Cloud Code スクリプトでは、JavaScript のみがサポートされます。
language
activeScriptversionsisDrafttrueversionリストのレスポンスは以下のようになります。curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
レスポンスには、作成したすべてのスクリプトの概要が含まれます。ページ区切りを使用するには、クエリパラメーターを使用して結果のスコープを絞り込み、結果の次のページへのリンクを取得します。{ "results": [ { "name": "script", "language": "JS", "type": "API", "published": true, "lastPublishedDate": "2022-03-08T12:37:48Z", "lastPublishedVersion": 40 }, { "name": "test", "language": "JS", "type": "API", "published": true, "lastPublishedDate": "2023-06-20T15:14:58Z", "lastPublishedVersion": 4 } ], "links": { "next": null }}
次のリンクは以下のようになります。curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?limit=2' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
{ "links": { "next": "/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts?after=<SCRIPT_NAME>" }}
スクリプトの削除
スクリプトを削除するには、以下のリクエストを送信します。空のレスポンスは削除に成功したことを示します。curl --request DELETE 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts/<SCRIPT_NAME>' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'