[Edit][History] last modified may 8

Open Library API

Read API

Infogami provides an API to query the database for objects matching particular criteria or to get an object from the database. All the requests take input either as part of the URL or query string. You can try sample requests and practice building requests in the API sandbox.

arrowQuerying for Objects (/things)
arrowGetting an Object (/get)
arrowQueries for Versions (/versions)

Because in infogami everything is an object, you can query for any object in infogami space, such as templates, editions, authors, or individual infogami fields in any page type. Each object has an object type. (List of types.) For example, there is /type/edition which represents all of the information about a book, and type/edition/isbn_10 which is the property for the 10-character ISBN in the edition record.

Querying for Objects (/things)

To find objects matching a particular query, send a GET request to http://openlibrary.org/api/things with query as parameter. In this documentation we use curl as a simple command line query client; any software that supports http GET can be used.

The query must use JSON dictionary syntax.

This query searches for all editions with a particular ISBN value, and returns the keys of all matching records. Since there can be only one book with the any ISBN, the result contains only one key.

A curl request for the above, then, would read as follows:

$ curl -G --data-urlencode 'query={"type":"\/type\/edition", "isbn_10":"0789312239"}' \
http://openlibrary.org/api/things

Pattern matching

The API supports querying for objects based of string matching.

Here is an example to get all object with key starting from /about/.

Due to performance reasons, * is allowed only at the end of a string.

The Sort Directive

The sort directive can be used to tell Infogami to sort the results of a query before returning.

The following query gives the list of all documents sorted in the order of their keys.

By default sort operation returns the results in the ascending order. To reverse that order, prepend minus (-) to the value of sort directive.

Limiting Queries

By default every results of every query are limited to 100 values. Alternate limit can be specified by using the limit directive.

Here is an example to get first 10 templates.

The offset directive can be used to ask infogami to give results from a specific offset.

The following query gives the next 10 templates.

Expression AND in queries

All the keys in the query other than special directives (sort, limit, offset) are implicitly ANDed.

For example, the following query finds all objects having type as /type/edition and author as Mark Twain (/a/OL18319A).

Getting an Object (/get)

In Infogami, every object is identified by a unique key. In the example above, we queried the database on an ISBN and were returned the unique key for the edition that matches that ISBN. We can then retrieve the edition with a get command:

$ curl http://openlibrary.org/api/get?key=/b/OL1001932M

This returns:

{ "status": "ok", "result": { "subject_place": [ "Venice (Italy)"], "lc_classifications": [ "DG674.2 .S3 2005" ], "latest_revision": 1, "genres": [ "Juvenile literature."], "title": "This is Venice", "languages": [ { "key": "\/l\/eng" }], "subjects": [ "Venice (Italy) -- Description and travel -- Juvenile literature."], "publish_country": "nyu", "by_statement": "M. Sasek.","type": { "key": "\/type\/edition" }, "revision": 1, publishers": [ "Universe"], "last_modified": "2008-04-01T03:28:50.625462", "key": "\/b\/OL3315616M", "authors": [ { "key": "\/a\/OL1396639A" } ], "publish_places": [ "New York, NY" ], "pagination": "56 p. :", "lccn": [ "2004110229" ], "notes": "\"A children's classic\"--Cover.\nOriginally published: New York : Macmillan, 1961.", "number_of_pages": 56, "isbn_10": [ "0789312239" ], publish_date": "2005" }}

Because this string is hard to read, you may want to add the parameter "prettyprint=true" when you are testing your queries.

$ curl http://openlibrary.org/api/get?key=/b/OL1001932M&prettyprint=true

This returns a more eye-readable version of the same string.

# response
     {
       "status": "ok", 
        "result": {
        "subject_place": 
            "Venice (Italy)", 
        "lc_classifications": 
            "DG674.2 .S3 2005", 
        "latest_revision": 1, 
        "genres": [
            "Juvenile literature."
        ], 
        "title": "This is Venice", 
        "languages": [
            {
                "key": "\/l\/eng"
            }
        ], 
        "subjects": [
            "Venice (Italy) -- Description and travel -- Juvenile literature."
        ], 
        "publish_country": "nyu", 
        "by_statement": "M. Sasek.", 
        "type": {
            "key": "\/type\/edition"
        }, 
        "revision": 1, 
        "publishers": [
            "Universe"
        ], 
        "last_modified": "2008-04-01T03:28:50.625462", 
        "key": "\/b\/OL3315616M", 
        "authors": [
            {
                "key": "\/a\/OL1396639A"
            }
        ], 
        "publish_places": [
            "New York, NY"
        ], 
        "pagination": "56 p. :", 
        "lccn": [
            "2004110229"
        ], 
        "notes": "\"A children's classic\"--Cover.\nOriginally published: New York : Macmillan, 1961.", 
        "number_of_pages": 56, 
        "isbn_10": [
            "0789312239"
        ], 
        "publish_date": "2005"
         }
       }

The following example gets the page /pagelist using the API.

    $ curl http://openlibrary.org/api/get?key=/pagelist?prettyprint=true
    {
        "status": "ok", 
        "result": {
            "body": "{{PageList(\"\/\")}}", 
            "title": "Page List", 
            "last_modified": "2008-04-18T09:19:56.978194", 
            "latest_revision": 1, 
            "key": "\/pagelist", 
            "type": {"key": "\/type\/page"}, 
            "revision": 1
        }
     }

Since the response is in JSON format, the Content-Type is set to application/json, which may not be displayed by the browsers as text. To enable that text=true must be passed in the query string.

Queries for Versions (/versions)

In Infogami, modification of every object creates a new version of that object. To find versions matching a particular query, send a GET request to http://openlibrary.org/api/versions with query as parameter. The query must be a JSON dictionary, just like the things query. The sort and limit directives work similar to the things query.

For example the following query finds the most 10 versions of / object.

# Query
    {
        "key": "/",
        "limit": 3,
        "sort": "-created"
    }

# Response
       {
           "status": "ok", 
           "result": [
               {
                   "comment": "", 
                   "created": "2008-04-18T15:40:04.279578", 
                   "ip": "207.241.226.140", 
                   "author": "/user/webchick", 
                   "thing_id": 9875590, 
                   "key": "/", 
                   "id": 9894268, 
                   "machine_comment": null, 
                   "revision": 14
               }, 
               {
                   "comment": "", 
                   "created": "2008-04-18T15:39:30.588136", 
                   "ip": "207.241.226.140", 
                   "author": "/user/webchick", 
                   "thing_id": 9875590, 
                   "key": "/", 
                   "id": 9894267, 
                   "machine_comment": null, 
                   "revision": 13
               }, 
               {
                   "comment": "", 
                   "created": "2008-04-16T00:18:32.743829", 
                   "ip": "207.241.226.140", 
                   "author": "/user/webchick", 
                   "thing_id": 9875590, 
                   "key": "/", 
                   "id": 9894257, 
                   "machine_comment": null, 
                   "revision": 12
               }
           ]
       }

Performance Issues

To have a bound on the amount of resources consumed by a query, the runtime of each query is limited to 60 sec. Any query that takes longer is aborted.