Authentication
Understand the authentication methods available for accessing Multiplay Hosting APIs and services.
Read time 3 minutesLast updated 3 days ago
The methods you must use for authentication vary slightly across the Multiplay Hosting service.
| Service | Authentication Type |
Unity Services Gateway | Basic authentication with service account keys. |
Unity Game Gateway | Bearer token from token exchange. |
Server Authentication
Server Authentication is associated with the server, and can be obtained when running on a Multiplay Hosting machine. It can be used for the Gaming Gateway. You can retrieve a Multiplay Hosting token in the following ways:- Scripting API with Authentication package
- Manually through a local request
Scripting API with Authentication package
Using the following call from the Authentication package:You can also authenticate using a service account:await ServerAuthenticationService.Instance.SignInFromServerAsync();var token = ServerAuthenticationService.Instance.AccessToken;
await ServerAuthenticationService.Instance.SignInWithServiceAccountAsync(apiKeyIdentifier, apiKeySecret);
Refer to service accounts.
Manually obtaining the token
The token may also be obtain manually through a local request:The request would return a response in the format below:curl -X GET http://localhost:8086/v4/token
{"token":"<BEARER_TOKEN>", "error":""}
Service accounts
All API authentication require service accounts. If you don't have a service account with a role appropriate for the request you intend to perform, refer to Create a service account. Service Accounts and authentication are mainly for administrative privileges. Server Authentication are instead of trusted-game services.Unity Services Gateway
Use the Unity Services Gateway (USG) to authenticate administrative APIs, such as managing builds, build configurations, fleets, and other Multiplay Hosting resources. Use basic authentication to authenticate the API call.This means that you create a string that has the KeyID and Secret key separated by a colon, then base64 encode it. Many HTTP libraries and tools have built-in support for basic authentication. In this case you can use KeyID as the username and Secret key as the password.Authorization: Basic <base64(keyID:keySecret)>
Example (list builds)
Here is an example of creating the header and using it in a curl request to list builds:| Key ID | Secret key |
| |
Which can now be used in an authorization header as follows:OTI1MGY1NzgtOWZmMS00Yjc1LWFmY2MtN2VjYTFlOTRkYjU2OjVkN2YxYTY2LWYyOWQtNDVjOC1hNmFhLWE4NDI0MmFhODA1Zg==
For more information about this endpoint, and parameters used such ascurl -X GET 'https://services.api.unity.com/multiplay/builds/v1/projects/<projectID>/environments/<environmentID>/builds \--header 'Authorization: Basic OTI1MGY1NzgtOWZmMS00Yjc1LWFmY2MtN2VjYTFlOTRkYjU2OjVkN2YxYTY2LWYyOWQtNDVjOC1hNmFhLWE4NDI0MmFhODA1Zg=='
projectIDenvironmentIDUnity Game Gateway
The Unity Game Gateway uses a bearer token for authentication. To get a bearer token, you must first use your service account to request a time limited token from the Unity Game Gateway.- Token exchange to retrieve an access token. Refer to Authentication API documentation to learn more.
- Use the Access Token with bearer authentication to make a request to Unity Game Gateway endpoints
- The recommended best practice is to reuse the token; however, it expires after one hour, so you must refresh it before it expires.
Example (list allocations)
This example shows how to use make a request to the list allocations endpoint. This consists of two steps:- Token exchange to retrieve an access token. Refer to Authentication API documentation to learn more.
- Use the Access Token with bearer authentication to make a request to the list allocations endpoint.
And receive the response:# Get the token from the token exchange endpointcurl -X POST 'https://services.api.unity.com/auth/v1/token-exchange?projectId=<projectID>&environmentId=<environmentID>' \--header 'Authorization: Basic <base64(keyID:keySecret)>' \--header 'Content-Type: application/json' \--data-raw '{ "scopes": []}'
The string{ "accessToken": <accessToken>}
accessTokenFor more information about this endpoint, and parameters used such as# Make the list allocations requestcurl -X GET 'https://multiplay.services.api.unity.com/v1/allocations/projects/<projectID>/environments/<environmentID>/fleets/<fleetID>/allocations \--header 'Authorization: Bearer <accessToken>'
projectIDenvironmentIDfleetID