Skip to main content

TypeScript SDK

@contextwtf/sdk · v0.3.5 · TypeScript · viem · ISC License The Context SDK wraps the REST API and handles EIP-712 order signing, so you can read market data and place trades with a few lines of code.

Install

npm install @contextwtf/sdk
The SDK requires Node.js 18+ and is published as @contextwtf/sdk.

Quick start

Read market data (no auth)

import { ContextClient } from "@contextwtf/sdk"

const ctx = new ContextClient()

// List trending markets
const { markets } = await ctx.markets.list({ sortBy: "trending", limit: 5 })

for (const market of markets) {
  console.log(market.question)
}

// Get quotes for a market
const quotes = await ctx.markets.quotes(markets[0].id)
console.log(`Yes: ${quotes.yes.bid}¢ bid / ${quotes.yes.ask}¢ ask`)

Place an order (with signer)

import { ContextClient } from "@contextwtf/sdk"

const ctx = new ContextClient({
  apiKey: process.env.CONTEXT_API_KEY!,
  signer: { privateKey: process.env.PRIVATE_KEY! as `0x${string}` },
})

// Buy 10 Yes contracts at 45¢
await ctx.orders.create({
  marketId: "0x...",
  outcome: "yes",
  side: "buy",
  priceCents: 45,
  size: 10,
})

Full onboarding flow

const ctx = new ContextClient({
  apiKey: process.env.CONTEXT_API_KEY!,
  signer: { privateKey: process.env.PRIVATE_KEY! as `0x${string}` },
})

// Check wallet status and setup if needed
const status = await ctx.account.status()
if (!status.isOperatorApproved) {
  await ctx.account.setup()
}

// Mint test USDC and deposit (testnet only)
await ctx.account.mintTestUsdc(1000)
await ctx.account.deposit(1000)

// Place a trade
await ctx.orders.create({
  marketId: "0x...",
  outcome: "yes",
  side: "buy",
  priceCents: 45,
  size: 10,
})

Client options

interface ContextClientOptions {
  apiKey?: string      // API key for authenticated endpoints
  baseUrl?: string     // Override API base URL (default: testnet)
  rpcUrl?: string      // Custom RPC URL for on-chain operations
  signer?: SignerInput // Wallet signer for order signing and transactions
}
The SDK supports three signer formats: private key string, viem Account object, or viem WalletClient. See best practices for details.

Modules

ModuleMethodsPurpose
ctx.markets11Market data, quotes, orderbooks, price history, oracle
ctx.orders13Order placement, cancellation, bulk operations, lifecycle
ctx.portfolio5Positions, balances, claimable winnings, stats
ctx.account9Wallet setup, deposits, withdrawals, gasless operations
ctx.questionsMarket creation

Pricing conventions

ValueFormatExample
PricesCents (1-99)45 = 45¢ = 45% probability
SizesContracts10 = 10 contracts
OutcomesString"yes" or "no"
The SDK handles all on-chain encoding (BigInts, outcome indices, TUSD decimals) internally.

Network

Currently targeting Base Sepolia testnet (chain ID 84532).
ContractAddress
Settlement0xCB6CBCb87fe36Dd48b08930867C8D1E5fDDeE251
Holdings0x3A81C17a9bf6D5d425fbF67C4BE8aA279f8F6F95
MarketFactory0x333271eB9a252F3CCc5fd9e6026B09C717Df01d5
TUSD0xBbee2756d3169CF7065e5E9C4A5EA9b1D1Fd415e