Documentation

Support

Call to Cloud Code API

Invoke module endpoints using HTTP requests to the Cloud Code REST API.
Read time 2 minutesLast updated a day ago

Call out to the Cloud Code API to run your module endpoints. Refer to Cloud Code API documentation for more information.

Authentication

Refer to Authentication to authenticate as a player or a trusted client using a service-account or a stateless token. Use the received token as a bearer token for HTTP authentication in the request header:
Authorization: Bearer <BEARER_TOKEN>

Run module endpoint request

Call out to the Cloud Code API by sending a POST request to the following URL:
https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>
Define the following path parameters:
  • PROJECT_ID
    : The ID of the project.
  • MODULE_NAME
    : The name of the module.
  • FUNCTION_NAME
    : The name of the module endpoint. This is the
    CloudCodeFunction
    you defined in your code.
The following is an example of a simple Curl command:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer <BEARER_TOKEN>'

Request payload

You can define parameters in your module function inside the method signature. Refer to module structure on how to structure your module functions. For example, the following method defines the string parameters
weapon
and
bodyPart
:
[CloudCodeFunction("IntroduceGuard")]public string IntroduceGuard(string weapon, string bodyPart){return $"I used to be an adventurer like you. Then I took an ${weapon} to the ${bodyPart}.";}
The request body passes these parameters as JSON when you call the endpoint. The casing of the parameter must match the method signature.
{ "params": { "weapon": "arrow", "bodyPart": "knee" }}
The following is an example of a complete Curl command:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer <BEARER_TOKEN>' \--data-raw '{ "params": { "weapon": "arrow", "bodyPart": "knee" }}'

Scoped function payload

Include a
scope
object in the request body to run the function within a scope, and to keep a persistent state across calls that use the same scope ID. Omit
scope
to run the function statelessly.
The scope object has the following fields:
  • type
    : The scope type. Supported value:
    MultiplayerSession
    .
  • id
    : Provide a UUID for the scope ID. Use the same UUID to reuse state across calls. Use a new UUID to start with a fresh state.
The following is an example of a scoped request body:
{ "params": { "weapon": "arrow", "bodyPart": "knee" }, "scope": { "type": "MultiplayerSession", "id": "a379af6c-4fac-4449-b6c7-925ee87b446e" }}
The following is an example of a scoped Curl command:
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer <BEARER_TOKEN>' \--data-raw '{ "params": { "weapon": "arrow", "bodyPart": "knee" }, "scope": { "type": "MultiplayerSession", "id": "a379af6c-4fac-4449-b6c7-925ee87b446e" }}'

Sample output

The following shows the example output of a module.
{ "output": "I used to be an adventurer like you. Then I took an arrow to the knee."}