Polls API Integration Example
1 Scope & Goal
Enable a client application to surface live Polls data that originates in Common.
Read-only access to Poll objects
Periodic refresh of results for live tallies
2 Commonwealth API – Polls Contract
Get polls for a thread
POST /api/v1/GetPolls
id, prompt, options[], ends_at, created_at, updated_at, votes[]
Get votes for a specific poll
POST /api/v1/GetPollVotes
id, poll_id, option, address, user_id, weight, created_at
Create a new poll
POST /api/v1/CreatePoll
id, prompt, options[], ends_at, thread_id, community_id
Vote on a poll
POST /api/v1/CreatePollVote
Vote object: poll_id, option, address, community_id
Details:
Protocol: tRPC over HTTP
Authentication: API Key (x-api-key) + Address (address)
Rate Limits:
Tier 1: 10 req/min
Tier 2: 60 req/min
Headers: RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset
Base URL: /api/v1/
Version: 2.0.0
3 Data Models
Notes:
Polls are thread-scoped
Vote weights calculated server-side
ends_at null = infinite
4 Polling Strategy
Bootstrap
On startup, call GetPolls for target threads
Cache by poll.id
Periodic Refresh
Every 60s: re-fetch GetPolls
Compare updated_at to detect changes
Vote Fetch
On change or first load, call GetPollVotes
Aggregate client-side
Rate-Limit Handling
On 429, back off exponentially: 60 → 120 → 240 → 300s
Merge & Emit
Update cache and fire polls.updated event
Housekeeping
Stop polling when polls closed and view hidden
No incremental endpoint available; rely on updated_at diffing.
5 Authentication & Headers
Include both headers on every call.
6 Example Client Data Model
Aggregation Logic:
Cache in IndexedDB/localStorage for fast cold starts.
7 UX Hooks
polls.loading
Show skeleton/shimmer
polls.updated
Animate count change; toast optional
Poll expired
Mark closed, disable voting
Rate limited
Show back-off indicator
8 Testing Checklist
Unit: Mock tRPC, test aggregation & back-off
Integration: Staging API keys, multiple poll states
Load: Simulate 10+ clients @60s intervals
Auth: Invalid/missing headers → 401
Offline/Online: Cache persists; first refresh uses cache
9 Sample Implementation
Extract core patterns into service and manager classes.
CommonwealthPollService
PollManager
10 Action Items
Backend: Obtain API keys; identify thread IDs
Frontend: Build tRPC wrapper & aggregation utils
DevOps: Monitor errors, rate limits, latency
Docs: Publish API key guide & thread discovery
Last updated
Was this helpful?
