HTTP APIs
Manage Cloud Code scripts using raw HTTP APIs and the Admin API.
Read time 3 minutesLast updated a day ago
You can use the raw HTTP APIs to manage Cloud Code scripts.
Using the API
- The Admin API documentation contains a detailed description of admin operations, such as creating, reading, updating, and deleting scripts.
- The Client API documentation contains a detailed description of client-facing operations, such as running a script.
Authorization header
To authenticate the requests, use Basic Authentication). Create a service account and base64 encode the<KEY_ID>:<SECRET_KEY>Authorization--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>' \
Deploy scripts
You can create a script by sending the following request:To make the script available to the game client, you have to publish it: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"}'
Every time you publish, the script version increments. A successful response could look as follows: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>'
You are not able to publish a script that has no changes since the last publish.{ "version": 1, "datePublished": "2023-06-23T10:39:20Z"}
Update scripts
You can update the script code, parameters or both by sending the following request:To make the updated version of the script available to the game client, you have to publish it again, resulting in creating version 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"}
Rollback scripts
If you published a script, and you are not happy with the new version, you can roll back to the previous version by sending a request with a specified version number:Now the first script version is republished as version 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"}
Retrieve scripts
To check the contents of your script, you can send the following request:The response could look as follows: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>'
The response contains the following information:{ "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 } ]}
- : the name of the script.
name - : the type of the script. Cloud Code scripts only support API scripts.
type - : the language of the script. Cloud Code scripts only support JavaScript.
language
activeScriptversionsisDrafttrueversionThe list response could look as follows:curl 'https://services.api.unity.com/cloud-code/v1/projects/<PROJECT_ID>/environments/<ENVIRONMENT_ID>/scripts' \--header 'Authorization: Basic <SERVICE_ACCOUNT_CREDENTIALS_ENCODED>'
The response contains a quick overview of all the scripts you have created. To use paging, you can use query parameters to narrow down the scope of your results and obtain a link to the next page of results.{ "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 }}
The next link could look as follows: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>" }}
Delete script
To delete a script, you can send the following request:An empty response indicates a successful deletion.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>'