We are going to get the top 5 Seasoned Advice (a StackOverflow sister site) questions tagged with pie, and display them in order of votes.

Getting started

The API docs let you try out the query directly on the site. Let’s do that first, to get a sense of the data returned.

Go to the search docs, scroll down to the form and where it says “Stack Overflow” hit “Edit”, type in “Seasoned Advice” and select it from the drop-down list. Then set the following options:

  • “page”: 1
  • “pagesize”: 5
  • “sort”: votes
  • “tagged”: pie

This should give you https://api.stackexchange.com/2.2/search?page=1&pagesize=5&order=desc&sort=votes&tagged=pie&site=cooking, which returns the top five pie-tagged questions. You can hit “Run” to see the result directly on the page, or go to the URL in a new tab.

Python example

The easiest way (for me) to get started is with a small Python script, and we can use the Python answer to the Hello world question on StackApps for guidance.

Create a file pie.py:

#!/usr/bin/env python

import requests



SO_URL = "https://api.stackexchange.com/2.2/search"

params = {
    "site": "cooking",
    "page": 1,
    "pagesize": 5,
    "order": "desc",
    "sort": "votes",
    "tagged": "pie"
}

response = requests.get(SO_URL, params=params)

# The text response will give us a dictionary
# where the key "items" contains a list of questions
questions = response.json()["items"]

# Print the title of the question
for index, question in enumerate(questions):
    pretty = "{}. {}".format(index + 1, question["title"])
    print(pretty)

# Print remaining quota
print("\nYou have {} requests left today.".format(response.json()["quota_remaining"]))

Now run the script:

$ python pie.py
1. Apple pie: peel or not?
2. Is cheesecake a cake or a pie?
3. Is there an alternative to cornstarch when used to thicken a pie filling
4. Is there a way to prevent my Fruit Pie from getting runny?
5. What is seeping out of my key lime pie?

You have 299 requests left today.

JavaScript example

Create a file simple.html and add this content:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
    <script type='text/javascript'>
    // Credits:
    // https://stackapps.com/questions/2/api-hello-world-code/7236#7236
    //
    fetch (
        'https://api.stackexchange.com/2.2/questions?pagesize=5&order=desc&sort=votes&tagged=pie&site=cooking'
    )
    .then (function (response) {
        // Check for error in response
        if (response.ok) {
            return response.json();
        }
        throw new Error (`API problem: Error ${response.status}, ${response.statusText}`);
    })
    .then (function (json) {
        // Check the backoff value
        // TODO: Instead of throwing an error, wait json.backoff seconds before retrying
        if (json.backoff  ||  json.error_id) {
            throw new Error (`From API: Backoff ${json.backoff}, Error: ${json.error_id}, ${json.error_message}, ${json.error_name}`);
        }
        console.log (`Quota remaining: ${json.quota_remaining}; Items found: ${json.items.length}`);
    
        let allQuestions = json.items,
            showText = "",
            index = 0;
        console.log ("\nQuestion Titles\n---------------");
        for (let question of allQuestions) {
            index++;
            console.log (question.title);
            showText = showText + "<p>" + index + ". " + question.title + "</p>";
        }
        console.log("showText: " + showText)
        document.getElementById("questions").innerHTML = showText;
    } );
    </script>
  <body>
    <div id="questions"></div>
  </body>
</html>

Get the full path to the file, and enter it in your browser, for example: /Users/jdoe/Desktop/simple.html. As long as you’re connected to the internet you should see the top 5 questions displayed.

Register

The request limit is 300 requests per day per IP for anonymous users. In order to get more, you should register your application.

Registering looks (and is) daunting at first, because you must provide a domain name that your applciation will be using for OAuth. However, there is a trick/method for registering even if you don’t have a domain. These are the steps:

  1. Go to Register Your V2.0 Application.
  2. Fill in an Application Name and Description of your choice.
  3. Enter stackexchange.com as the Application Website.
  4. Enter stackexchange.com as the OAuth Domain.

You can then use https://stackexchange.com/oauth/login_success as the redirect_uri when calling the API.