📊
Pollz
  • Introduction
  • Quickstart
  • Guides
    • Initializing your app
    • Create a Poll
    • Vote for a Poll
    • Retrieve a Poll
    • Manage Poll Options
    • Listen to update
    • Anonymous Poll
  • Components
    • React
    • React Native
    • Embedded
  • API Reference
    • init()
    • pollOptions()
      • addOption()
      • deleteOption()
      • renameOption()
    • anonymous
      • createAnonymousToken()
      • getAnonymousPoll()
      • voteAnonymously()
    • polls
      • create()
      • get()
      • getAll()
      • vote()
      • rename()
      • deleteOne()
      • listen()
    • pollTypes
      • getAll()
  • Tooling
    • Typescript
    • React
      • usePoll()
      • usePolls()
      • usePollz()
      • usePollTypes()
      • useAnonymousPoll()
Powered by GitBook
On this page
  • Guide: Anonymous Voting with pollz-js SDK
  • Prerequisites
  • Enable Anonymous Voting
  1. Guides

Anonymous Poll

PreviousListen to updateNextComponents

Last updated 1 year ago

Guide: Anonymous Voting with pollz-js SDK

Prerequisites

Before you proceed with enabling anonymous voting using the pollz-js SDK, ensure that you have already and by following the relevant guides in the "Guides" section.

Enable Anonymous Voting

Anonymous voting allows users to participate in polls without revealing their identities. Follow these steps to enable anonymous voting using the pollz-js SDK.

Step 1: Import the SDK

In your Node.js application, ensure you have already imported the pollz-js SDK:

const { PollzSDK } = require('pollz-js');

Step 2: Generate Anonymous Voting Token

Use the createAnonymousToken method provided by the Anonymous module of the SDK to generate an anonymous voting token for a specific poll. Provide the poll ID as an argument.

const pollz = new PollzSDK();

// Replace 'your-app-id' and 'your-app-secret' with actual credentials
const initInput = {
  appId: 'your-app-id',
  appSecret: 'your-app-secret',
};

// Initialize the SDK
await pollz.init(initInput);

// Replace with the actual poll ID
const pollIdForToken = 1;

try {
  const anonymousToken = await pollz.anonymous.createAnonymousToken(pollIdForToken);
  console.log('Anonymous Token:', anonymousToken);
} catch (error) {
  console.error('Failed to generate anonymous token:', error.message);
}

Replace 'your-app-id' and 'your-app-secret' with the actual credentials obtained from the Pollz platform. Adjust the pollIdForToken according to the poll for which you want to generate an anonymous voting token.

Step 3: Vote Anonymously

Users can now vote anonymously using the generated token. Use the voteAnonymously method provided by the Anonymous module of the SDK to submit anonymous votes. Provide the anonymous voting token, chosen option IDs, and an optional user ID.

// Replace with the actual anonymous voting token
const anonymousPollToken = 'your-anonymous-token';

// Replace with the actual option IDs
const optionIdsForAnonymousVote = [1, 2];

// Replace with an optional unique user identifier
const userIdForAnonymousVote = 'user-123';

try {
  const voteResult = await pollz.anonymous.voteAnonymously(anonymousPollToken, optionIdsForAnonymousVote, userIdForAnonymousVote);
  console.log('Anonymous Vote Submitted:', voteResult);
} catch (error) {
  console.error('Failed to submit anonymous vote:', error.message);
}

Replace 'your-anonymous-token' with the actual anonymous voting token generated in the previous step. Adjust the optionIdsForAnonymousVote and userIdForAnonymousVote according to your application's context.

Congratulations! You have successfully enabled and implemented anonymous voting for a poll using the pollz-js SDK. Explore more SDK features and functionalities to enhance your application's polling experience. Refer to the relevant guides for detailed instructions on other SDK capabilities.

initialized the SDK
created a poll