> ## Documentation Index
> Fetch the complete documentation index at: https://docs.brx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# BRX Client

> Interact with the BRX platform programmatically

# BRX Client

The BRX Client is the primary way to interact with the BRX platform programmatically. It provides methods for fetching, creating, updating, and executing BRKs.

## Overview

The BRX Client is available in multiple languages, with the official Node.js SDK (`brx-node`) being the most feature-complete. The client handles:

* Authentication with the BRX platform
* Fetching BRK schemas
* Creating and updating BRKs
* Executing BRKs and handling results
* Managing WebSocket connections for streaming results

## Node.js SDK

### Installation

```bash theme={null}
# For npm:
npm install brx-node -s

# For yarn:
yarn add brx-node

# For pnpm:
pnpm add brx-node
```

### Initialization

To use the BRX Client, you need to initialize it with your API key:

```typescript theme={null}
import BRX from 'brx-node';

// Initialize with your API key
const brx = new BRX('your-api-key');

// Optional configuration
const brxWithOptions = new BRX('your-api-key', {
  verbose: true,           // Enable verbose logging
  send_local: false,       // Use local or production API
  force_client: false,     // Force client-side WebSocket
  silent: false            // Disable welcome message
});
```

## Core Methods

### Fetching BRKs

The `get` method fetches a BRK schema by its ID:

```typescript theme={null}
// Fetch a BRK schema
const brkSchema = await brx.get('brk-id');
```

### Creating BRKs

The `create` method creates a new BRK:

```typescript theme={null}
const createRequest = {
  modifyBrxMode: 'CREATE',
  brx: {
    // BRK definition
  },
  schema: {
    // Schema definition
  }
};

const result = await brx.create(createRequest);
```

### Updating BRKs

The `update` method updates an existing BRK:

```typescript theme={null}
const updateRequest = {
  modifyBrxMode: 'UPDATE',
  brx: {
    // Updated BRK definition
  },
  schema: {
    // Updated schema definition
  }
};

const result = await brx.update(updateRequest);
```

### Executing BRKs

The `run` method executes a BRK:

```typescript theme={null}
import { BRK } from 'brx-node';

// Initialize a BRK with a schema
const myBrk = new BRK(brkSchema);

// Set input values
myBrk.input['inputField'] = 'input value';

// Execute the BRK
const result = await brx.run(myBrk);
```

### Using Callbacks

The `run` method also supports callbacks for handling streaming results:

```typescript theme={null}
const results = [];

await brx.run(myBrk, (result) => {
  // Handle each result as it comes in
  results.push(result);
  console.log(result);
});

// All results are also returned when the promise resolves
console.log(results);
```

## Advanced Usage

### WebSocket Connections

The BRX Client uses WebSockets for real-time communication with the BRX platform. This allows for streaming results as they become available, rather than waiting for the entire execution to complete.

```typescript theme={null}
// The run method handles WebSocket connections automatically
const result = await brx.run(myBrk);

// You can also use the execute method directly
const result = await brx.execute(myBrk);
```

### Error Handling

The BRX Client throws errors for various failure conditions:

```typescript theme={null}
try {
  const result = await brx.run(myBrk);
} catch (error) {
  console.error('Error executing BRK:', error);
}
```

Common errors include:

* Authentication failures
* Invalid BRK schemas
* Execution errors
* Network issues

## Best Practices

* **Reuse the BRX Client**: Create a single instance of the BRX Client and reuse it for multiple operations
* **Handle errors gracefully**: Implement proper error handling to provide a good user experience
* **Use callbacks for streaming**: When executing long-running BRKs, use callbacks to provide real-time feedback
* **Validate inputs**: Ensure that input values match the expected types and formats
* **Manage API keys securely**: Store API keys securely and never expose them in client-side code
