Poll TypeScript API Client

Use typed Poll REST API methods in Node.js, web, and server applications.

Install

npm install @solworks/poll-api-client

The package exports PollClient, PollApiError, and the TypeScript types for API data.

Read public data

You do not need a token to read public bets. Create a client with the production API URL and call a public method.

public-bets.ts
import { PollClient } from "@solworks/poll-api-client";

const client = new PollClient({
  apiUrl: "https://api.poll.fun",
});

const bets = await client.getTrendingBets();

for (const bet of bets) {
  console.log(bet.question);
}

Authenticated requests

Pass a Personal Access Token when you create the client. Keep the token on the server. Do not put it in browser code or source control.

account.ts
import { PollClient } from "@solworks/poll-api-client";

const client = new PollClient({
  apiUrl: "https://api.poll.fun",
  token: process.env.POLL_API_TOKEN,
  clientId: "my-poll-integration",
});

const account = await client.getAccount();
console.log(account.displayName);

The client sends the token in the Authorization header. Review the available scopes before you create the token.

Pagination

Use the paginated method when you need the total count, offset, or next-page state.

typescript
const result = await client.listPublicBetsPaginated({
  limit: 25,
  offset: 0,
  excludeResolved: true,
});

console.log(result.items);
console.log(result.pagination.hasMore);

Error handling

typescript
import { PollApiError } from "@solworks/poll-api-client";

try {
  await client.getAccount();
} catch (error) {
  if (error instanceof PollApiError) {
    console.error(error.statusCode, error.errorMessage);
    console.error(error.rawResponse);
  }
}

A network failure uses status code 0. An API response uses its HTTP status code.