Skip to main content

Prerequisites

Step 1: Create an OAuth Application

1

Go to Developer Portal

Navigate to prefid.dev/developer and log in.
2

Create New Application

Click “Create OAuth Application” and fill in:
  • App Name: Your application name
  • Redirect URI: http://localhost:3000/callback (for development)
  • Scopes: Select the domains you need (e.g., preferences:read)
3

Save Credentials

Copy your client_id and client_secret. The secret is only shown once!

Step 2: Install the SDK

npm install @prefid/sdk

Step 3: Initialize PrefID

import { PrefID } from '@prefid/sdk';

const prefid = new PrefID({
  clientId: process.env.PREFID_CLIENT_ID,
  clientSecret: process.env.PREFID_CLIENT_SECRET,
  redirectUri: 'http://localhost:3000/callback'
});

Step 4: Authenticate Users

// Generate authorization URL
const authUrl = prefid.getAuthorizationUrl({
  scopes: ['preferences:read', 'music_preferences'],
  state: 'random-state-string'
});

// Redirect user to authUrl
// After user approves, they're redirected to your callback URL

// Handle callback
app.get('/callback', async (req, res) => {
  const { code, state } = req.query;
  
  // Exchange code for tokens
  const tokens = await prefid.exchangeCode(code);
  
  // Store tokens securely
  // tokens.access_token, tokens.refresh_token
});

Step 5: Fetch Preferences

// Get user preferences
const musicPrefs = await prefid.getPreferences('music_preferences', {
  accessToken: tokens.access_token
});

console.log(musicPrefs);
// {
//   domain: "music_preferences",
//   preferences: {
//     favorite_artists: ["AR Rahman", "Coldplay"],
//     genres: ["Indian Classical", "Electronic"]
//   }
// }

Step 6: Use with AI

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Fetch preferences
const prefs = await prefid.getPreferences('music_preferences');

// Use in AI prompt
const result = await generateText({
  model: openai('gpt-4'),
  system: `User's music preferences: ${JSON.stringify(prefs.preferences)}`,
  prompt: 'Recommend 5 songs for my road trip playlist'
});

Next Steps