Call to Cloud Code API
Invoke module endpoints using HTTP requests to the Cloud Code REST API.
Read time 1 minuteLast updated 4 hours 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:Define the following path parameters:https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/<MODULE_NAME>/<FUNCTION_NAME>
- : The ID of the project.
PROJECT_ID - : The name of the module.
MODULE_NAME - : The name of the module endpoint. This is the
FUNCTION_NAMEyou defined in your code.CloudCodeFunction
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 parametersweaponbodyPartThe request body passes these parameters as JSON when you call the endpoint. The casing of the parameter must match the method signature.[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 following is an example of a complete Curl command:{ "params": { "weapon": "arrow", "bodyPart": "knee" }}
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
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."}