Call to Cloud Code API

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

Note: If a module has not received any traffic in the last 15 minutes, you may experience a cold start latency. Any subsequent calls to the module are faster.

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:

https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>

Define the 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.

A simple cURL command could look like this:

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}.";
}

These parameters are passed in the request body as JSON when you call the endpoint. The casing of the parameter must match the method signature.

{
  "params": {
    "weapon": "arrow",
    "bodyPart": "knee"
  }
}

A complete cURL command could look like this:

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

Sample output

This is an example output of a module.

{
    "output": "I used to be an adventurer like you. Then I took an arrow to the knee."
}