How can I set an environment variable in my C# module and does it saves?

I'm writing C# module for cloud code to get data about other players in my game. To do this, I need to use Service account. I'd like to store my key and secret in environment/gitignored script to secure my module. So here is the question: can I make environment variables, and will it work?

Hi Aheront,

Where is the data located for the other players in your game? You do not need to use Service Accounts to access any other UGS products, because Cloud Code has a built in feature that supports this. If you are trying to get or set data in Cloud Save, you can authenticate the call with the ServiceToken attribute of the context object at runtime. This provides similar permissions to service accounts, but you would not need to ever store the credentials anywhere.

Here is a code example of what that would look like:

using Unity.Services.CloudCode.Apis;
using Unity.Services.CloudCode.Core;
using Unity.Services.CloudCode.Shared;
using Unity.Services.CloudSave.Model;

namespace ExampleModule;

public class CloudSaveSDKSample
{
    [CloudCodeFunction("SaveData")]
    public async void SaveData(IExecutionContext context, IGameApiClient gameApiClient, string key, object value)
    {
        try
        {
            await gameApiClient.CloudSaveData.SetItemAsync(context, context.ServiceToken, context.ProjectId, context.PlayerId, new SetItemBody(key, value));
        }
        catch (ApiException ex)
        {
            throw;
        }
    }

    [CloudCodeFunction("GetData")]
    public async Task<object> GetData(IExecutionContext context, IGameApiClient gameApiClient, string key)
    {
        try
        {
            var result = await gameApiClient.CloudSaveData.GetItemsAsync(context, context.ServiceToken, context.ProjectId, context.PlayerId, new List<string> {key});
            return result.Data.Results.First().Value;
        }
        catch (ApiException ex)
        {
            throw;
        }
    }
}

public class ModuleConfig : ICloudCodeSetup
{
    public void Setup(ICloudCodeConfig config)
    {
        config.Dependencies.AddSingleton(GameApiClient.Create());
    }
}

Notice the "context.ServiceToken" used in the call to Cloud Save.

In a service external to UGS, you could verify Cloud Code service tokens by using the public keys, which are located at https://cloud-code.services.api.unity.com/.well-known/jwks.json - for more information on how to validate JWTs, have a look at https://jwt.io/. Just to be clear, if you are using UGS products, the Cloud Code service tokens are supported in other UGS products and you would never need to validate these tokens yourself when calling UGS services.

Finally you might notice that the latest version of the Com.Unity.Services.CloudCode.Apis package supports the Cloud Save Custom Data feature when accessed from Cloud Code - here are the API documentation for that if you'd like to explore custom data in more detail - https://services.docs.unity.com/cloud-save/v1/#tag/Data/operation/setCustomItem.

Let me know if that helps,
Francois

1 Like