OpenClaw SDK
    Preparing search index...

    Class OpenClawClient

    OpenClaw Client

    Main client class for connecting to and interacting with an OpenClaw Gateway. Provides methods for connection lifecycle, request/response handling, and state queries.

    Index

    Constructors

    Accessors

    • get isConnected(): boolean

      Check if the client is currently connected.

      Returns boolean

      True if the connection state is 'ready'

      if (client.isConnected) {
      console.log("Ready to send requests");
      }
    • get connectionState(): ConnectionState

      Get the current connection state.

      Returns ConnectionState

      The current ConnectionState ('disconnected' | 'connecting' | 'ready' | 'closing')

      console.log("State:", client.connectionState);
      // Output: State: ready

    Methods

    • Connect to the OpenClaw Gateway.

      Returns Promise<void>

      Promise that resolves when the connection is established

      Error if connection fails

      const client = createClient({
      url: "ws://localhost:8080",
      clientId: "my-app"
      });

      await client.connect();
      console.log("Connected:", client.isConnected);
    • Disconnect from the OpenClaw Gateway.

      Returns void

      client.disconnect();
      console.log("Disconnected:", client.isConnected);
    • Send a request to the OpenClaw Gateway and wait for a response.

      Type Parameters

      • T = unknown

      Parameters

      • method: string

        The method name to invoke

      • Optionalparams: unknown

        Optional parameters for the method

      • Optionaloptions: RequestOptions

        Optional request options

      Returns Promise<T>

      Promise that resolves with the response payload

      Error if the request fails or times out

    • Abort a pending request by ID.

      Parameters

      • requestId: string

        The request ID to abort

      Returns void

      // Abort a specific request
      client.abort("req-123-456");
    • Register a handler for connection state changes.

      Parameters

      • handler: (state: ConnectionState) => void

        Function to call when connection state changes

      Returns () => void

      Unsubscribe function to remove the handler

      const unsub = client.onStateChange((state) => {
      console.log("State changed to:", state);
      });

      // Later: unsub();
    • Register a handler for connection errors.

      Parameters

      • handler: (error: Error) => void

        Function to call when an error occurs

      Returns () => void

      Unsubscribe function to remove the handler

      const unsub = client.onError((error) => {
      console.error("Connection error:", error.message);
      });
    • Register a handler for incoming gateway frames.

      Parameters

      • handler: (frame: GatewayFrame) => void

        Function to call when a frame is received

      Returns () => void

      Unsubscribe function to remove the handler

      const unsub = client.onMessage((frame) => {
      console.log("Received frame:", frame.type);
      });
    • Register a handler for connection close events.

      Parameters

      • handler: () => void

        Function to call when the connection closes

      Returns () => void

      Unsubscribe function to remove the handler

      const unsub = client.onClosed(() => {
      console.log("Connection closed");
      });