Runtime guide
Endpoint calls, game values, encrypted requests and key pinning, compatibility status and helpers.
Everything game code calls. The generated per-member reference is at sboxns.com/docs/library/api; copyable patterns are in Examples/.
Calling the server
if ( !NetworkStorage.IsConfigured )
NetworkStorage.Configure( "your_project_id", "sbox_ns_your_public_key" );
// Endpoint without input (GET; POST when encrypted requests are required)
var player = await NetworkStorage.CallEndpoint( "load-player" );
// Endpoint with input (POST)
var result = await NetworkStorage.CallEndpoint( "mine-ore", new { ore_id = "iron", kg = 5.0f } );
// Endpoint URLs copied from the dashboard work too; the slug is extracted.
await NetworkStorage.CallEndpoint(
"https://api.sboxcool.com/v3/endpoints/your_project_id/mine-ore?apiKey=sbox_ns_your_public_key",
new { ore_id = "iron", kg = 5.0f } );
// Documents (dedicated servers attach +network_storage_secret_key automatically)
await NetworkStorage.SaveDocument( "player-data", Game.SteamId.ToString(), new { level = 2, xp = 100 } );
await NetworkStorage.UpdateDocument( "player-data", Game.SteamId.ToString(),
NetworkStorageOperation.Increment( "xp", 50, source: "server", reason: "quest" ),
NetworkStorageOperation.Set( "lastSeen", DateTimeOffset.UtcNow.ToUnixTimeSeconds() ) );
await NetworkStorage.DeleteDocument( "player-data", Game.SteamId.ToString() );
if ( result.HasValue )
{
var currency = result.Value.Int( "currency", 0 );
}
| Member | Description |
|---|---|
Configure(projectId, apiKey, ...) |
Set credentials once at startup. See getting started. |
IsConfigured / ApiRoot |
Whether Configure ran; the full API URL (.../v3, or .../api with apiVersion api). |
CallEndpoint(slugOrUrl, input?) |
Call an endpoint by slug or dashboard URL. Returns JsonElement?. |
TryGetLastEndpointError(...) |
The last error code and message for a call. Keys: an endpoint slug, storage, storage-save, storage-update, storage-delete, storage-records, storage-record-create, storage-record-delete, storage-record-rename, query, query:{id}, game-values. INVALID_ENDPOINT, INVALID_QUERY and CAPABILITY_UNAVAILABLE are also keys holding the latest error with that code, so a blank endpoint reference or query id stays readable. |
GetGameValues() |
All game value groups and tables in one request. |
GetDocument(collectionId, documentId?) |
Read a document; defaults to the current player's Steam ID. |
SaveDocument / UpdateDocument / DeleteDocument |
Replace a document, apply set/inc/push/pull/remove operations, or delete one row. Public deletes need record deletes enabled on the server. |
ListRecords / CreateRecord / RenameRecord / DeleteRecord |
Multi-record save slots. |
TrackAnalyticsEvent(eventType, payload?) |
Custom Player Analytics event; the name must be listed in the project's analytics settings. |
NetworkStorageAnalytics.Warning/Error/SessionStart/SessionEnd |
Warnings, caught exceptions and session boundaries on the player timeline. |
LibraryVersion / MinServerVersion |
Library SemVer (1.2.1, unreleased candidate) and the oldest supported self-hosted server (0.4.0, metadata only). |
ServerVersion / LibraryStatus / OnLibraryStatusChanged / HasCapability(name) |
What the server reported; see server compatibility. |
SendAsync(NetworkStorageRequest) |
Send any /v3 (or api.root) request and get a NetworkStorageResult back. Never throws; see generic requests. |
Retries and errors
Rate-limited calls (HTTP 429) are retried at most twice, with the total wait capped at 5 seconds,
using the server's Retry-After header, then retryAfterSeconds, then retryAfterSec in the body. GET requests
always retry; other methods only when the server marks the response retrySafe. A GET that gets a 503 with
Retry-After is retried the same way. A call that still fails is reported as RATE_LIMITED through TryGetLastEndpointError.
Failed replies keep the server's error code. A JSON error code ({"ok":false,"error":{"code":...}}, or a bare
"error":"CODE" string when the value is code-shaped) is reported as-is, with the server's message. A non-2xx
reply without a JSON code is reported with the call's fallback code (HTTP_ERROR for endpoints, REQUEST_FAILED
for storage, queries and game values; NOT_FOUND for a missing document), and the HTTP status appears in the
message (for example HTTP 413 RequestEntityTooLarge). 429 without a code is RATE_LIMITED. Transport failures are REQUEST_FAILED.
A document read returns the stored JSON exactly as saved, including top-level arrays and keys named ok, body
or error. An endpoint reply is unwrapped to its body only when it has ok: true and a timing object.
A security refusal (SECURITY_*) is retried once after a config refresh only when the server marks it retrySafe: true.
Storage writes send x-ns-write-mode (replace for SaveDocument, ops for UpdateDocument) so the server does not
infer the mode from the body. Runtime POST, PUT, PATCH and DELETE requests send an Idempotency-Key,
including endpoint calls and analytics, and transport retries reuse it. Direct endpoint security/clock retries
also retain the logical call's key. Sync Tool and Python management writes send one key per call.
Every runtime, Sync Tool and Python API request sends an X-Correlation-ID, including server-info reads.
These headers are metadata; they do not add server-side deduplication or change authentication.
A listener on a library event that throws is logged; the throw does not reach the request that raised the event. Later listeners on the same event are skipped, so keep listeners small.
Generic requests
NetworkStorage.SendAsync(new NetworkStorageRequest { ... }) sends one request to the storage API and returns a
NetworkStorageResult (Ok, Status, Code, Message, Body, RetryAfterSeconds, RequestId). It never throws.
Method:GET(default),POST,PUT,PATCHorDELETE.Pathstarts with/, for example/storage/{project}/{collection}/{key}.Query: extra query parameters.apiKey,revisionTarget,includeStagedandsecret-keyare reserved.Body: JSON-serialised body for non-GET requests.Auth:Player(default; the signed-in player's token),Public(project key only) orDedicatedSecret(dedicated server secret).IdempotencyKey: optional, 1 to 128 visible characters; generated when omitted.UseServerRoot: send to the server'sapi.root(path only, from server info) instead of theApiRootURL.
Refusals before a request is sent use codes INVALID_REQUEST, INVALID_METHOD, INVALID_PATH, NOT_CONFIGURED,
API_ROOT_UNAVAILABLE, DEDICATED_SECRET_REQUIRED and DEDICATED_SECRET_UNAVAILABLE. /endpoints/... is refused
with USE_CALL_ENDPOINT; use CallEndpoint. /manage/... is refused with UNSUPPORTED_PATH.
These checks also apply to percent-encoded route segments. Dot segments and encoded
separators, queries, fragments or control characters are refused with INVALID_PATH.
Calls through the host (proxy mode) wait at most 30 seconds for the host's reply. A timeout is reported as PROXY_TIMEOUT. PROXY_FAILED means the host could not answer: it returned nothing, replied in a format this game build does not understand, or failed while handling the call. Endpoint and document calls relay the server's own error code and message through the host (for example NOT_FOUND or HTTP_ERROR), so games see the same codes as in direct mode. A host that refuses a request whose claimed Steam ID does not match the sending connection reports PROXY_REJECTED. Document reads through the host return the stored JSON unchanged. Proxy requests are sent only to the host and replies only to the requesting player; game builds older than this release still broadcast requests to every player.
Analytics batching
When the server advertises analytics-batch, events are queued and sent together (up to 50 events
or 256 KiB per request; the first event waits at most 2 seconds). Servers without the capability
get one request per event. Rejected events, events over 32 KiB and events beyond the 500-event
queue are counted in a [NetworkStorage] Dropped N analytics event(s) warning, at most once every
30 seconds. An abrupt process exit can lose in-flight events.
Game values
Shared numbers the game and server both read: tuning constants, named groups and typed tables. Author them in the server's owner console or over its API; see the server's game values guide for revisions, snapshots and a worked anti-exploit example.
var gv = (await NetworkStorage.GetGameValues()).Value;
var combat = gv.GetProperty( "groups" ).GetProperty( "combat" ).GetProperty( "values" );
int zombieHp = combat.Int( "zombie_hp_per_level" ) * zombieLevel;
var weapons = gv.GetProperty( "tables" ).GetProperty( "weapons" )
.ReadList( "rows", r => (Id: r.Str( "weapon_id" ), Damage: r.Int( "damage" ), Model: r.Str( "model" )) );
GetGameValues() is one GET {ApiRoot}/values/{projectId}. The library keeps no offline copy:
load once at startup and look up locally afterwards. Game code cannot write game values. Endpoints
read the same numbers with {{values.combat.zombie_hp_per_level}} and lookup steps with
source: values.
A build keeps its published snapshot only when the project has an explicit revision policy and its revision is retained inside the support window; otherwise it reads live values.
GetGameValues is a public, unsigned GET outside the endpoint path: neither the encryption pin
nor a refused config covers it, and a proxy can read or change it. Do not put secrets in game
values.
Encrypted requests
The server owner picks the mode (off, v1, v2-preferred, v2-required) and the game follows
the server's signed security config. v2 seals each request to a server-generated key that can
rotate without a game update.
- With encryption required, a call without input is still sent as an encrypted POST. The host's
CallEndpointAspath builds no envelope, so a no-input call on behalf of another player is refused locally withENCRYPTION_REQUIRES_POST. - Pin the signing key so a proxy or swapped config cannot substitute its own: pass
securityKeyFingerprintas the seventhConfigureargument, or enter it in the Setup window. Copy it fromsbox-ns security status("Security signing key"); spaces, colons and case are ignored. With a pin, endpoint calls are refused withENCRYPTION_KEY_UNVERIFIEDuntil a config signed by that key loads. - Without a pin, the first signing key seen per API root is trusted and saved in the game's
data folder (
network-storage-signing-key-trust.json). A config signed by a different key is refused withENCRYPTION_SIGNING_KEY_CHANGED, and the last trusted config is kept. The one exception is an announced rotation. The server signs a statement with the key it replaces, naming the new key and an expiry, and carries the statements in the signed config (signing.rotations, oldest first, at most 8). The library follows the chain from the trusted key to the config's key. A statement that is malformed, not signed by the key it replaces, or expired at the config'sgeneratedAtis refused withENCRYPTION_SIGNING_KEY_ROTATION_INVALID; the log names the reason. Expiry uses the server-signedgeneratedAt, never the device clock. A game offline past a statement's expiry stays refused until it resets the trust or ships a pin. - Reset the trust after verifying the server identity out of band with
NetworkStorage.ResetSigningKeyTrust(). It forgets the key trusted for the current API root and makes the next call fetch the config again; that config is trusted on first use. Pins are not changed, so a pinned game still refuses a different key. - Unpinned games that never receive a trusted config stay on v1. The trust store checks each saved entry on its own: an unusable entry is logged and skipped, and the others keep their trust. A trusted signing key may have any RSA modulus of at least 128 bytes (RSA-1024 and larger). Encryption v2 still requires RSA-2048 keys.
- A config that fails verification refuses every endpoint call, plain or encrypted. Calling
Configurewith a different pin, base URL, API version or project drops the held config and verifies again. - Clock skew. Encrypted requests carry a unix time that the server accepts only inside its replay
window (30 to 600 seconds, default 120). The library learns the server's clock from the
Dateheader of its responses (a response with anAgeheader is cached, so it is ignored) and from theserverTimein anENCRYPTED_REQUEST_STALErefusal, and builds request ids from that clock. A stale refusal is retried once with a fresh id, so a game whose client clock is wrong still makes its calls. The learned offset is clamped to one day, and differences under 5 seconds are treated as zero. - Diagnose a refused config.
NetworkStorage.TryGetSecurityConfigError(out string code, out string message)returnstruewith the refusal code and remediation text while the held config is refused and blocks endpoint calls. It reports only for the project and API root the runtime is configured for, and it never refreshes, accepts or resets a config or returns key material. The Setup window shows the same notice when its project ID and base URL match the runtime. Calls stay blocked until a trusted config loads; the API does not unblock them.
Server compatibility status
The library reads x-ns-server-version and x-ns-library-status from responses and
capabilities from /v3/server-info:
| Member | Meaning |
|---|---|
ServerVersion |
Last version the server reported, or null. |
LibraryStatus |
supported, outdated, deprecated, unsupported, blocked or unknown. |
OnLibraryStatusChanged |
Raised when the status changes. No player-facing UI is shown. |
HasCapability(name) |
true unless the server lists capabilities and the name is missing. |
A 426 LIBRARY_UNSUPPORTED or LIBRARY_BLOCKED response is surfaced through
TryGetLastEndpointError and logged once. In the editor, Setup and the Sync Tool show a banner for
outdated/unknown (or a newer published revision), a once-per-session dialog for deprecated,
and refuse Push All for unsupported/blocked; Pull and Test Live keep working. Nothing here
runs in play mode or published games.
Runtime, Sync Tool and Python sync requests send x-ns-library-version. Editor tools also send
x-ns-client-type: editor and, when the installed .version has a positive revision,
x-ns-library-revision. Runtime never reads .version. These headers are self-reported
diagnostics, not authentication.
Helpers
JSON
var name = data.Str( "playerName", "Unknown" ); // JsonHelpers.GetString( data, ... )
var level = data.Int( "level", 1 ); // missing keys and "5" strings handled
var speed = data.Float( "speed", 1.0f );
var active = JsonHelpers.GetBool( data, "active", true );
var upgrades = data.ReadStringList( "purchasedUpgrades" );
var ores = data.ReadDictionary( "ores", v => (float)v.GetDouble() );
var items = data.ReadList( "rows", row => new ItemInfo( row.Str( "id" ), row.Str( "name" ), row.Int( "tier" ) ) );
SaveStateTracker
Wraps endpoint calls with state for HUD feedback:
var tracker = new SaveStateTracker();
var result = await tracker.Call( "mine-ore", new { ore_id = "iron", kg = 5 } ); // State: Saved or Error; IsBusy while in flight
await tracker.CallAndApply( "sell-ore", input,
applyOptimistic: () => { /* update local state */ },
applyServer: data => { /* apply authoritative response */ },
revert: () => { /* undo on failure */ } );
Call sets State to Error and rethrows when the endpoint throws; CallAndApply runs revert before rethrowing. A call that completes after Reset() no longer changes State.
NetLog
Ring buffer of every Network Storage request, response and error, for debug panels:
foreach ( var entry in NetLog.Entries ) { /* entry.Time, entry.Kind, entry.Tag, entry.Message */ }
NetLog.Info( "my-system", "Custom log message" );
var version = NetLog.Version; // increments on every add or clear