Autogenerated SDK

How to use Beamable's auto generated SDK

🚧

Advanced Feature

This documentation describes an advanced capability of the Beamable SDK.

Autogenerated Beamable SDK

Beamable publishes a set of OpenAPI documents, here. These documents are used to generate C# classes and methods inside the Beamable SDK. This SDK can be accessed through the IDependencyProvider available via a BeamContext instance.

The data structures used for request and responses are json serializable by Unity, which means they can be used in the Unity Inspector for visibility. As new Beamable server features are released, the autogenerated SDK will capture the latest features.

Accessing the SDK

The autogenerated SDK can be accessed via the ServiceProvider on any BeamContext instance. The SDK is a set of interface types for each Beamable service. All interfaces and model types are under the Beamable.Api.Autogenerated namespace.

using Beamable;
using Beamable.Api.Autogenerated.Inventory;
using Beamable.Api.Autogenerated.Models;
using Beamable.Common;
using UnityEngine;

public class Example : MonoBehaviour
{
  async void Start()
  {
    // access a context,
    var ctx = BeamContext.Default;
    
    // and then get any autogeneratd sdk interface
    var inventoryApi = ctx.ServiceProvider.GetService<IInventoryApi>();
  } 
}

The available autogenerated SDKs can be found in Packages/com.beamable/Common/Runtime/OpenApiSystem.cs, which as of Beamable 1.4 looked like this.

namespace Beamable.Api.Autogenerated
{
	public class OpenApiRegistration
	{
		public static void RegisterOpenApis(IDependencyBuilder builder)
		{
			builder.AddSingleton<IAccountsApi, AccountsApi>();
			builder.AddSingleton<IAnnouncementsApi, AnnouncementsApi>();
			builder.AddSingleton<IAuthApi, AuthApi>();
			builder.AddSingleton<IBeamoApi, BeamoApi>();
			builder.AddSingleton<ICalendarsApi, CalendarsApi>();
			builder.AddSingleton<IChatv2Api, Chatv2Api>();
			builder.AddSingleton<ICloudsavingApi, CloudsavingApi>();
			builder.AddSingleton<ICommerceApi, CommerceApi>();
			builder.AddSingleton<IEventPlayersApi, EventPlayersApi>();
			builder.AddSingleton<IEventsApi, EventsApi>();
			builder.AddSingleton<IGroupsApi, GroupsApi>();
			builder.AddSingleton<IGroupUsersApi, GroupUsersApi>();
			builder.AddSingleton<IInventoryApi, InventoryApi>();
			builder.AddSingleton<ILeaderboardsApi, LeaderboardsApi>();
			builder.AddSingleton<IMailApi, MailApi>();
			builder.AddSingleton<IMatchmakingApi, MatchmakingApi>();
			builder.AddSingleton<INotificationApi, NotificationApi>();
			builder.AddSingleton<IPaymentsApi, PaymentsApi>();
			builder.AddSingleton<IPushApi, PushApi>();
			builder.AddSingleton<IRealmsApi, RealmsApi>();
			builder.AddSingleton<ISocialApi, SocialApi>();
			builder.AddSingleton<IStatsApi, StatsApi>();
			builder.AddSingleton<ITournamentsApi, TournamentsApi>();
		}
	}
}

Authorization

Most Beamable APIs require user authorization. The generated SDK functions for these APIs won't include any authorization options, because authorization is required. Every request sent through the auto generated SDK will use the authorization from the associated BeamContext.

However, some Beamable APIs do not require user authorization. If authorization isn't required, the SDK function will include a boolean parameter, includeAuthHeader. The parameter will always default to true, but it is possible to override the default behavior and pass in false. Sending a request without an authorization header will have different effects depending on the SDK being executed.

Examples

In this example, the IInventoryApi is being used to set the player's inventory and then fetch the latest inventory state. The InventoryUpdateRequest and InventoryView are public class members to illustrate that they can be visualized in the Inspector.

using Beamable;
using Beamable.Api.Autogenerated.Inventory;
using Beamable.Api.Autogenerated.Models;
using Beamable.Common;
using UnityEngine;

public class Tester : MonoBehaviour
{
	public InventoryUpdateRequest inventoryUpdateRequest;
	public InventoryView inventoryView;
	
	[ContextMenu("Set Inventory")]
	public async Promise SetInventory()
	{
		var ctx = BeamContext.Default;
		await ctx.OnReady;

		var inventory = ctx.ServiceProvider.GetService<IInventoryApi>();

		await inventory.ObjectPut(ctx.PlayerId, inventoryUpdateRequest);
		inventoryView = await inventory.ObjectGet(ctx.PlayerId);
	}

}

📘

Optional fields for Request Objects

Many request objects, including InventoryUpdateRequest will include optional fields. In the inspector, there is a check-box on the right side of each optional field. Make sure to click the checkbox so you can edit the actual value of the field.