> For the complete documentation index, see [llms.txt](https://docs.opinion.trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opinion.trade/developer-guide/opinion-clob-typescript-sdk/core-concepts/client.md).

# Client

The `Client` class is the main interface to the Opinion prediction market infrastructure. It provides methods for market data queries, trading, position management, and blockchain interactions.

## Initialization

{% code title="example.ts" %}

```typescript
import 'dotenv/config';
import { Client, CHAIN_ID_BNB_MAINNET, DEFAULT_API_HOST } from '@opinion-labs/opinion-clob-sdk';

const client = new Client({
  host: DEFAULT_API_HOST,
  apiKey: process.env.API_KEY!,
  chainId: CHAIN_ID_BNB_MAINNET,
  rpcUrl: process.env.RPC_URL!,
  privateKey: process.env.PRIVATE_KEY! as `0x${string}`,
  multiSigAddress: process.env.MULTI_SIG_ADDRESS! as `0x${string}`,
});
```

{% endcode %}

{% hint style="warning" %}
Security: Never hardcode API keys or private keys. Always load from environment variables.
{% endhint %}

## Method Categories

| Category         | Description                         | Gas Required       |
| ---------------- | ----------------------------------- | ------------------ |
| Market Data      | Query markets, orderbooks, prices   | No                 |
| Trading          | Place and cancel orders             | No (gas-free CLOB) |
| User Data        | Query positions, balances, trades   | No                 |
| Token Operations | split, merge, redeem, enableTrading | Yes (BNB)          |

## Response Format

All API methods return a consistent response wrapper:

{% code title="example.ts" %}

```typescript
const response = await client.getMarkets();

// Response structure
response.errno    // 0 = success
response.errmsg   // Error message (empty on success)
response.result   // Data payload
```

{% endcode %}

The `ApiResponse<T>` type is defined as:

{% code title="types.ts" %}

```typescript
interface ApiResponse<T> {
  errno: number;
  errmsg: string;
  result: T;
}
```

{% endcode %}

## Caching

The Client implements built-in caching for frequently accessed data:

| Cache          | Default TTL      | Recommendation                   |
| -------------- | ---------------- | -------------------------------- |
| Quote Tokens   | 3600s (1 hour)   | Rarely changes, keep high        |
| Market Data    | 300s (5 minutes) | Balance freshness vs performance |
| Enable Trading | 3600s (1 hour)   | Approvals rarely change          |

Cache TTLs can be configured during initialization:

{% code title="example.ts" %}

```typescript
const client = new Client({
  host: DEFAULT_API_HOST,
  apiKey: process.env.API_KEY!,
  chainId: CHAIN_ID_BNB_MAINNET,
  rpcUrl: process.env.RPC_URL!,
  privateKey: process.env.PRIVATE_KEY! as `0x${string}`,
  multiSigAddress: process.env.MULTI_SIG_ADDRESS! as `0x${string}`,
  quoteTokensCacheTtl: 7200,  // 2 hours
  marketCacheTtl: 120,         // 2 minutes
  enableTradingCheckInterval: 1800, // 30 minutes
});
```

{% endcode %}

Bypass cache for individual calls:

{% code title="example.ts" %}

```typescript
// Bypass cache
const tokens = await client.getQuoteTokens(false);
const market = await client.getMarket(123, false);
```

{% endcode %}

## Best Practices

{% stepper %}
{% step %}

### Single Instance

Create one Client for your application; connection pooling is automatic.
{% endstep %}

{% step %}

### Error Checking

Always check `errno` before accessing `result`.
{% endstep %}

{% step %}

### Cache Tuning

Adjust cache TTLs based on your use case.
{% endstep %}

{% step %}

### Connectivity Verification

Test with `getQuoteTokens()` before trading.
{% endstep %}

{% step %}

### Async/Await

All Client methods are async; always use `await` when calling them.
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.opinion.trade/developer-guide/opinion-clob-typescript-sdk/core-concepts/client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
