Skip to content

Quickstart

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

Terminal window
npm install so-teams-sdk
app.ts
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');

Start exploring Stack Overflow data with these basic examples:

api-calls.ts
// Fetch recent questions
async 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 content
async 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 information
async 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);
}
}
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