Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

4️⃣ Assemble the dApp

Assemble the dApp

Audience: Someone who has made every call in steps 1–3 by hand.

Goal: Assemble those calls into the Run Watcher — one dependency-free Node file that serves a browser page listing recent executions and updating live from the stream.

The shape

The browser never sees the API key — EventSource cannot send an x-api-key header even if you wanted it to. So the dapp is a tiny relay: one Node server that holds the key, forwards REST and stream routes to the Nexus API, and serves the page.

Browser ──GET /────────────▶ server.mjs ── (static HTML)
Browser ──GET /api/runs────▶ server.mjs ──x-api-key──▶ GET /executions
Browser ──GET /api/events/status──▶ server.mjs ──x-api-key──▶ GET /events/stream (status probe)
Browser ──GET /api/events──▶ server.mjs ──x-api-key──▶ GET /events/stream  (relayed SSE)

The relay is the authority boundary for the API key and for disconnect cancellation; the browser owns only same-origin routes and resume cursors. This sample enforces an origin-root NEXUS_API_URL with no path, query, or fragment, then appends the documented resource paths at the root. A provider path prefix requires a separate reviewed adapter that preserves that prefix.

The sequence is intentionally HTTP-only: the relay forwards indexed reads and events, while any state-changing Sui transaction still needs the separate signer boundary described by the Concepts and SDK references. The status probe exists because native EventSource.onerror does not expose the upstream HTTP status; it lets the page close on fatal 401/403 instead of retrying forever.

The whole dapp

Save as server.mjs — no npm install, nothing but Node 18+:

The relay forwards the upstream status and body instead of replacing API errors with a local 502, and forwardHeaders forwards only content type, request/error IDs, retry, and canonical rate-limit headers; cookies, redirects, authentication challenges, CSP, cache controls, and transport/body-framing headers never cross the origin boundary. The handlers set local cache/SSE policy, the AbortController cancels the authenticated fetch when the browser closes the request, and the listener binds only to 127.0.0.1 for this local teaching example. parseRunPage branches on status, content type, JSON shape, and bounded body detail so envelope-free 408/413, JSON 429, plaintext 502, malformed JSON, wrong-list responses, and malformed execution items render as retryable or fatal states instead of unhandled rejections. validateExecutionItem requires safe object_id, token-shaped status, RFC3339 created_at, and bounded supported optional fields before any row is constructed. The table uses DOM nodes and textContent, so API strings cannot become markup or event handlers. The event path classifies the status-probe HTTP status and content type before parsing an optional JSON envelope, so envelope-free 401/403 remain fatal while 408/429/5xx remain bounded-retry states. It accepts only a 2xx stream response whose MIME is text/event-stream (parameters such as charset are allowed); a wrong-MIME 2xx or malformed probe is fatal but manually recoverable. It validates nonnegative event IDs and rejects duplicate/backwards frames, stores the last validated ID, and forwards it as last_event_id when a replacement EventSource is opened. Disconnects use three attempts with capped exponential backoff, close the source after the limit, expose Retry, and merge independent REST/SSE states so a REST fatal is not overwritten by a healthy stream.

The provider may omit tx_digest when that field is not projected for an event. The raw parser accepts the omission, rejects an empty or non-string digest when the property is present, and renders an omitted value as an empty display suffix; this matches the optional EventFrame contract used by the React consumer. If the provider rejects a stale or reset cursor with HTTP 400, the page exposes Reset cursor and reload; use it only when the provider documents restarting at the current tip, because a browser reload discards the stored Last-Event-ID.

Run the parser regression without starting the server:

Re-enter the key and run it safely

Step 3 deliberately cleaned its API key before you reached this page. Re-enter it without echoing, keep it inside a subshell, and let the scoped runner terminate the Node child and clean the variable on normal exit, launch failure, SIGINT, or SIGTERM; the exported value is still visible to same-user process inspection while the child runs.

The subshell boundary prevents the key, cleanup function, and traps from entering the shell that continues to step 5. A missing key or a failed Node launch returns nonzero after cleanup; Ctrl-C or terminating the subshell forwards termination to the child before cleanup completes.

Open http://127.0.0.1:3000. The table is step 2's filtered list; the feed is step 3's stream; the relay is step 1's provider-issued key doing its job server-side. Trigger a run from the CLI or SDK and watch it appear without a refresh. To exercise recovery, use an intentionally invalid or insufficiently scoped test credential and confirm the status probe renders fatal 401/403 without an endless EventSource loop; ask the provider to restore or rotate access, correct the relay configuration, and use the visible Retry action.

Where the toy stops

Two habits to grow out of before this pattern carries real traffic:

  • Backoff on 429. Each scope's token bucket is finite; refresh() on every event is fine on a quiet deployment and rude on a busy one. Debounce it, or drive the table straight from the event payloads.

  • Detail pages. Each row's object_id unlocks the whole sub-resource tree from step 2 — walks, events, verdicts, payment ledger. The API reference lists them all.

The bigger step is keeping the handwritten plumbing small and auditable: the TypeScript consumer page extracts the same public response types, cursor rules, status/content-type/schema branching, and relay-header policy into ordinary application code. This example is a loopback-bound teaching relay, not a production authorization, CSRF, observability, or deployment policy.

Next: port the Run Watcher to React on the typed client.

Last updated