# Startup Flow

The full gateway startup sequence involves both systems: the **Config Module** runs before the gateway starts, and the **SDK** is used by the Worker to interact with the running gateway.

## Sequence

```mermaid
flowchart TD
    START["start-openclaw.sh"] --> GUARD{"Gateway\nalready running?"}
    GUARD -- yes --> EXIT["exit 0"]
    GUARD -- no --> ONBOARD{"No config?"}
    ONBOARD -- yes --> SETUP["openclaw onboard\n--non-interactive"]
    ONBOARD -- no --> MOLTLAZY["moltlazy patch\n(generates moltlazy.json\n+ injects $include)"]
    SETUP --> MOLTLAZY
    MOLTLAZY --> LAUNCH["openclaw gateway\n--port 18789\n--allow-unconfigured\n--bind lan"]

    subgraph WORKER["Worker (after port ready)"]
        CLIENT["1. createClient()\nwith gateway token"]
        RPC["2. client.config.set()\nagents.create() etc."]
        CLIENT --> RPC
    end

    LAUNCH --> WORKER
    WORKER --> TUNNEL{"CLOUDFLARE_TUNNEL_TOKEN\nset?"}
    TUNNEL -- yes --> CF["cloudflared tunnel run\n(exposes port 18790)"]
    TUNNEL -- no --> DONE["Gateway configured\nand running"]
    CF --> DONE
```

## Step 1 — Config Module (before gateway starts)

`moltlazy patch` generates `/home/openclaw/.openclaw/moltlazy.json` and injects `$include: "./moltlazy.json"` into `openclaw.json`. OpenClaw natively merges both files on startup.

See [Concepts → Config Module](/llms/platform/libraries/moltlazy/concepts/index.md#config-module-startup) for details.

## Step 2 — Gateway starts

OpenClaw loads `openclaw.json`, resolves the `$include`, and starts listening on port 18789.

## Step 3 — Worker applies runtime config via SDK

Once the port is ready, the Worker creates an RPC client and applies any additional runtime configuration:

```ts
import { createClient } from 'moltlazy/sdk';

const client = createClient({
  baseUrl: 'http://localhost:18789',
  token: process.env.OPENCLAW_GATEWAY_TOKEN!,
});

// Apply a config fragment
await client.config.set({
  raw: JSON.stringify({
    gateway: { auth: { mode: 'token' } },
  }),
});

// Create agents
await client.agents.create({
  name: 'coder',
  workspace: '/home/openclaw/workspace',
  model: 'claude-opus-4',
});
```

## Step 4 — Gateway manages its own persistence

OpenClaw persists all applied config changes internally. The Worker does not manage files.
