Call from Unity game server (Multiplay)

You can call a script from a dedicated game server using Game Server Hosting (Multiplay).

Prerequisites

  1. The game server you are invoking Cloud Code from and any related resources must exist in the same Unity Cloud Dashboard project and environment.
  2. Onboard with Game Server Hosting (Multiplay).

Call out to Cloud Code

Follow the steps below to call out to a Cloud Code script from a game server.

Authentication

You can authenticate requests using a Game Server Hosting (Multiplay) token or a stateless token. Use the received token as a bearer token for HTTP authentication in the request header.

Authenticating with a Multiplay token is recommended. Authenticating with a stateless tokens requires you to create a service account and store the private key securely, whereas Multiplay tokens do not require this and are less complex to manage.

Authenticate using a Game Server Hosting (Multiplay) token

You can retrieve a Game Server Hosting (Multiplay) token by running the following request on the game server:

curl -X GET http://localhost:8086/v4/token

The request would return a response in the format below:

{"token":"<BEARER_TOKEN>", "error":""}

Authenticate using a stateless token

To use a stateless token, you need to create a service account and call the Token Exchange API. Refer to authenticating trusted clients for the Cloud Code Client API.

Call a script

You can call the script using any HTTP library that is native to the codebase. Use the retrieved authentication token as a bearer token for HTTP authentication in the request header. An example CURL request to call a script using the Cloud Code API could look like this:

curl -X POST -H "Authorization: Bearer <BEARER_TOKEN>" 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/scripts/<SCRIPT_NAME>'