# Cloud Code API の呼び出し

> Invoke scripts by making HTTP requests to the Cloud Code REST API.

Cloud Code API を呼び出して、スクリプトを実行します。詳細については、[Cloud Code API のドキュメント](https://services.docs.unity.com/cloud-code/v1/index.html#tag/cloud-code/operation/runscript) を参照してください。

## 認証##authentication

サービスアカウントまたはステートレストークンを使用してプレイヤーまたは信頼されているクライアントとして認証するには、[認証](../authentication) を参照してください。受信したトークンを Bearer トークンとしてリクエストヘッダーで HTTP 認証に使用します。

```bash
Authorization: Bearer <BEARER_TOKEN>
```

## スクリプトリクエストの実行##run-script-request

POST リクエストを以下の宛先に送信することで、Cloud Code API を呼び出します。

```http request
https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>
```

パスパラメーターを定義します。

* `projectId` - プロジェクトの ID。
* `scriptName` - スクリプトの名前。

単純な cURL コマンドは以下のようになります。

```bash
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BEARER_TOKEN>'
```

## リクエストペイロード##request-payload

スクリプト内でパラメーターを定義できます。これらのパラメーターは、リクエスト本文内で渡されます。スクリプトにパラメーターがない場合は、空の `params` オブジェクトを渡すことができます。

```json
{
  "params": {
    "numericParam": 123,
    "stringParam": "abcdef",
    "booleanParam": true,
    "jsonParam": {
      "key": "value"
    }
  }
}
```

完全な cURL コマンドは以下のようになります。

```bash
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BEARER_TOKEN>' \
--data-raw '{
"params": {
  "numericParam": 123,
  "stringParam": "abcdef",
  "booleanParam": true,
  "jsonParam": {
    "key": "value"
  }
}
}'
```

## サンプル出力##sample-output

これは、`playerInfo` オブジェクトを返すスクリプトの出力例です。

```json
{
  "output": {
    "playerInfo": {
      "playerID": "TestUser",
      "userName": "TestUserName",
      "displayName": "Name",
      "email": "test@test.com",
      "country": null,
      "deviceType": null,
      "platform": "IOS",
      "dateCreated": "string"
    }
  }
}
```
