JavaScript SDK
The JavaScript SDK is a small library with utility & convenience methods for interacting with the Polls Platform. The package is isomorphic and can be used in the browser or in a node backend.
Be sure to check out our platform-specific packages which offer more functionality.
Installation
The first step is to install the Polls Platform JavaScript SDK.
- npm
- Yarn
npm install @polls-platform/core
yarn add @polls-platform/core
Create a Poll
Once you have installed the SDK, you're ready to create your first poll.
- TypeScript
- JavaScript
import { initializePolls, DomainConfigType, PollResponse, createPoll, Environment,} from "@polls-platform/core";initializePolls({ apiKey: "YOUR API KEY", domainConfig: { type: DomainConfigType.subdomain, subdomain: "YOUR SUBDOMAIN", }, environment: Environment.production,});// creating a poll is synchronous and instantaneousconst poll: PollResponse = createPoll({ title, ownerId, settings, options,});// the poll can be opened immediately// in a new tab, or embedded in your siteconsole.log("Poll URL", poll.url);
import { initializePolls, DomainConfigType, createPoll, Environment,} from "@polls-platform/core";initializePolls({ apiKey: "YOUR API KEY", domainConfig: { type: DomainConfigType.subdomain, subdomain: "YOUR SUBDOMAIN", }, environment: Environment.production,});// creating a poll is synchronous and instantaneousconst poll = createPoll({ title, ownerId, settings, options,});// the poll can be opened immediately// in a new tab, or embedded in your siteconsole.log("Poll URL", poll.url);
Setting up Poll Data
- TypeScript
- JavaScript
import { createPoll, PollResponse, OptionRequest, OptionType,} from "@polls-platform/core";const options: OptionRequest[] = [ { resourceId: "1", // the id of this resource in your system title: "This is the first option", type: OptionType.defaultTemplate, subtitle: "Subtitles are displayed below the title", details: "Descriptions are on the third line", url: "https://example.com/1", imageUrl: "https://example.com/images/1.png", }, { resourceId: "2", // the id of this resource in your system title: "This is the second option", type: OptionType.defaultTemplate, subtitle: "Subtitles are displayed below the title", details: "Descriptions are on the third line", url: "https://example.com/2", imageUrl: "https://example.com/images/2.png", },];const poll: PollResponse = createPoll({ title: "Which one should we buy?", ownerId: ownerId, settings: { multipleVotes: true, }, options: options,});
import { createPoll, OptionRequest, OptionType } from "@polls-platform/core";const options = [ { resourceId: "1", // the id of this resource in your system title: "This is the first option", type: OptionType.defaultTemplate, subtitle: "Subtitles are displayed below the title", details: "Descriptions are on the third line", url: "https://example.com/1", imageUrl: "https://example.com/images/1.png", }, { resourceId: "2", // the id of this resource in your system title: "This is the second option", type: OptionType.defaultTemplate, subtitle: "Subtitles are displayed below the title", details: "Descriptions are on the third line", url: "https://example.com/2", imageUrl: "https://example.com/images/2.png", },];const poll = createPoll({ title: "Which one should we buy?", ownerId: ownerId, settings: { multipleVotes: true, }, options: options,});
Open the Poll
Once you have created the poll, you can simply open the URL in the user's browser and our platform will take care of everything from there.
Our responsive web app will be styled to match your brand
with your logo on the top and a URL like yourapp.pollsplatform.com
so that users will still feel like they are on your platform.
Tripping Hazard
Some mobile browsers do not allow you to programmatically open a new tab (to protect users from annoying pop-ups).
For this reason, on mobile we recommend navigating the user to the poll in their current tab (this is also the common convention on mobile web, compared to opening a new tab on Desktop).