TypeScript SDK
Use the TypeScript SDK from Node.js scripts, apps, or services.
Install
Run these commands in a terminal. Use Node.js 18+ for built-in fetch.
mkdir polybridge-ts-test
cd polybridge-ts-test
npm init -y
npm install @polybridge-ai/sdk
npm install -D typescript tsx @types/nodeQuick terminal test
This quick test uses the SDK for Search and REST for Forecast, so it works without an API key.
API key for higher usage
Without a key, the example uses anonymous limits. With a key, it uses higher API-key limits where supported. Do not put API keys in files you commit.
export POLYBRIDGE_API_KEY="your_api_key_here"Create test.ts
This file runs Search first, then Forecast. Forecast can take longer than Search.
import { PolyBridge } from "@polybridge-ai/sdk";
type ForecastMarket = {
question?: string;
};
type ForecastResponse = {
request_id?: string;
question?: string;
probability?: number | null;
confidence?: number | null;
confidence_interval?: unknown;
latency_ms?: number;
error?: string | null;
reasoning?: string | null;
markets_used?: ForecastMarket[];
};
const apiKey = process.env.POLYBRIDGE_API_KEY?.trim();
const client = new PolyBridge({
...(apiKey ? { apiKey } : {}),
timeoutMs: 10_000,
clientName: "typescript-quick-test",
});
async function main() {
console.log("Running Search...");
const searchResults = await client.search("Fed rate cut before September 2026", {
dimensions: ["direct", "correlated"],
topKPerDimension: 3,
filters: { status: "active" },
});
console.log({
requestId: searchResults.request_id,
query: searchResults.query,
totalMarkets: searchResults.total_markets,
latencyMs: searchResults.latency_ms,
});
for (const [dimension, markets] of Object.entries(searchResults.results)) {
console.log(`\n${dimension}:`);
for (const market of markets.slice(0, 3)) {
console.log(`- ${market.question}`);
}
}
console.log("\nRunning Forecast...");
console.log("Forecast can take longer than Search.");
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 90_000);
const forecastHeaders: Record<string, string> = {
"Content-Type": "application/json",
};
if (apiKey) {
forecastHeaders.Authorization = `Bearer ${apiKey}`;
}
try {
const response = await fetch("https://api.polybridge.ai/v1/forecast", {
method: "POST",
headers: forecastHeaders,
body: JSON.stringify({
question: "Will the Fed cut interest rates before September 2026?",
include_graph: false,
}),
signal: controller.signal,
});
if (!response.ok) {
const body = await response.text();
throw new Error(`Forecast failed with ${response.status}: ${body}`);
}
const forecast = (await response.json()) as ForecastResponse;
console.log({
requestId: forecast.request_id,
question: forecast.question,
probability: forecast.probability,
confidence: forecast.confidence,
confidenceInterval: forecast.confidence_interval,
latencyMs: forecast.latency_ms,
error: forecast.error,
});
console.log("\nReasoning:");
console.log(forecast.reasoning);
console.log("\nMarkets used:");
for (const market of forecast.markets_used ?? []) {
console.log(`- ${market.question}`);
}
} finally {
clearTimeout(timeout);
}
}
main().catch((error) => {
console.error("PolyBridge test failed:");
if (error instanceof Error) {
console.error(`${error.name}: ${error.message}`);
} else {
console.error(error);
}
process.exit(1);
});Run it
You should see Search results first, then a Forecast response. The example uses SDK Search and REST Forecast with a 90-second Forecast timeout.
npx tsx test.tsUse in your app
Once the terminal test works, use the same client setup in your app, service, script, worker, or agent backend. Keep the API key in an environment variable or secret manager. Do not hardcode it in source files.
If something fails
Check the error and fix the simplest cause first.
- 401: API key is invalid. Remove the key to use anonymous mode, or replace it with a valid key.
- 403: key does not have access for that operation.
- 429: wait before retrying or use an API key for higher usage.
- Timeout: Forecast can take longer than Search.