The user-code layer is what turns Realm from an economy sim into a platform. Players write Lua services that run on the engine, read public and private game state, place orders, propose contracts, and send messages — and can sell those services to other players as subscriptions. A player who automates their inventory management writes a script. A player who notices others have the same problem polishes it, adds a UI, and offers it as a SaaS. Other players subscribe, and a real software business exists entirely in-game, with revenue, customers, and reputation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/at6132/econ/llms.txt
Use this file to discover all available pages before exploring further.
The full user-code layer (in-browser IDE, block editor, service marketplace) ships in Phase 4. The current API endpoints are stubs for static validation and source deployment. The simulation engine already exposes all the actions that scripts will eventually call.
What code can and cannot do
Before writing any Lua, it helps to understand the hard boundaries the runtime enforces.Permitted
- Read public world state: current tick, map and plot data, market order books, price history
- Read your own private state: your inventory, accounts, and contracts
- Place market orders and cancel them
- Propose and accept contracts
- Send in-game messages to parties
- Call other players’ published services (Phase 4)
- Move money between your own accounts
Forbidden
The runtime enforces these at the sandbox layer — not by convention.- Cannot read another player’s private inventory, account balances, or contracts
- Cannot impersonate another party
- Cannot create money or materials from nothing (all transactions go through the engine’s conservation-law enforcement)
- Cannot bypass the contract system
- Cannot make other players’ code execute
- Cannot interact with external networks (no real-world HTTP)
- Cannot use real randomness — only the simulation’s
(tick, purpose)seed - Cannot consume more CPU, memory, or storage than budgeted
- Cannot use forbidden Lua globals:
require,dofile,loadfile,io,os,debug,package,coroutine,getfenv,setfenv,string.dump,collectgarbage,newproxy,module
Resource metering
Code costs CPU, memory, and storage, all billed in in-game currency. CPU budget: Each player has a base monthly allocation (free for everyone). Beyond that, you buy more at a market rate. Each deployed service executes within a hard instruction budget per call — exceeding it fails the call rather than stalling the simulation. Memory / storage: Code can persist data in a per-service key-value store. Storage costs in-game money per byte per game-day. Determinism budget: Code must execute within a fixed instruction count per call. Over budget = the call fails. This is what prevents one player’s badly-written code from stalling the tick loop for everyone else.API endpoints
GET /code/status
Returns the user-code capability advertisement. Use this to check whether a Lua runtime is available, what the size limits are, and what phase the sandbox is in.POST /code/validate
Performs static validation on a Lua source string: size and shape only, no execution. Use this from an IDE or CI pipeline to check whether a source is within limits before deploying. Request body:POST /code/deploy
Validates and stores a Lua source string for a party inworld.deployed_lua_sources. Validated size only; execution is handled separately and gated. This is the Phase 4 staging endpoint.
Request body:
GET /world under the deployed_lua key as { chars, lines } metadata (the source itself is not returned in the public world view).
POST /code/eval
Runs a Lua chunk and returns its return value. This is a local development tool — it is not the production sandbox and does not have access to the full Realm API. Request body:tick and purpose available as globals, plus a restricted math table (abs, floor, min, max, sqrt). No other globals are available.
REALM_LUA_EVAL is not set:
Writing Lua services
The read API (free or low-cost)
The write API (consumes resources)
Example: auto-restock
The simplest useful service watches your inventory and places a buy order whenever stock of a material drops below a threshold.Example: logistics optimizer
A more advanced service accepts delivery requests and returns an optimized route plan. Other players call it and pay per use.Example: market-making bot
A tick-scheduled service that maintains a spread around the mid-price for a material:Services as a business
A published service is a deployed function with:- A name and description visible in the service marketplace
- A pricing model (per-call, subscription, free, or custom)
- An owner (the deploying party) who receives revenue
- Reputation (uptime, response quality, customer reviews)
- Versioning — services can be updated without breaking subscribers
Sandbox internals
The eval sandbox uses Lua 5.1 via lupa withloadstring + setfenv. The sandbox environment contains only tick, purpose, and a stripped math table. Forbidden patterns are checked with a regex before execution:
- A Lua interpreter with restricted standard library
- A connection to the simulation API (read/write)
- A per-service key-value store with quota
- A hard CPU instruction budget per call
- An execution history for debugging and audit
- OS-level isolation if needed for additional security