SDK Documentation
v1.0.0A beginner-friendly guide to get started with the Polybased SDK. This SDK provides type-safe access to Polymarket data including markets, events, trades, and real-time WebSocket streams.
1Installation
Install the SDK using your preferred package manager:
npm install @polybased/sdkpnpm add @polybased/sdkyarn add @polybased/sdk2Import & Initialize
Import the SDK and create a client instance:
import { PolymarketClient } from '@polybased/sdk';
// Initialize the client
const client = new PolymarketClient();
// Optional: with custom config
const clientWithConfig = new PolymarketClient({
apiKey: 'your-api-key', // optional
timeout: 5000,
});3Fetch Markets
Retrieve active markets from Polymarket:
// Get all markets
const markets = await client.getMarkets();
// Get specific market by slug
const market = await client.getMarket('presidential-election-2024');
// Filter markets
const activeMarkets = await client.getMarkets({
closed: false,
limit: 10,
offset: 0,
});
console.log(markets);4Fetch Trades
Get recent trades for a specific market:
// Get trades for a market
const trades = await client.getTrades({
market: 'presidential-election-2024',
limit: 50,
});
// Get trades for a specific outcome
const yesTrades = await client.getTrades({
market: 'presidential-election-2024',
outcome: 'Yes',
limit: 20,
});
console.log(trades);5Real-time WebSocket
Stream live orderbook updates for a market:
// Subscribe to market updates
const ws = client.subscribeToMarket('presidential-election-2024');
ws.on('book', (data) => {
console.log('Orderbook update:', data);
});
ws.on('trade', (data) => {
console.log('New trade:', data);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
// Clean up when done
ws.close();Note for Node.js: If running in Node.js (not browser), you may need to polyfill WebSocket:
import WebSocket from 'ws';
global.WebSocket = WebSocket;6Troubleshooting
WebSocket Not Connecting
Ensure you're using a compatible WebSocket implementation. In Node.js, install and use the ws package.
Node Version
The SDK requires Node.js 18 or higher. Check your version with node --version
Rate Limiting
If you encounter rate limits, implement exponential backoff and respect API rate limits. Consider caching responses when appropriate.
Next Steps
- →Check out the Guides for advanced usage patterns
- →View the GitHub repository for examples and source code
- →Join the community on Twitter