Skip to content

Call C# from the page and back

Move data between your page and your C# mod. Strings cross in both directions. Use JSON for structured data. No CLR object reaches the script and no script object reaches your mod.

You need: a registered app. See Build your first app.

DirectionC#JS
page asks the modapp.OnCall("name", arg => answer)const s = s1.call('name', 'arg')
mod tells the pageapp.Emit("name", payload)s1.on('name', payload => ...)
page keeps its own state-s1.storage.get/set/remove/clear
page reaches the networkapp.AllowHost("api.example.com")fetch(url, init).then(...)
phone orientationapp.Orientation("landscape", "portrait")s1.orientation, s1.setOrientation(v)
player pressed back-document.addEventListener('back', ...)
  1. Register handlers on the app. Handlers are scoped to your app id, so two mods can both own list.

    app.OnCall("items.list", _ => string.Join("\n", Items))
    .OnCall("items.add", Add)
    .OnCall("items.clear", _ => { Items.Clear(); return "ok"; });
  2. Call it from the page. s1.call is synchronous. It runs your handler on the Unity main thread in the same frame, so it can touch game state. Do not await it.

    const raw = s1.call('items.list');

A call with no handler returns "" and logs a warning. A handler that throws returns "" and logs the exception. The page sees only the empty string, so show a message for it.

  1. Emit from OnUpdate, when something changed. Do not emit on a timer.

    public override void OnUpdate()
    {
    if (_revision == _pushed) return;
    _pushed = _revision;
    _app?.Emit("items.changed", Items.Count.ToString());
    }
  2. Subscribe in the page.

    s1.on('items.changed', render);

Keep the payload small. Send “something changed” and let the page ask for the data.

Never call Emit inside an OnCall handler. It re-enters the script engine while the engine is still on the stack.

Sideload.Api has no JSON writer. Write one, or copy the 60-line Json.cs from WhatsDab. The page has JSON.parse.

app.OnCall("chat.threads", _ =>
Json.Array()
.Item(Json.Object().Add("id", t.Id).Add("name", t.Name).Add("unread", t.Unread))
.Close());
const threads = JSON.parse(s1.call('chat.threads') || '[]');

For a simple list, use a newline-separated string. For several inbound arguments, use one separator character. Reject a payload that contains a second one:

int split = raw.IndexOf('\n');
if (split <= 0) return "error";
string id = raw.Substring(0, split), text = raw.Substring(split + 1);
if (text.IndexOf('\n') >= 0) return "error";

s1.storage holds strings by name. It writes one JSON file per app at UserData/Sideload/<appId>.json.

const tab = s1.storage.get('tab', 'players');
s1.storage.set('tab', 'items');
s1.storage.remove('tab');

It is not the game save. Use it for UI state such as the last tab or a draft. Keep world data in your mod behind an s1.call.

Do not store the phone orientation here. Sideload remembers it per app.

localStorage and sessionStorage write to the same store.

A page reaches no host until your mod allows it.

Apps.Register("mystash", "MyMod.Assets.mystash")
.AllowHost("api.example.com")
.AllowHost("*.cdn.example.com");
  • *.example.com covers one label under it, not example.com itself.
  • Ports are ignored.
  • The scheme must be https, except for 127.0.0.1 and localhost.
  • Every redirect hop is checked again.
  • Limits: 10 s timeout, 4 MB response, 8 requests in flight.
  • A blocked request rejects with the exact AllowHost call that is missing.

Never await a fetch. It blocks the thread that delivers the answer and freezes the game. Use .then() and .catch():

fetch('https://api.example.com/status')
.then(r => r.json())
.then(data => render(data))
.catch(e => console.error('status failed:', e));

There is no navigator, no file access and no clipboard. Timers are the four standard ones plus requestAnimationFrame. window is an alias for the global object. For anything else, put it behind an s1.call. Your mod is ordinary C# with the whole game in reach.

For limits that s1.call cannot fix, see Phone integration.

The page shows the string your handler returned. When an event is emitted, the s1.on callback runs. Failures appear in MelonLoader/Latest.log.