Native games
When the launcher starts a native game (the program you named as the executable when uploading the build), it sets three environment variables:
| Variable | Meaning |
|---|---|
SPAWNLE_TICKET |
The player’s game ticket (gt_…). |
SPAWNLE_API |
The API to call, e.g. https://api.spawnle.com. |
SPAWNLE_GAME |
Your game’s id. |
If SPAWNLE_TICKET is missing, the game was not started by Spawnle (you ran
it yourself while developing). Fall back to offline behaviour.
Send the ticket as a bearer token to the SDK routes:
GET /v1/sdk/me HTTP/1.1Host: api.spawnle.comAuthorization: Bearer gt_9f2c…Examples
Section titled “Examples”using System;using UnityEngine;using UnityEngine.Networking;
public class Spawnle : MonoBehaviour{ string ticket = Environment.GetEnvironmentVariable("SPAWNLE_TICKET"); string api = Environment.GetEnvironmentVariable("SPAWNLE_API") ?? "https://api.spawnle.com";
[Serializable] class Me { public string handle; public string display_name; public int level; }
System.Collections.IEnumerator Start() { if (string.IsNullOrEmpty(ticket)) yield break; // not started by Spawnle var request = UnityWebRequest.Get(api + "/v1/sdk/me"); request.SetRequestHeader("Authorization", "Bearer " + ticket); yield return request.SendWebRequest(); var me = JsonUtility.FromJson<Me>(request.downloadHandler.text); Debug.Log($"Hi {me.display_name}!"); }}var ticket = OS.get_environment("SPAWNLE_TICKET")var api = OS.get_environment("SPAWNLE_API")
func _ready(): if ticket.is_empty(): return # not started by Spawnle var http = HTTPRequest.new() add_child(http) http.request_completed.connect(_on_me) http.request(api + "/v1/sdk/me", ["Authorization: Bearer " + ticket])
func _on_me(_result, _code, _headers, body): var me = JSON.parse_string(body.get_string_from_utf8()) print("Hi ", me.display_name)use serde::Deserialize;
#[derive(Deserialize)]struct Me { handle: String, display_name: String, level: u32 }
fn main() -> Result<(), Box<dyn std::error::Error>> { let Ok(ticket) = std::env::var("SPAWNLE_TICKET") else { return Ok(()); // not started by Spawnle }; let api = std::env::var("SPAWNLE_API").unwrap_or("https://api.spawnle.com".into()); let me: Me = ureq::get(&format!("{api}/v1/sdk/me")) .header("Authorization", &format!("Bearer {ticket}")) .call()? .body_mut() .read_json()?; println!("Hi {}!", me.display_name); Ok(())}const FString Ticket = FPlatformMisc::GetEnvironmentVariable(TEXT("SPAWNLE_TICKET"));const FString Api = FPlatformMisc::GetEnvironmentVariable(TEXT("SPAWNLE_API"));if (Ticket.IsEmpty()) return; // not started by Spawnle
auto Request = FHttpModule::Get().CreateRequest();Request->SetURL(Api + TEXT("/v1/sdk/me"));Request->SetHeader(TEXT("Authorization"), TEXT("Bearer ") + Ticket);Request->OnProcessRequestComplete().BindLambda( [](FHttpRequestPtr, FHttpResponsePtr Response, bool bOk) { UE_LOG(LogTemp, Log, TEXT("%s"), *Response->GetContentAsString()); });Request->ProcessRequest();Keep the ticket private
Section titled “Keep the ticket private”The ticket only opens the SDK routes for this player in this game, but treat
it like a password: do not log it, do not show it, and send it only to
SPAWNLE_API or to your own server.
Tickets last 24 hours. A game session longer than that should handle
401 unauthorized by asking the player to restart the game from Spawnle.