Now on PyPI!

You can now find Stack.PY on PyPI, Python's package index. This means that you can install the package simply by running the following command in a terminal:

pip install stackpy

enter image description here


Stack.PY is based heavily on Stack.PHP and Stack.JS, taking the chained-method concept and applying it to Python. The end result is an extremely easy to use module, named stackpy:

from stackpy import API, Site

# Print the names of all Stack Exchange sites
for site in API.sites:

# Grab the first question on Stack Overflow

Here are some of the other features you can expect from Stack.PY:

  • Caching: currently Stack.PY ships with an SQLite database backend (used for caching currently). By default, if no cache is set, Stack.PY creates an in-memory SQLite database to cache requests for the current session.
  • Full documentation: using a single command (see the README file) you can generate all of the documentation for the entire module - including an explanation for each parameter of every method.

Many more features are planned:

  • A test suite.
  • A series of examples (currently there is one really primitive example).
  • Full support for rate-limiting and the backoff response.

Stack.PY should run perfectly fine in Python 3k using 2to3.


Stack.PY is released under the MIT License.


I can be reached at [email protected].


The code for Stack.PY is hosted here on Launchpad. You can check out the latest code using:

bzr init
bzr pull lp:stackpy

You can view the code online here.

Stack.PY uses distutils so you can install the module by running:

python setup.py install

...or... if you are using Ubuntu, you can add my PPA and install the appropriate package:

sudo apt-get install python-stackpy (for Python 2k)
sudo apt-get install python3-stackpy (for Python 3k)

  • 1
    nice work! But can you tell how to make a API.begin_explicit call? I can't find an example of this.
    – Thomas15v
    Commented Mar 2, 2013 at 20:40
  • @Thomas15v: I've added an explanation and example in an answer below. Commented Mar 2, 2013 at 21:19
  • Can you add a better explanation of what this can do? Commented May 25, 2013 at 15:17
  • @Cody: Do you mean more code examples? Commented May 25, 2013 at 15:25
  • I guess I don't understand what this does Commented May 25, 2013 at 15:26
  • How do I use the /users/me call? The example shows how to pass an ID, but after a user logs in all I've got in the access_token, which me uses to return a user.
    – Andy
    Commented Mar 13, 2014 at 2:10
  • 1
    @Andy: Good question. How this is supposed to work is that you call some_se_site.users.me.access_token('12345'). Unfortunately, the me attribute is mysteriously missing from the source code. I am planning to rewrite this library within the next couple of months, so I will make sure this is added to the list of things to fix. Commented Mar 13, 2014 at 6:30
  • can I access Stack Overflow data in real time using this API ? Commented Aug 15, 2014 at 21:14
  • @RakeshAdhikesavan: Yes, please see the documentation here: api.stackexchange.com/docs/events Commented Aug 16, 2014 at 21:40
  • Where/How to set _ttl=None and disable caching altogether?
    – WooYek
    Commented Mar 20, 2016 at 12:10
  • Unfortunately, I haven't touched the code for this in years. I don't think there was ever a way to adjust _ttl, sadly. Commented Mar 21, 2016 at 6:07

6 Answers 6


Example of Explicit Authentication:

Explicit authentication is relatively straightforward with Stack.PY. Assuming you have already registered your application, the instructions look something like this:

  1. Visit your application's page to obtain the following information:

    • key
    • client ID
    • client secret

  2. Provide Stack.PY with the above information:

    from stackpy import API
    # Fill in the strings with the appropriate values:
    API.key, API.client_id, API.client_secret = "", "", ""
  3. The API.begin_explicit() call consists of the following parameters:

    • a string consisting of the required privileges separated by commas
    • the URI to redirect the user to when the authorization completes
    • an optional string value to be returned with the access token when authorization completes


    redirect_uri = API.begin_explicit('read_inbox,no_expiry',

    The return value of the function is the URL that you will need to redirect the user to. That page will allow the user to authorize your application.

  4. The API.complete_explicit() call consists of the following parameters:

    • the value of the GET parameter code
    • the URI you provided to begin_explicit() above


    access_token = API.complete_explicit(request.GET['code'],

    The return value is the access token for the user.

  • On this line, access_token = API.complete_explicit(request.GET['code'], 'example.com/done'), I am getting an error - Name error: name 'request' is not defined. Why? Commented Dec 30, 2013 at 21:33
  • @lifebalance: That was an example using Django. You will need to use whatever web framework you are using to get the value of the 'code' GET parameter. Commented Dec 30, 2013 at 21:49

Example - Downloading a question with its body

from stackpy import Site

site = Site('stackoverflow')
question = site.questions(1732348).filter('withbody')[0]

print '--- %s ---' % question.title
print question.body
  • Actually, printing a question will print the title by default. So you can do: print question instead of print question.title. Commented May 24, 2013 at 2:10

Question: How do you request all Tags?

Retrieving a list of tags on a given Stack Exchange site with Stack.PY is relatively straightforward:

from stackpy import Site

sa = Site('stackapps')
for t in sa.tags:
    print t

However, this will only fetch the first 30 tags. The tricky part is fetching more than one page. You can fetch any particular page by sticking .page(n) on the end of the request chain (where n is the page to fetch).

If we rewrite the loop a bit, we end up with:

from itertools import count
from stackpy import Site

sa = Site('stackapps')
for p in count(1):
    tags = sa.tags.page(p)
    for t in tags:
        print t
    if not tags['has_more']:

The example above uses a generator that will yield an infinite list of consecutive page numbers to fetch all of the pages. When the has_more property is set to false in the JSON returned by the API, the loop will terminate.

  • 2
    I've edited your answer into a question / answer combo. Please let me know if you have any further questions. Commented May 23, 2013 at 3:42

Do you know, offhand, how complete this library is? Are there parts of the StackExchange API v2.1 that you know are not covered yet?

  • As a server-side library, Stack.PY does not cover implicit auth. Pretty much everything else is fully accessible through the library. Commented May 24, 2013 at 2:11
  • @NathanOsman As a follow up, has this been maintained to support the ability to add content to SE (questions, answers, flags, etc) from version 2.2?
    – Andy
    Commented May 15, 2014 at 15:33
  • @Andy: in theory, it should. I'm using the ask question method on my own website. I haven't done a lot of testing, however. Commented May 15, 2014 at 15:54
  • 1
    @NathanOsman, could you post a short snippet of how that works? I am attempting to a.) get available flag options and then b.) send a flag, but I can't seem to figure out what I need to call to get the available options.
    – Andy
    Commented May 16, 2014 at 14:27

After I installed stackpy, how can I get the docs? I installed doxygen but cannot get Stackpy documentation. Can somebody help me? In fact, I want to know how to search StackOverflow for related questions in my own QA site using your StackExchange API app.

Many thanks.

  • Quote: "Full documentation: using a single command (see the README file) you can generate all of the documentation for the entire module - including an explanation for each parameter of every method.". Is that inaccurate?
    – Mat
    Commented Sep 4, 2013 at 11:32
  • @Mat that documentation looks nice, but doesn't actually tell anything (beyond the names of classes, and their __str__, __init__ etc. members)
    – sehe
    Commented Nov 20, 2013 at 0:50
  • I tried with doxygen in windows and it´s necessary a configuration file that i don´t know where is it Commented Jan 4, 2017 at 10:50

My actual requirement is to find the answers to question with specified tags. That is given a tag I want to get answers to questions with those tag. I am hoping when I get the tag I will get the time of answering as well. How can I do that. More over I am getting the below error. Can anyone help me with this.

    Traceback (most recent call last):
  File "C:\Users\ANI\workspace\StackOverFlow\src\Pydev.py", line 28, in <module>
    question = sa.answers(ans)[0]
  File "C:\Users\ANI\Anaconda-1.9.2\lib\site-packages\stackpy\request.py", line 135, in __getitem__
    return self._fetch()['items'][index] if type(index) == int else self._fetch()[index]
  File "C:\Users\ANI\Anaconda-1.9.2\lib\site-packages\stackpy\request.py", line 157, in _fetch
    self._data = self._url.fetch()
  File "C:\Users\ANI\Anaconda-1.9.2\lib\site-packages\stackpy\url.py", line 87, in fetch
    raise APIError(data['error_id'], data['error_message'])
stackpy.url.APIError: API error 502: too many requests from this IP, more requests available in 84392 seconds.
  • The error you're receiving is caused by exceeding the daily request quota. If you are not using an API key, I would suggest registering for one since it greatly increases the daily limit. If you are using a key, then you will need to contact Stack Exchange to request an increased limit on your key. Commented Jan 8, 2015 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .