< Back

Querying Google Books

2014-03-13

Introduction

Google Books provides a free API (Application Programming Interface) for querying its book database. This article explains how to search this database by ISBN and obtain information about the book.

Google Books Request

Details about the Google Books API can be found on the Google Developers website. Throughout the Google documentation, you will find mentions of needing an ‘API key’. Such a key is not required for basic searches, so we do not need to worry about that here.

The method that we are interested in is a book search by ISBN. The details of this method are explained on this page, but can be summarized as an HTTP request formatted as:

GET https://www.googleapis.com/books/v1/volumes?q={search terms}

In order to search by ISBN, the search terms should be formatted as:

isbn:{isbn number}

So, an example of a complete request becomes:

https://www.googleapis.com/books/v1/volumes?q=isbn:9780689856662

Google Books Response

The result returned by Google Books is in JSON format. A complete response can be viewed by following this link, but a truncated response is shown below (some fields >have been omitted):

{
 "kind": "books#volumes",
 "totalItems": 1,
 "items": [
  {
   "kind": "books#volume",
   "id": "XHtaAAAAYAAJ",
   "etag": "+l9F2hyJAoA",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/XHtaAAAAYAAJ",
   "volumeInfo": {
    "title": "The City of Gold and Lead",
    "authors": [
     "John Christopher"
    ],
    "publishedDate": "2003-04-01",
    "description": "Three boys set out on a secret mission...",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "0689856660"
     },
     {
      "type": "ISBN_13",
      "identifier": "9780689856662"
     }
    ],
    "pageCount": 224,
    "printType": "BOOK",
    "categories": [
     "Juvenile Fiction"
    ],
   }
  }
 ]
}

A Perl Interface

Any programming language with the ability to perform an HTTP request and decode JSON data can be used to interface with Google Books. The example below demonstrates how Perl can be used to fetch information about a book given an ISBN.

# These libraries are required for HTTP and JSON
use HTTP::Request;
use LWP::UserAgent;
use JSON::PP;

# Form the URL for the HTTP request
my $url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:' . $isbn;

# Perform the HTTP request
my $request = HTTP::Request->new(GET => $url);
my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);

my $result;
# Only process the result if the HTTP request was successful
if ($response->is_success) {
	# Use JSON::PP to decode the JSON formatted data
	my $json = JSON::PP->new->utf8;
	$json->relaxed();
	my $data = $json->decode($response->content);

	# Make sure that a book was found
	if ($data->{totalItems} >= 1) {
		# Store the book information to $result
		$result = $data->{items}->[0]->{volumeInfo};
	}
}