Skip to content

Instantly share code, notes, and snippets.

@MatthiasWinkelmann
Last active July 11, 2017 11:59
Show Gist options
  • Save MatthiasWinkelmann/e4d03428565a644dd68df68e07990485 to your computer and use it in GitHub Desktop.
Save MatthiasWinkelmann/e4d03428565a644dd68df68e07990485 to your computer and use it in GitHub Desktop.
Minimal example using the StackExchange API
<html>
<head>
<script type='text/javascript' src='https://api.stackexchange.com/js/2.0/all.js'></script>
<script>
// I was a bit stumped because there wasn\'t a single example of how to actually'
// use the StackExchange JS API. As it turns out, you actually don\'t really use it.
// This SE.init() call may or may not get you higher quotas, and it allows your
// users to authenticate. If you don't need those, you can use the REST API without any
// libraries as shown below in getQuestions();",
SE.init({
clientId: 12345,
key: 'J2rQQpZqo1OTHHU9pk2PFsw((',
channelUrl: 'http://localhost:8080/blank',
complete: getQuestions})
function getQuestions(data) {
fetch('https://api.stackexchange.com/2.2/tags/redis/faq?site=stackoverflow&filter=!mSGiMkrKI7')
.then(function(response) {
if(response.ok) {
return response.json();
}
console.log("Error getting Stackoverflow data: " + resonse);
})
.then(function(questions) {
container = window.document.getElementById('stack')
container.innerHTML = questions.items.map((question) => {
return '<h3><a href="'+question.link +'">' + question.title + '</a></h3>';
})
})
}
</script>
</head>
<body>
<div id="stack">This will be replaced with a String of Success</div>
</body></html>
@MatthiasWinkelmann
Copy link
Author

I was a bit stumped because there wasn't a single example of how to actually use the StackExchange JS API. As it turns out, you actually don't really use it.

This SE.init() call may or may not get you higher quotas, and it allows your users to authenticate. If you don't need those, you can use the REST API without any libraries as shown in getQuestions();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment