SDK¶
A C# client library in sdk/, shaped like PlayFab's client SDK, so a game
(the Fabled plugin) never hand-rolls HTTP calls or JSON against this API. It
stands in for the future Unity SDK. It is written so it can go into a Unity
project unchanged, and the Simulator is its first user.
Using it¶
Like PlayFab, there are two ways in.
The static API, one player per process. This is what a game uses, and the callback form is what Unity code expects:
using Hakutaku;
using Hakutaku.ClientModels;
HakutakuSettings.staticSettings.ServerUrl = "https://51.79.242.169.nip.io";
HakutakuClientAPI.LoginWithDeviceToken(
new LoginWithDeviceTokenRequest { DeviceToken = storedDeviceToken },
result => Debug.Log("Logged in as " + result.PlayerId),
error => Debug.LogError(error.GenerateErrorReport()));
An instance per player, for anything that holds several players at once,
such as the simulator or a dedicated server. Every call also has an …Async
form that returns a result instead of taking callbacks:
var client = new HakutakuClientInstanceAPI(new HakutakuApiSettings { ServerUrl = "http://localhost:5008" });
var login = await client.LoginWithDeviceTokenAsync(new LoginWithDeviceTokenRequest { DeviceToken = storedDeviceToken });
if (login.Error != null)
Console.WriteLine(login.Error.GenerateErrorReport());
else
Console.WriteLine(login.Result.PlayerId);
No call throws on an HTTP or network failure. Every result has exactly one of
Result and Error set.
The launch flow¶
The SDK doesn't store anything. The game keeps two things between launches: the
device token, while the player is a guest, and the session, valid 30
days. The session is all three of PlayerId, SessionTicket and
SessionExpiration from the authentication context. On each launch:
- A saved session that hasn't expired? Put all three back into
HakutakuSettings.staticPlayer, and you're signed in with no network call. Restoring the ticket without thePlayerIdlooks fine at first:IsClientLoggedIn()only checks the ticket. But calls that send the player's id, such asWritePlayerEvent, would then send none. - Otherwise, a saved device token? Call
LoginWithDeviceToken. If it fails withNotAuthenticated, the token is gone: go to step 3. Don't register a new guest automatically. That silently replaces the player's account with an empty one, the classic "I reinstalled and lost everything". - Otherwise, show a title screen with Start, which calls
RegisterGuestand savesLoginResult.DeviceToken, and I have an account, which callsLoginWithEmailAddress.
And two moments to handle:
VerifyEmailreturnsDeviceTokenRemoved = true: delete the stored device token. The server has already deleted it, so from now on the player signs in by email. The current session keeps working.- The player signs into a different account by email: delete the stored device token too. Otherwise the next launch signs back into the throwaway guest.
Until refresh tokens exist, a verified player retypes their password every 30 days
A verified player has no device token, so between launches only the saved
session keeps them signed in. When it expires, they sign in by email again.
Refresh tokens (TODO.md) will remove this.
The hardware ID is not a credential
RegisterGuest needs a HardwareId, and SystemInfo.deviceUniqueIdentifier
is the right thing to send. The server only records it, for support and
spotting reroll farms, because it isn't secret: analytics, ad and crash SDKs
read and send it. The credential is the device token the server mints.
Store that like a password: PlayerPrefs is fine for a prototype, and a
shipped game should use the iOS Keychain or the Android Keystore.
The calls¶
| SDK call | PlayFab counterpart | Route | Works with a player token today? |
|---|---|---|---|
RegisterGuest |
LoginWithCustomID with CreateAccount |
POST /api/players/register |
Yes (public) |
LoginWithDeviceToken |
LoginWithCustomID |
POST /api/players/login/device |
Yes (public) |
LoginWithEmailAddress |
same | POST /api/players/login/email |
Yes (public) |
UpdateUserTitleDisplayName |
same | POST /api/players/display-name |
Yes |
LinkEmailAddress |
AddUsernamePassword |
POST /api/players/link/email |
Yes |
VerifyEmail |
— | POST /api/players/email/verify |
Yes |
ResendVerificationEmail |
— | POST /api/players/email/resend |
Yes |
SendAccountRecoveryEmail |
same | POST /api/players/password/forgot |
Yes (public) |
ResetPassword |
— | POST /api/players/password/reset |
Yes (public) |
WritePlayerEvent |
same | POST /api/events |
No — admin-only, 401 |
GetAllUsersCharacters |
same | GET /api/characters |
No — admin-only, 401 |
GrantCharacterToUser |
same, minus the catalog item | POST /api/characters |
No — admin-only, 401 |
The last three exist so game code can be written against them now. They start
working when the server opens those routes to player tokens
(TODO.md item 2), and their wire format will likely change at that point.
That change belongs in ServerContract.cs (below), not in game code.
Differences from PlayFab worth knowing:
- No
TitleId. Hakutaku is self-hosted and serves one game, so settings hold aServerUrlinstead. - Creating and signing in are separate calls. PlayFab's
LoginWithCustomIDtakes a client-chosen ID and aCreateAccountflag. Here the server mints the credential inRegisterGuest, andLoginWithDeviceTokennever creates. - Display names are optional on
RegisterGuest, andUpdateUserTitleDisplayNamechanges them any time. They're 3–25 characters and not unique, so never use one to identify a player. ForgetAllCredentialsis client-side only. There is no player logout route, so the session stays valid on the server until it expires (30 days) or a password reset revokes it.SendRawRequestAsync(method, path, json)sends any request with the current token and returns the body as text. The simulator uses it to check that admin routes refuse player tokens. A game shouldn't need it.
Errors¶
HakutakuError mirrors PlayFabError: ApiEndpoint, HttpCode,
HttpStatus, Error (a HakutakuErrorCode), ErrorMessage, and
GenerateErrorReport().
The server sends only { "error": "<text>" }, with no machine-readable code,
so HakutakuErrorCode comes from the HTTP status: InvalidParams (400),
NotAuthenticated (401), Conflict (409), TooManyRequests (429), and so on.
Two codes mean nothing was answered: NotLoggedIn, when a session call was
made before logging in and was never sent, and ConnectionError, when no
response came back.
The SDK also deals with the responses that aren't { error }:
- the rate limiter's
429has an empty body; - Caddy's
404for a non-public route has an empty body.
These get a sensible default message instead of a JSON parse failure.
If the server ever sends error codes
Mapping on message text would break the first time a message is reworded.
A stable code field in the server's error body is the fix, and the SDK
would then read it in HakutakuHttp.MessageFor.
How it's built¶
| File | What it holds |
|---|---|
HakutakuClientAPI.cs |
The static facade over one default instance |
HakutakuClientInstanceAPI.cs |
Every call, in Async and callback forms, plus the mapping from wire to public types |
ClientModels.cs |
The public request and result types (Hakutaku.ClientModels) |
ServerContract.cs |
Internal. The routes and the server's JSON shapes, kept in one place |
HakutakuHttp.cs |
Internal. The only file that touches the network or JSON |
HakutakuSettings.cs |
HakutakuApiSettings, HakutakuAuthenticationContext, and the static defaults |
HakutakuError.cs |
HakutakuError, HakutakuErrorCode, HakutakuResult<T> |
Decisions that shape it:
- Instance-first. Each
HakutakuClientInstanceAPIowns its session, like PlayFab's instance API, so mock players never share a token. OneHttpClientis shared by all of them. The Bearer token goes on each request rather than inDefaultRequestHeaders, and cookies are off. - Public types are separate from the wire format. The server's JSON is
mapped into PlayFab-style types in one place. A route rename such as the
planned
/api/players→/api/playeris then an internal edit, not a breaking change for games. - IDs are opaque strings.
PlayerId,CharacterIdandEventIdare never parsed as GUIDs; see Practices. - Callbacks run on the caller's thread. The callback overloads await the
async call on the caller's
SynchronizationContext, so in Unity they fire on the main thread, as PlayFab's do.
Taking it into Unity¶
The project targets netstandard2.1 with C# 9, which is what Unity 2021.2
and later compile. Keep it that way: no file-scoped namespaces, records,
init, required or global usings in sdk/. The project file pins
LangVersion so the build catches them.
Nullable reference types are switched on by a #nullable enable line at the
top of each file, not in the project file, because Unity compiles copied
source without ever reading the .csproj. Start every new SDK file with that
line. If you forget, the build warns on the file's first ?.
- JSON is Newtonsoft.Json. Unity ships it as an official package
(
com.unity.nuget.newtonsoft-json). System.Text.Json would have to be vendored DLL by DLL. - HTTP is
HttpClient, which works in the editor and on desktop and mobile builds but not on WebGL. For WebGL, rewriteHakutakuHttp.cson top ofUnityWebRequest; nothing else in the SDK touches the network. - Either copy the
.csfiles into the Unity project, or build the DLL (dotnet build -c Release) and drop it inAssets/Plugins.
The server SDK¶
sdk-server/ (Hakutaku.Sdk.Server) is a second assembly for the game
server. It authenticates with a server key (see
Auth and sessions), never a player token. It's what a
Mirror authenticator calls when a player joins:
var server = new HakutakuServerInstanceAPI(url, Environment.GetEnvironmentVariable("HAKUTAKU_SERVER_KEY"));
var who = await server.AuthenticateSessionTicketAsync(new AuthenticateSessionTicketRequest { SessionTicket = ticket });
if (who.Error != null)
{
if (who.Error.Error == HakutakuServerErrorCode.InvalidSessionTicket) { /* turn the player away */ }
else if (who.Error.IsHakutakuUnavailable) { /* Hakutaku is down or slow: let them in offline */ }
else { /* a setup problem on this game server, e.g. ServerKeyRejected */ }
return;
}
var data = await server.GetUserReadOnlyDataAsync(new GetUserDataRequest { PlayerId = who.Result!.PlayerId, Keys = new List<string> { "Xp" } });
long xp = data.Result != null && data.Result.Data.TryGetValue("Xp", out var s) && long.TryParse(s, out var v) ? v : 0;
| Call | Route |
|---|---|
AuthenticateSessionTicket |
POST /api/server/sessions/validate, which gives the player's id and display name |
GetUserReadOnlyData |
GET /api/server/players/{id}/data?tier=ReadOnly. No Keys means every key, and a key never set is left out |
UpdateUserReadOnlyData |
POST to the same path. Sets values and never adds to them, so a retry is harmless; at most 10 changes a call |
Each comes in Async and callback forms, like the client SDK, and returns a
HakutakuServerResult<T> instead of throwing. The key is a constructor
argument, which the game server reads from its own environment, never from a
build. The constructor also takes a timeout per call (10 seconds by default,
30 at most), since a joining player waits on the validate call.
The error split. HakutakuServerErrorCode keeps apart the cases the
authenticator decides on:
| Code | Meaning | What the game does |
|---|---|---|
InvalidSessionTicket |
A 401 carrying invalid_ticket: the ticket is unknown, revoked or expired |
Turn the player away |
Timeout, ConnectionError, InternalServerError, ServiceUnavailable |
No answer in time, no answer at all, or a 5xx. IsHakutakuUnavailable is true for all of them |
Let the player in offline, if the game allows it |
ServerKeyRejected |
A 401 without that code: this game server's own key was refused |
Fix the setup. It's never the player's fault |
MissingServerKey |
No key was given, so nothing was sent | Set HAKUTAKU_SERVER_KEY |
How it shares code with the client SDK. The project compiles
HakutakuHttp.cs, HakutakuError.cs and ServerContract.cs from sdk/ as
linked files, not copies, so there is one copy of the HTTP code and of the
routes. It defines HAKUTAKU_SERVER_SDK, which makes the client's
HakutakuError, HakutakuErrorCode and HakutakuResult<T> internal there. The
server SDK's public types are all named HakutakuServer…. A Unity project that
references both DLLs therefore has one public copy of every type and no
CS0433. The test project and the simulator both reference both SDKs, so the
build checks this.
The same Unity rules apply as for sdk/: netstandard2.1, C# 9, Newtonsoft,
and #nullable enable at the top of every file.
Caddy proxies /api/server/* publicly, so a game server outside the VM (the
homelab) uses the public URL, https://51.79.242.169.nip.io. How the game
server must read and write XP (never write after a failed read, one write in
flight per player, at most every 30 seconds) is in the
API reference.
Still open¶
| Question | Notes |
|---|---|
| Retries and caching | None. Calls pass straight through, and a 429 is returned to the caller. The simulator does its own retrying. |
| Packaging | Source or DLL by hand for now. A Unity package (UPM) is the likely end state. |
Related reading¶
- Simulator — the SDK's first user
- API reference — the routes underneath
- Auth and sessions — what a player token is