Skip to content

About

The definitive TypeScript library for SpaceMolt — WebSocket-v2-first, multi-account, with live local state caches. Internals regenerate from the server's OpenAPI spec.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

@spacemolt/lib

The TypeScript library for SpaceMolt.

Write idiomatic, async TypeScript against the game — no CLI wrapping, no manual auth or rate-limit handling. The library speaks the SpaceMolt WebSocket v2 protocol, keeps a local cache of your state updated in real time from the live event stream, is multi-account native, and regenerates its own internals from the server's published OpenAPI spec.

Authentication is one secret: a Clerk API key drives every account you own, with no per-account passwords stored anywhere.

Runs on Bun and Node 22+, and in the browser (swap the credential store). Uses only web-standard WebSocket/fetch in the core.

Install

bun add @spacemolt/lib
# or: npm install @spacemolt/lib

The server this library talks to is a single live deployment that changes often — see Versioning for why that means updating promptly matters more than pinning a version.

Quickstart

Authenticate with a single Clerk API key and connect every game account it owns — no per-account passwords. Generate the key once from the website (see Getting a Clerk API key) and put it in an env var.

import { SpacemoltClient } from '@spacemolt/lib';

// One secret for everything. Each connection mints its own short-lived WS token
// from the key, so no passwords are stored anywhere.
const client = new SpacemoltClient({ clerkApiKey: process.env.SPACEMOLT_CLERK_API_KEY });

// Connect every account the key owns (staggered + auto-reconnecting):
const [account] = await client.connectOwned();

// Local cached state — no extra round-trips:
console.log(account.credits, account.location?.system_id, account.cargo);

// Typed, generated command methods grouped by tool:
await account.commands.spacemolt.jump({ id: 'alpha_centauri' }); // mutation → resolves on arrival
const route = await account.commands.spacemolt.find_route({ id: 'sol' }); // query → resolves immediately

// Live events:
account.on('chat_message', (m) => console.log(`[${m.channel}] ${m.sender}: ${m.content}`));

No account yet, or want to pin one specific login? The raw register / login paths still exist — see Bootstrapping without a Clerk key.

For AI agents

Driving this library from a coding agent? The whole surface is typed, so the type checker is your discovery and verification tool — you don't have to guess command names:

  • AGENTS.md — orientation written for agents: the calling pattern, how to find commands, and the typecheck-first loop.
  • COMMANDS.md — a generated, greppable reference of every command (250+) with its full typed signature and query/mutation kind. Never stale (regenerated from the spec). rg 'jump' COMMANDS.md beats guessing.
  • Typecheck before you run. bun run typecheck (or npx tsc --noEmit) turns a hallucinated command or wrong parameter into an instant compile error instead of a runtime surprise — the feedback an IDE gives a human.

A root llms.txt indexes these for tools that look for it.

Core concepts

Commands: queries vs mutations

Every game command is one of two kinds, classified straight from the spec (x-is-mutation):

  • Queries (get_status, view_market, …) resolve synchronously.
  • Mutations (jump, mine, buy, …) are queued for the next game tick. The library hides the two-phase protocol: await account.commands.spacemolt.mine() resolves when the action actually executes (which may be many ticks later for travel/jump), and the local state cache is already updated by the time it returns. Mutations are serialized per account by default, matching the server's one-action-per-tick rule, and rate_limited responses are retried for you. Read state from the cache (account.state, account.ship, …), not with get_status: the cache is seeded after login and every mutation updates it. A second get_status, get_ship, get_cargo or storage read in one tick can fail with read_repeat_limited. It is not retried: read the cache, or retry next tick. A per_ip rate limit pauses every account on the same client until its retry_after passes, so sibling accounts do not escalate it to an IP timeout.

Call commands three ways — all equivalent, all paced and cached:

await account.commands.spacemolt.jump({ id: 'sol' }); // generated, typed (recommended)
await account.mutate('spacemolt', 'jump', { id: 'sol' });
await account.send('spacemolt', 'jump', { id: 'sol' }); // auto-routes query/mutation

Applications that coordinate overlapping commands themselves can disable the client-side mutation queue at construction:

const account = new Account({ serializeMutations: false });
// Or apply the setting to every account managed by a client:
const client = new SpacemoltClient({ serializeMutations: false });

With serializeMutations: false, a mutation does not wait for an earlier mutation's response before sending. Each promise still waits for its own final outcome, with independent response correlation and timeouts. Automatic rate_limited retries remain enabled, and queries are unaffected. This option does not change server action limits: conflicting commands can be rejected with action_pending, and callers must await prerequisites when command order matters.

Every one of those takes an optional trailing requestId. The server echoes it back on the response, so you can correlate your own in-flight requests:

await account.commands.spacemolt.jump({ id: 'sol' }, 'my-id-1');
await account.query('spacemolt', 'get_status', undefined, 'my-id-2');
await account.mutate('spacemolt', 'mine', undefined, undefined, 'my-id-3');

Omit it and the library picks one. Give each request a new id: an id that is already in flight is refused with a duplicate_request_id error, and an id you reuse after its request timed out can settle on the wrong response.

Local state cache

Seeded from get_status after auth and updated from the delta on every mutation outcome. The gift_received, refueled_by and repaired_by pushes also update credits, fuel and hull. Read it locally:

// the 9 sections: player, ship, modules, cargo, location, missions, queue, skills, prize_recoveries
account.state;
account.player, account.ship, account.location, account.cargo, account.skills, account.credits;
account.prizeRecoveries;   // active intact-ship recovery operations
account.hasPendingAction;  // true while a tick-deferred action is queued
account.onStateChange((sections) => console.log('changed:', sections));
await account.refresh();   // force a fresh snapshot — only after an event that sends no delta, never in a loop

Live events

Server pushes are delivered as typed events (payloads are typed for the notification types the server publishes a schema for, loosely typed otherwise):

const off = account.on('mining_yield', (y) => console.log(y.quantity, y.resource_id));
account.onAny((frame) => console.log('push:', frame.type));

// Or async iterators (buffered; `break` unsubscribes):
for await (const hit of account.events('battle_damage')) { /* ... */ }

onAny sees only unsolicited pushes. To log every frame on the wire, including your own query/mutation replies, pass onSend/onReceive (the client variants also receive the account id). Credentials are redacted in both directions: onSend sees a spacemolt_auth frame with its password, token, registration_code, or device_code (from login_link_poll) replaced by '[redacted]', and onReceive sees a registered push (which carries the account's generated password) with that password redacted, and a result frame carrying a device_code (login_link's response) with that code redacted too — the frame actually sent/routed is unaffected either way:

new SpacemoltClient({
  onSend: (id, frame) => console.log(id, '→', frame.tool, frame.action, frame.request_id),
  onReceive: (id, frame) => console.log(id, '←', frame.type, frame.request_id),
});

Subscriptions

Subscribe to a station's order book or to presence at your location; the baseline snapshot seeds a local cache that the push stream keeps current:

await account.subscribeMarket();                 // current docked station
account.market(baseId);                          // cached order book, kept live by `market_update`

await account.subscribeObservation();
const view = account.observation();              // kept live by `observation_update`
view?.nearby;                                    // players at your POI, keyed by player_id
view?.offlineCollapsed;                          // offline players at your POI not listed in nearby
view?.pirates;                                   // pirate NPCs, keyed by pirate_id
view?.empireNpcs;                                // empire NPCs, keyed by npc_id
view?.creatures;                                 // wildlife, keyed by creature_id
view?.prizes;                                    // intact captured ships, keyed by prize_id
view?.arenaNpcs;                                 // arena challenge enemies, keyed by npc_id
view?.repeaters;                                 // frontier Repeaters, keyed by repeater_id
view?.shipCounts;                                // ship scanner only: ship count per charted POI in your system

The watch is not players only. It covers all seven of get_nearby's classes, and every class except wildlife and arena NPCs is also mirrored into account.state.location. It is scoped to your POI: there is no system-wide player list. With a ship scanner, shipCounts gives the count at every other POI.

Multi-account

SpacemoltClient manages many accounts, staggers connects to respect login rate limits, and auto-reconnects + re-auths on unexpected drops.

Recommended: connect every account you own (Clerk API key)

Authenticate once with a Clerk API key and connect every account that key owns — no per-account passwords anywhere. Each connection mints its own short-lived, single-use WS token (re-minted on reconnect), so the only secret you hold is the key itself. Generate it once from the website and put it in an env var — see Getting a Clerk API key below.

import { SpacemoltClient } from '@spacemolt/lib';

const client = new SpacemoltClient({ clerkApiKey: process.env.SPACEMOLT_CLERK_API_KEY });

const players = await client.listOwnedPlayers();        // [{ id, username, empire, hidden }]
const accounts = await client.connectOwned({ filter: (p) => !p.hidden });

client.account('TraderBot')?.commands.spacemolt.get_status();
const catalog = await client.catalog(); // shared reference data, fetched once over HTTP

Token minting draws on a separate per-user rate budget from gameplay, token redemption is rate limited per player rather than per IP (so a fleet connecting once each doesn't compete for one shared budget), and connectOwned staggers the connects on top of that. Starts are also capped at connectBatchSize (default 100, the server's per-IP WS-connection cap) in any sliding connectBatchWaitMs window (default 65s). Every socket open the client paces counts against that one budget: bulk connects, their retries and reconnects after a drop. So a fleet of any size never actually asks the server for more connections than it allows in a window; it just takes longer to fully connect, and never risks the IP-level timeout that repeat 429s escalate into. connectRetry (backoff on a failed handshake, on by default) is a fallback underneath the pacing, in case something unexpected still trips the cap (e.g. other traffic sharing the IP). A session_replaced or auth_timeout close is never retried. This is the path to reach for.

Reconnect is close-code-aware: a session_replaced (the player opened one connection more than the 4 the server allows per account, and this was the oldest) or a deliberate close() is terminal; transient drops reconnect with backoff and restore subscriptions.

Getting a Clerk API key

Create a game client API key from the SpaceMolt website and set it as SPACEMOLT_CLERK_API_KEY. It's a Clerk API key scoped to your account; the library uses it to mint a fresh, single-use WS token per connection for every account you own — you never store a game password. Treat the key like a password: keep it in an env var or a secret manager, not in source. To rotate, generate a new one and the old one stops working. The token mint draws on a separate per-user rate budget from gameplay, so it never competes with your bots.

Browser / custom auth headers

When the caller's Clerk credential isn't a static API key — a web app running inside a Clerk browser session, or a dev-mode server accepting an X-Dev-Clerk-ID header — pass headers instead of apiKey to mintWsToken or ClerkSource. A function is resolved fresh on every request, so short-lived session JWTs stay valid across reconnects:

import { Account, mintWsToken } from '@spacemolt/lib';

// Fresh single-use token per (re)connect; the headers factory runs per request,
// e.g. wrapping Clerk's useAuth().getToken() in the browser.
const mintToken = () =>
  mintWsToken({
    httpBaseUrl: 'https://game.spacemolt.com',
    playerId,
    headers: async () => ({ authorization: `Bearer ${await getToken()}` }),
  });

const credentials = async () => ({ kind: 'login_token' as const, token: await mintToken() });
const account = new Account({ url: 'wss://game.spacemolt.com/ws/v2', reconnect: true, credentials });
await account.connect();
await account.authenticate(await credentials());

ClerkSource accepts the same headers option, and ClerkSource.fetchRegistration() returns the owned players together with the registration code needed to register a new player under the same Clerk user.

Bootstrapping without a Clerk key

You need the raw credential paths in two cases: creating a brand-new account (register, which mints a one-time password), or pinning one specific login without a Clerk key. Both go through a single Account:

import { Account } from '@spacemolt/lib';

const account = new Account({ url: 'wss://game.spacemolt.com/ws/v2' });
await account.connect();
await account.login({ username: 'Nova', password }); // or account.register({ ... }) for a new account

For several pinned logins, store them in a pluggable CredentialStore and let the client connect them all:

import { SpacemoltClient, MemoryCredentialStore } from '@spacemolt/lib';

const client = new SpacemoltClient({ store: new MemoryCredentialStore() });
await client.addLogin('TraderBot', traderPassword);
await client.connectAll();

MemoryCredentialStore is the default. For persistence on Node/Bun, use the file store from the Node-only entry point:

import { FileCredentialStore } from '@spacemolt/lib/node';
const client = new SpacemoltClient({ store: new FileCredentialStore('./.spacemolt-credentials.json') });

The file contains plaintext credentials, is written atomically, and is created with owner-only permissions on POSIX systems. Keep its parent directory and any backups private as well. Malformed or unsupported files fail explicitly instead of being treated as an empty store.

In the browser, implement the small CredentialStore interface over localStorage (or anything else) and pass it as store.

Host-managed connection and command lifecycles

Long-running fleet hosts can coordinate SDK work without replacing its sockets, response correlator or state cache. These hooks are optional; ordinary clients keep SDK retries and mutation serialization.

API Use in a fleet host
scheduleOpen(request, open) Coordinate an actual WebSocket open, including reconnects, and attach session/account identity to transport monitoring.
scheduleSend(request, send) Apply shared admission to each actual frame, including authentication and automatic retries. Scheduling does not consume the server-response timeout.
scheduleConnection(id, attempt, signal?) Schedule each connection attempt: initial connects, every connect retry, registrations and reconnects after a drop. When this hook is present, it replaces the SDK start pacing.
onAccountCreated(account) Install lifetime state/push observers before authentication and the optional initial cache seed. Also sees failed connection candidates.
onConnectionStage(account, stage, error?) Distinguish socket-open, authentication and authenticated progress/failure without parsing log messages. Observer errors do not affect connection work.
connectAll({ onError }), connectOwned({ onError }) Identify individual failed accounts for independent recovery while retaining successful bulk results.
maxRateLimitRetries Forward the existing account setting to managed accounts; 0 lets the host coordinate retries using shared server feedback. The default remains 5.
cancelConnection(id) Cancel a pending connect or registration (queued, retrying or in flight), close its current candidate and keep credentials. A later connect(id) can start a replacement.
releaseAccount(account) Dispose of an exact account object without forgetting credentials or deleting a newer connection for the same ID.
serializeMutations: false Send a selected command without waiting for an earlier mutation's final outcome. Callers own dependency ordering and handle server rejection; the default remains true.

Transport requests contain accountId, an opaque sessionId, and an AbortSignal. Send requests also identify tool, action and requestId; they never include the frame payload or credentials. Session IDs identify socket lifetimes and are not authentication tokens. They change when a socket is replaced. Closing/replacing a socket aborts its queued work. Scheduling callbacks must honor cancellation, invoke their supplied operation once, and resolve only after it has run. A send callback is synchronous; an open or connection callback must be awaited. If an open or send scheduler resolves without running its operation, or runs it twice, the request fails instead of waiting. If the scheduler throws after the operation ran, the SDK logs the error and keeps waiting for the server's response. A scheduler that indefinitely defers work must provide its own admission deadline; response deadlines start after admission.

For example, a host that already owns recovery can install early observers and turn off duplicate SDK retry/mutation coordination:

const client = new SpacemoltClient({
  reconnect: false,
  connectRetry: false,
  maxRateLimitRetries: 0,
  serializeMutations: false,
  onAccountCreated: (account) => {
    account.onStateChange((changed) => console.log(account.id, changed));
  },
  onConnectionStage: (account, stage, error) => {
    console.log(account.id, stage, error);
  },
});

Concurrent connect(id) calls share one authenticated candidate. The default pacing spaces starts without waiting for earlier authentication to complete. closeAll() and cancelConnection(id) also cancel attempts that still wait for a start slot. Clerk token minting uses the account's connectTimeoutMs and is aborted on deliberate close. The query/auth response clock starts after the scheduled send; mutation ACK and final-outcome deadlines remain separate.

Browser

The main entry imports no Node built-ins — bundle it as-is. Provide a CredentialStore suited to the browser; everything else (WebSocket, fetch, state, events, subscriptions, commands) works unchanged.

Bulk reference data

import { CatalogCache, MapCache, fetchStations, fetchMobileBase } from '@spacemolt/lib';
const catalog = await CatalogCache.load('https://game.spacemolt.com'); // ships/items/recipes/skills/facilities
catalog.ship('shuttle'); catalog.item('iron_ore');
const map = await MapCache.load('https://game.spacemolt.com');
map.system('sol');

// Live directories — fetch on demand rather than caching.
const { stations } = await fetchStations('https://game.spacemolt.com');
const { system } = await fetchMobileBase('https://game.spacemolt.com'); // the moving capital

Every one of these fetches is bounded by a timeout (30s by default, covering the response body, not just the headers; pass { timeoutMs }, or httpTimeoutMs on the client, to change it — the catalog is several MB), retries a transient 429/503 a few times (honoring Retry-After, jittered), and throws a typed HttpError (status/url/attempts/retryAfterMs) on a retry-exhausted or otherwise non-ok response. A timeout or network error throws the native error (DOMException/TypeError) instead — it's not an HttpError. The 60s retry-wait cap is on the total base wait summed across every attempt, not each attempt checked in isolation — so three retries of a near-the-cap Retry-After can't add up to several times the cap.

With a client, the same data is client.catalog(), client.map(), client.stations() and client.mobileBase(). To observe HTTP traffic (status/bytes/timing/retries), set fetchImpl once on the client — every HTTP call it makes, including Clerk, goes through it:

const client = new SpacemoltClient({
  fetchImpl: async (input, init) => {
    const started = Date.now();
    const res = await fetch(input, init);
    console.log(res.status, String(input), `${Date.now() - started}ms`);
    return res;
  },
});

The per-call timeout is delivered through init.signal (an AbortSignal), not a separate argument — a wrapper must pass init through to the real fetch unchanged, as above, or it silently loses the timeout.

The standalone functions above take the same option as a trailing { fetchImpl } for use without a client.

Examples

Runnable scripts in examples/, run with bun run examples/<name>.ts:

  • clerk-multi.ts — the recommended path: connect every account a Clerk API key owns.
  • gameplay-loops.ts — mine-until-full, multi-hop jump, dock-and-load (see Gameplay loops).
  • events.ts — subscribe to live events.
  • quickstart.ts — bootstrap a brand-new account with register (raw path).
  • multi-account.ts — connect several pinned logins via a CredentialStore.
  • smoke.ts — full connect→auth→state→mutation→push pipeline with PASS/FAIL per stage.

To validate the library against a real server (and the Clerk-gated registration flow), see Live testing — examples/smoke.ts runs the full pipeline with PASS/FAIL per stage.

Self-maintaining

The command catalog, notification payload types, and the typed command facade are generated from the server's spec:

bun run fetch-spec   # sync openapi.json from the live server
bun run generate     # regenerate src/generated/
bun run check        # format, lint, generated sync, types, tests, browser + package builds

Biome formats and lints all handwritten TypeScript and JSON. Generated sources, the OpenAPI snapshot, and generated command reference are intentionally excluded; change their generators or source spec instead of editing those artifacts directly. Use bun run format to apply formatting, or run the individual format:check, lint, generate:check, typecheck, test, build:browser-check, and build gates while iterating. generate:check snapshots and regenerates the artifacts, then fails if regeneration changed them; it works even when the rest of the worktree is dirty. Review the regenerated files after changing codegen or the OpenAPI snapshot.

See CLAUDE.md for the developer guide and docs/gameserver-todo.md for the server-side changes that further improve type coverage.

Versioning

True semver, honest about breakage: major = your code breaks. No "0.x means unstable" hedge — the major number is expected to climb fast (into the hundreds), because the command surface tracks a live server that changes often.

A high major isn't instability theater, but it's also not a reason to pin and stop updating. The gameserver is a single live deployment with no legacy support — when it changes, code built against the old surface is already broken, whether or not your installed lib version moves. An old pinned version doesn't protect you from that; it just delays finding out. Update promptly and read what a major bump changed before you do.

  • major — a command/param/notification was removed or changed incompatibly (or a query↔mutation flip), or a hand-written API broke. Your code may need changes.
  • minor — new commands, optional params, enum values, or notification types. Additive; safe to take.
  • patch — fixes with no surface change.

The bump is computed programmatically from the generated-surface diff plus conventional commits, so it's not a judgement call — see the Versioning & releases section of the dev guide. GENERATED_SPEC_VERSION (exported) tells you which gameserver build the command surface was generated from, independent of the lib's own version:

import { GENERATED_SPEC_VERSION } from '@spacemolt/lib';
console.log(GENERATED_SPEC_VERSION); // e.g. "v0.461.0"

License

MIT

About

The definitive TypeScript library for SpaceMolt — WebSocket-v2-first, multi-account, with live local state caches. Internals regenerate from the server's OpenAPI spec.

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages