# BridgeClient

Low-level REST client for direct Bridge API access.

## Prefer Hooks

For most use cases, we recommend using the [React hooks](/content/docs/sdk/hooks/index.html) instead of BridgeClient. The hooks provide automatic caching, real-time updates via WebSocket, and proper React integration.

Use BridgeClient only when you need:

- Direct API access outside of React components
- One-off requests without caching
- Custom integration with non-React frameworks

## [Installation](/content/docs/sdk/client#installation/index.html)

BridgeClient is included in the SDK:

```
import { BridgeClient, createBridgeClient } from "@cntrl-pw/sdk";
```

## [Usage](/content/docs/sdk/client#usage/index.html)

```
import { createBridgeClient } from "@cntrl-pw/sdk";

const client = createBridgeClient({
  host: "192.168.1.100",
  port: 9990,
  secure: false,
  apiKey: "your-api-key", // optional
});

// Health check
const isOnline = await client.ping();

// Get system info
const systemInfo = await client.getSystemInfo();
console.log(`Connected to ${systemInfo.hostname}`);
```

## [API Reference](/content/docs/sdk/client#api-reference/index.html)

### [Constructor](/content/docs/sdk/client#constructor/index.html)

```
const client = new BridgeClient(config);
// or
const client = createBridgeClient(config);
```

**Config options:**

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `host` | `string` | required | Bridge IP or hostname |
| `port` | `number` | `9990` | Bridge port |
| `secure` | `boolean` | `false` | Use HTTPS/WSS |
| `apiKey` | `string` | - | API key for authentication |

### [Properties](/content/docs/sdk/client#properties/index.html)

```
client.baseUrl; // HTTP base URL (e.g., "http://192.168.1.100:9990")
client.wsUrl; // WebSocket URL (e.g., "ws://192.168.1.100:9990/api/ws")
```

### [Methods](/content/docs/sdk/client#methods/index.html)

#### [`ping(): Promise<boolean>`](/content/docs/sdk/client#ping-promiseboolean/index.html)

Simple health check. Returns `true` if bridge is reachable.

```
if (await client.ping()) {
  console.log("Bridge is online");
}
```

#### [`getStatus(): Promise<StatusResponse>`](/content/docs/sdk/client#getstatus-promisestatusresponse/index.html)

Get detailed status from `/api/status`.

```
const status = await client.getStatus();
console.log(status.status); // "ok"
```

#### [`getSystemInfo(): Promise<SystemInfo>`](/content/docs/sdk/client#getsysteminfo-promisesysteminfo/index.html)

Get static system information (CPU specs, GPU, memory, etc.).

```
const info = await client.getSystemInfo();
console.log(info.hostname); // "GAMING-PC"
console.log(info.cpu.brand); // "AMD Ryzen 9 5900X"
console.log(info.gpu?.brand); // "NVIDIA GeForce RTX 3080"
console.log(info.memory.total); // 34359738368 (bytes)
```

#### [Power Control](/content/docs/sdk/client#power-control/index.html)

```
await client.shutdown(); // Shut down system
await client.restart(); // Restart system
await client.sleep(); // Put system to sleep
await client.hibernate(); // Hibernate system
```

#### [Process Control](/content/docs/sdk/client#process-control/index.html)

```javascript
// Kill process by PID
await client.killProcess(1234);

// Kill all processes by name
await client.killProcessByName("notepad.exe");

// Launch application
await client.launchProcess("notepad.exe");
await client.launchProcess("code", ["--new-window", "/path/to/project"]);
```

## [Error Handling](/content/docs/sdk/client#error-handling/index.html)

All methods throw on failure:

```
try {
  await client.shutdown();
} catch (error) {
  console.error("Failed to shutdown:", error.message);
}
```

## [Example: Connection Test Script](/content/docs/sdk/client#example-connection-test-script/index.html)

```
import { createBridgeClient } from "@cntrl-pw/sdk";

async function testConnection(host: string) {
  const client = createBridgeClient({ host, port: 9990 });

console.log(`Testing connection to ${host}...`);

const online = await client.ping();
  if (!online) {
    console.log("Bridge is offline");
    return;
  }

console.log("Bridge is online!");

const info = await client.getSystemInfo();
  console.log(`Hostname: ${info.hostname}`);
  console.log(`OS: ${info.os.name} ${info.os.version}`);
  console.log(`CPU: ${info.cpu.brand}`);
  if (info.gpu) {
    console.log(`GPU: ${info.gpu.brand}`);
  }
}

testConnection("192.168.1.100");
```
