Quickstart
Get your Stack Overflow integration running in minutes with our JavaScript SDK. This quickstart covers installation, initialization, and making your first API calls.
Installation
Section titled “Installation”npm install so-teams-sdk
yarn add so-teams-sdk
pnpm add so-teams-sdk
1. Initialize the SDK
Section titled “1. Initialize the SDK”import { StackOverflowSDK } from 'so-teams-sdk';
const sdk = new StackOverflowSDK({ baseUrl: 'https://api.stackoverflowteams.com/v3', accessToken: 'your-access-token'});
const teamContext = sdk.forTeam('team-slug');
import { StackOverflowSDK } from 'so-teams-sdk';
const sdk = new StackOverflowSDK({ baseUrl: 'https://[your-site].stackenterprise.co/api/v3', accessToken: 'your-access-token'});
2. Make Your First API Call
Section titled “2. Make Your First API Call”Start exploring Stack Overflow data with these basic examples:
// Fetch recent questionsasync function getAll() { try { const questions = await sdk.questions.getAll(); console.log('Recent questions:', questions.items); return questions; } catch (error) { console.error('Failed to fetch questions:', error); }}
// Search for contentasync function searchContent(query: string) { try { const results = await sdk.search.query(query); console.log('Search results:', results.items); return results; } catch (error) { console.error('Search failed:', error); }}
// Get user informationasync function getUser(userId: number) { try { const user = await sdk.users.getUserById(userId); console.log('User details:', user); return user; } catch (error) { console.error('Failed to fetch user:', error); }}
3. Working with Different Content Types
Section titled “3. 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();
4. Team Context
Section titled “4. 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