Skip to main content
1 of 1
Brock Adams
  • 13k
  • 5
  • 39
  • 64

JavaScript:

Here is a bare-bones javascript, and you can run it from the console of decent modern browsers. It returns the 5 most recently active economics questions, tagged inflation:

fetch (
    'https://api.stackexchange.com/2.2/questions?pagesize=5&tagged=inflation&site=economics'
)
.then (function (response) {
    if (response.ok) { return response.json(); }
} )
.then (rJsn => console.log (rJsn) );

It returns rJsn as an object like:

{ items: Array[5], has_more: true, quota_max: 300, quota_remaining: 288 }

where rJsn.items is the payload and is like:

[
    {
        "tags": ["inflation"],
        "owner": { "display_name": "Bob Umadbro" },
        "view_count": 408,
        "answer_count": 3,
        "score": 6,
        "last_activity_date": 1488986618,
        "creation_date": 1449974376,
        "question_id": 9767,
        "title": "Inflation without Increase in Money Supply?",
        ...
    }, {
        "tags": ["inflation", "growth", "money-supply"],
        "owner": { "display_name": "gabyarg25" },
        "view_count": 91,
        "answer_count": 1,
        "score": 4,
        "last_activity_date": 1488804578,
        "creation_date": 1483498408,
        "question_id": 14897,
        "title": "What happens if money supply grows more slowly than inflation?",
        ...
    },
    ...
]


A more robust starter example:

fetch (
    'https://api.stackexchange.com/2.2/questions?pagesize=5&order=desc&sort=activity&tagged=inflation&site=economics&filter=!Pvz4gBt9fuC8jI)sZ7Hbtli6RDBLFE'
)
.then (function (response) {
    if (response.ok) {
        return response.json();
    }
    throw new Error (`API problem: Error ${response.status}, ${response.statusText}`);
} )
.then (function (rJsn) {
    if (rJsn.backoff  ||  rJsn.error_id) {
        throw new Error (`From API: Backoff ${rJsn.backoff}, Error: ${rJsn.error_id}, ${rJsn.error_message}, ${rJsn.error_name}`);
    }
    console.log (`Quota remaining: ${rJsn.quota_remaining}; Items found: ${rJsn.items.length}`);

    let zQuestions = rJsn.items;
    console.log ("\nQuestion Titles\n---------------");
    for (let qst of zQuestions) {
        console.log (qst.title);
    }
} );

Note:

  1. Always check for error codes and responses!
  2. Always check for the backoff value! If present you do not need to error-out though. Your code can wait the indicated interval and try again.
  3. Monitor quota_remaining. When it gets to 0, you are done for the day.
  4. API key not shown. Just add &key=YOUR_API_KEY to the fetch URL.
  5. Use a filter every time.
  6. For lots of results, monitor has_more and keep fetching the next page as necessary.
Brock Adams
  • 13k
  • 5
  • 39
  • 64