Skip to content

Quickstart

Get your Stack Overflow integration running in minutes with our JavaScript SDK. This quickstart covers installation, authentication setup, initialization, and making your first API calls.

Terminal window
npm install @stackoverflow/teams-sdk

Before making API calls, you’ll need to set up authentication. The approach varies by your Stack Overflow for Teams tier:

Enterprise instances can use built-in OAuth clients or manual tokens:

// Option 1: OAuth setup (recommended for Enterprise)
import { StackOverflowSDK } from '@stackoverflow/teams-sdk';
const sdk = StackOverflowSDK.enterpriseOAuth({
clientId: 'your-client-id',
redirectUri: 'https://yourapp.com/callback',
baseUrl: 'https://your-site.stackenterprise.co'
});
// Option 2: Manual token (if you prefer)
const sdk = StackOverflowSDK.fromToken(
'your-access-token',
'https://your-site.stackenterprise.co'
);

Basic and Business tiers require Personal Access Tokens (PATs):

import { StackOverflowSDK } from '@stackoverflow/teams-sdk';
const sdk = StackOverflowSDK.fromToken(
'your-personal-access-token',
'https://api.stackoverflowteams.com/v3'
);
// Team context required for Basic/Business
const teamContext = sdk.forTeam('your-team-id');

📖 Need authentication help? See our Authentication Guide for detailed OAuth setup and PAT generation.

Start exploring Stack Overflow data with these basic examples:

// For Enterprise (direct SDK usage)
async function getQuestions() {
try {
const questions = await sdk.questions.getAll();
console.log('Recent questions:', questions.items);
return questions;
} catch (error) {
console.error('Failed to fetch questions:', error);
}
}
// For Basic/Business (requires team context)
async function getTeamQuestions() {
try {
const teamContext = sdk.forTeam('your-team-id');
const questions = await teamContext.questions.getAll();
console.log('Team questions:', questions.items);
return questions;
} catch (error) {
console.error('Failed to fetch questions:', error);
}
}

If using OAuth on Enterprise, here’s a complete authentication flow:

import { StackOverflowSDK, BackendAuthClient } from '@stackoverflow/teams-sdk';
// 1. Set up authentication client
const authClient = new BackendAuthClient({
clientId: 'your-client-id',
redirectUri: 'https://yourapp.com/callback',
baseUrl: 'https://your-site.stackenterprise.co'
});
// 2. Start OAuth flow
const { url, codeVerifier, state } = await authClient.getAuthUrl();
// Redirect user to `url`
// 3. Handle callback and get tokens
const tokens = await authClient.exchangeCodeForToken(code, codeVerifier);
// 4. Initialize SDK with token
const sdk = StackOverflowSDK.fromToken(
tokens.access_token,
'https://your-site.stackenterprise.co'
);
// 5. Make authenticated requests
const questions = await sdk.questions.getAll();
qa-example.ts
// Get questions with their answers
const questions = await sdk.questions.getAll();
// Get specific question details
const questionId = 12345;
const question = await sdk.questions.get(questionId);
// Get answers for a question
const answers = await sdk.answers.getAll(questionId);
// Get specific answer
const answerId = 67890;
const answer = await sdk.answers.getAnswerById(answerId);

For Stack Overflow for Teams, use the team context:

team-example.ts
// Switch to team context
const teamContext = sdk.forTeam('your-team-id');
// Get team-specific questions
async function getTeamQuestions() {
try {
const teamQuestions = await teamContext.questions.getAll();
console.log('Team questions:', teamQuestions.items);
return teamQuestions;
} catch (error) {
console.error('Failed to fetch team questions:', error);
}
}
// Get team-specific answers
async function getTeamAnswers() {
try {
const teamAnswers = await teamContext.answers.getAll(question.id);
console.log('Team answers:', teamAnswers.items);
return teamAnswers;
} catch (error) {
console.error('Failed to fetch team answers:', error);
}
}

Always wrap your API calls in try-catch blocks:

error-handling.ts
async function safeApiCall() {
try {
const data = await sdk.questions.getAll();
return data;
} catch (error) {
if (error.status === 401) {
console.error('Authentication failed - check your access token');
} else if (error.status === 429) {
console.error('Rate limit exceeded - please retry later');
} else {
console.error('API call failed:', error.message);
}
throw error;
}
}

“Invalid access token” errors

  • Verify your access token is valid and not expired
  • Ensure you have the necessary permissions for the API endpoint

API calls return empty results

  • Check that you’re using the correct site parameter
  • Verify the endpoint supports the parameters you’re sending

Network timeout errors

  • Implement retry logic with exponential backoff
  • Consider reducing the page size for large data requests