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.
Installation
Section titled “Installation”npm install @stackoverflow/teams-sdkyarn add @stackoverflow/teams-sdkpnpm add @stackoverflow/teams-sdkAuthentication Setup
Section titled “Authentication Setup”Before making API calls, you’ll need to set up authentication. The approach varies by your Stack Overflow for Teams tier:
Enterprise - OAuth Available
Section titled “Enterprise - OAuth Available”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/Business - Token Required
Section titled “Basic/Business - Token Required”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/Businessconst teamContext = sdk.forTeam('your-team-id');📖 Need authentication help? See our Authentication Guide for detailed OAuth setup and PAT generation.
Make Your First API Call
Section titled “Make Your First API Call”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); }}Complete Authentication Flow (Enterprise)
Section titled “Complete Authentication Flow (Enterprise)”If using OAuth on Enterprise, here’s a complete authentication flow:
import { StackOverflowSDK, BackendAuthClient } from '@stackoverflow/teams-sdk';
// 1. Set up authentication clientconst authClient = new BackendAuthClient({ clientId: 'your-client-id', redirectUri: 'https://yourapp.com/callback', baseUrl: 'https://your-site.stackenterprise.co'});
// 2. Start OAuth flowconst { url, codeVerifier, state } = await authClient.getAuthUrl();// Redirect user to `url`
// 3. Handle callback and get tokensconst tokens = await authClient.exchangeCodeForToken(code, codeVerifier);
// 4. Initialize SDK with tokenconst sdk = StackOverflowSDK.fromToken( tokens.access_token, 'https://your-site.stackenterprise.co');
// 5. Make authenticated requestsconst questions = await sdk.questions.getAll();Working with Different Content Types
Section titled “Working with Different Content Types”// Get questions with their answersconst questions = await sdk.questions.getAll();
// Get specific question detailsconst questionId = 12345;const question = await sdk.questions.get(questionId);
// Get answers for a questionconst answers = await sdk.answers.getAll(questionId);
// Get specific answerconst answerId = 67890;const answer = await sdk.answers.getAnswerById(answerId);// Get user informationconst users = await sdk.users.getAll();
// Get communitiesconst communities = await sdk.communities.getAll();
// Get user groupsconst userGroups = await sdk.usergroups.getAll();// Browse articlesconst articles = await sdk.articles.getAll();
// Get collectionsconst collections = await sdk.collections.getAll();
// Explore tagsconst tags = await sdk.tags.getAll();
// Get commentsconst questionComments = await sdk.comments.getQuestionComments();const answerComments = await sdk.comments.getAnswerComments();const articleComments = await sdk.comments.getArticleComments();Team Context
Section titled “Team Context”For Stack Overflow for Teams, use the team context:
// Switch to team contextconst teamContext = sdk.forTeam('your-team-id');
// Get team-specific questionsasync 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 answersasync 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); }}Error Handling
Section titled “Error Handling”Always wrap your API calls in try-catch blocks:
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; }}Troubleshooting
Section titled “Troubleshooting”“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