Quick Start

Get up and running with the Opinion CLOB TypeScript SDK in minutes. This guide will walk you through your first integration.

Prerequisites

Before starting, ensure you have:

circle-info

Need credentials? Fill out this short application formarrow-up-right to get your API key.

5-Minute Quickstart

1

Set Up Environment

Create a .env file in your project directory:

.env
# .env file
API_KEY=your_api_key_here
RPC_URL=https://bsc-dataseed.binance.org
PRIVATE_KEY=0x1234567890abcdef...
MULTI_SIG_ADDRESS=0xYourWalletAddress...
2

Initialize the Client

Create a new TypeScript file (my_first_app.ts):

my_first_app.ts (initialization)
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, // 56
  rpcUrl: process.env.RPC_URL!,
  privateKey: process.env.PRIVATE_KEY! as `0x${string}`,
  multiSigAddress: process.env.MULTI_SIG_ADDRESS! as `0x${string}`,
});

console.log('Client initialized successfully!');
3

Fetch Market Data

Add market data fetching:

fetch-markets.ts
import { TopicStatusFilter } from '@opinion-labs/opinion-clob-sdk';

// Get all active markets
const marketsResponse = await client.getMarkets({
  status: TopicStatusFilter.ACTIVATED,
  page: 1,
  limit: 10,
});

// Parse the response
if (marketsResponse.errno === 0) {
  const markets = marketsResponse.result.list;
  console.log(`\nFound ${markets.length} active markets:`);

  for (const market of markets.slice(0, 3)) {
    console.log(`  - Market #${market.market_id}: ${market.market_title}`);
    console.log(`    Status: ${market.status}`);
  }
} else {
  console.error(`Error: ${marketsResponse.errmsg}`);
}
4

Get Market Details

// Get details for a specific market
const marketId = markets[0].market_id;

const marketDetail = await client.getMarket(marketId);
if (marketDetail.errno === 0) {
  const market = marketDetail.result.data;
  console.log(`\nMarket Details for #${marketId}:`);
  console.log(`  Title: ${market.market_title}`);
  console.log(`  Question ID: ${market.question_id}`);
  console.log(`  Quote Token: ${market.quote_token}`);
  console.log(`  Chain ID: ${market.chain_id}`);
}
5

Check Orderbook

const tokenId = 'your_token_id_here'; // Replace with actual token ID

try {
  const orderbook = await client.getOrderbook(tokenId);
  if (orderbook.errno === 0) {
    console.log(`\nOrderbook for token ${tokenId}:`);
    console.log('Orderbook:', JSON.stringify(orderbook.result, null, 2));
  }
} catch (e) {
  console.log(`  (Skip if token_id not set: ${e})`);
}
6

Complete Example

Here is the full my_first_app.ts combining all the steps above:

my_first_app.ts
import 'dotenv/config';
import {
  Client,
  CHAIN_ID_BNB_MAINNET,
  DEFAULT_API_HOST,
  TopicStatusFilter,
} from '@opinion-labs/opinion-clob-sdk';

async function main() {
  // Initialize client
  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}`,
  });
  console.log('Client initialized successfully!');

  // Get active markets
  const marketsResponse = await client.getMarkets({
    status: TopicStatusFilter.ACTIVATED,
    limit: 5,
  });

  if (marketsResponse.errno === 0) {
    const markets = marketsResponse.result.list;
    console.log(`\nFound ${markets.length} active markets\n`);

    // Display markets
    markets.forEach((market, i) => {
      console.log(`${i + 1}. ${market.market_title}`);
      console.log(`   Market ID: ${market.market_id}`);
      console.log();
    });

    // Get details for first market
    if (markets.length > 0) {
      const firstMarket = markets[0];
      const detail = await client.getCategoricalMarket(firstMarket.market_id);

      if (detail.errno === 0) {
        const m = detail.result.data;
        console.log(`Details for '${m.market_title}':`);
        console.log(`  Status: ${m.status}`);
        console.log(`  Question ID: ${m.question_id}`);
        console.log(`  Quote Token: ${m.quote_token}`);
      }
    }
  } else {
    console.error(`Error fetching markets: ${marketsResponse.errmsg}`);
  }
}

main();

Run Your App

# Install dotenv if not already installed
npm install dotenv

# Run the script
npx tsx my_first_app.ts

Expected Output:

Next Steps

Now that you've fetched market data, explore more advanced features:

Trading

Learn how to place orders:

See Placing Orders for detailed examples.

Position Management

Track your positions:

See Managing Positions for more.

Smart Contract Operations

Interact with blockchain:

See Contract Operations for details.

Common Patterns

Error Handling

Always check response status:

Using Try-Catch

Pagination

For large datasets:

Configuration Tips

Cache Settings

Optimize performance with caching:

Set to 0 to disable caching:

Chain Selection

For production deployment, ensure you're using the correct configuration:

Resources


Ready to build? Explore the API Reference to see all available methods!

Last updated