Skip to content
Wrapping web APIs made easy.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests Re-implement tests and move to codecov for coverage Apr 20, 2018
tortilla Create child wraps using self.__class__ so it is possible to create s… Feb 18, 2019
.gitignore Add pytest cache to gitignore Apr 7, 2018
CHANGES.rst Version 0.5.0 Apr 13, 2018
LICENSE Bump year Apr 6, 2018
README.rst Re-implement tests and move to codecov for coverage Apr 20, 2018
setup.cfg Oops, use reStructuredText for PyPI Nov 27, 2014 Upped lowest supported six version Feb 22, 2019
tox.ini Upped lowest supported six version Feb 22, 2019



build coverage docs version pyversions license

Wrapping web APIs made easy.

Installation via PIP:

pip install tortilla

Quick usage overview:

>>> import tortilla
>>> github = tortilla.wrap('')
>>> user = github.users.get('octocat')
>>> user.location
'San Francisco'

The Basics

Tortilla uses a bit of magic to wrap APIs. Whenever you get or call an attribute of a wrapper, the URL is appended by that attribute's name or method parameter. Let's say we have the following code:

id, count = 71, 20
api = tortilla.wrap('')

Every attribute and method call represents a part of the URL:

api         ->
.video      -> /video
(id)        -> /71
.comments   -> /comments
.get(count) -> /20
Final URL   ->

The last part of the chain (.get()) executes the request. It also (optionally) appends one last part to the URL. Which allows you to do stuff like this:
# instead of this

So to summarize, getting attributes is used to define static parts of a URL and calling them is used to define dynamic parts of a URL.

Once you've chained everything together, Tortilla will execute the request and parse the response for you.

At the moment, Tortilla only accepts JSON-formatted responses. Supporting more formats is on the roadmap for future Tortilla versions.

The parsed response will be bunchified which makes dictionary keys accessible through attributes. So, say we get the following JSON response for the user 'john':

{"name": "John Doe"}

If we request this with an already created wrapper, we can access the response data through attributes:

>>> user = api.users.get('john')
'John Doe'


A common requirement for accessing APIs is providing authentication data. This usually has to be described in the headers of each request. Tortilla makes it very easy for you to describe those recurring headers:

api.config.headers.token = 'secret authentication token'

You can also define custom headers per request:

api.endpoint.get(headers={'this': 'that'})

These headers will be appended to the existing headers of the wrapper.


URL parameters can be defined per request in the params option:{'q': 'search query'})


Some APIs have a limit on the amount of requests you can make. In these cases, caching can be very helpful. You can activate this with the cache_lifetime parameter:

api = tortilla.wrap('', cache_lifetime=100)

All the requests made on this wrapper will now be cached for 100 seconds. If you want to ignore the cache in a specific situation, you can use the ignore_cache parameter:


The response will now be reloaded.

URL Extensions

APIs like Twitter's require an extension in the URL that specifies the response format. This can be defined in the extension parameter:

api = tortilla.wrap('', extension='json')

This option can be overridden with every request or subwrap:

api.special.endpoint.extension = 'xml'

URL Suffix

Some APIs uses a trailling slash at the end of URLs like in example below:

You can add the trailling slash with suffix="/" argument when wrapping the API or getting the URL with .url(suffix="/") method:

api = tortilla.wrap('', suffix="/")

Will return the following URL:

api         ->
.video      -> /video
(id)        -> /71/
Final URL   ->


Activating debug mode can be done with the debug parameter:

api.debug = True
# OR
api = tortilla.wrap('', debug=True)

You can override the debug parameter per request:


An example using the GitHub API:

>>> user = github.users.get('octocat')
Executing GET request:
    headers: {}
    query:   None
    data:    None

Got 200 OK:
    {u'public_repos': 5, u'site_admin': ...

Enjoy your data.

You can’t perform that action at this time.