The other day Google announced a new API console to simplify working with their various APIs. These APIs are basically REST-based calls into the Google servers where you connect through a URL, passing different parameters in the URL itself, and get back a response. The response comes back as either XML, or as JSON, which is ideal for a client-side JavaScript program. (Although most server-side languages such as PHP and C#/ASP.NET provide parsers for JSON as well, so you can process the results on server-side if you prefer.)
As has been the case since the early days of the Google API, you must include an API key. That way Google knows who is using the APIs and can put daily limits so you don't slam their servers.
Much of this is nothing new, but what is new is the addition of the API console as well as two new APIs, a Custom Search API (which replaces the previous Web Search API), and a new Translate API, which is a brand new replacement for the older translation API.
I took the console and these two of these new APIs for a quick spin so I could report them back to you.
The console is incredibly easy to use. It's just a web page that you log into with your Google account, and offers several links for activating the different APIs (and not just the new APIs, but several others as well, including Buzz API, Moderator API, and others).
Initially you're asked to create a project, which is really just a name that you can group your API usage together into, as shown here:
Next, the console screen opens:
In this case, I created a project called DaniWeb Project. I then activated the Custom Search API. I was asked to accept the terms and conditions. After doing so, I was then initially given a limit of 100 queries per day:
Right there on the screen was a sample URL, which can be used as a REST call. But since it's a URL, you can just paste it right into the browser, which I did. Here's the first part of the JSON response (I didn't put the whole thing here as it's pretty big, so this isn't a complete JSON object):
handleResponse({
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "http://www.googleapis.com/customsearch/v1?q\u003d{searchTerms}&num\u003d{count?}&start\u003d{startIndex?}
&hr\u003d{language?}&safe\u003d{safe?}&cx\u003d{cx?}&cref\u003d{cref?}
&sort\u003d{sort?}&alt\u003djson"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - lectures",
"totalResults": 9230000,
"searchTerms": "lectures",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"cx": "017576662512468239146:omuauf_lfve"
}
],
Not bad. There's some more data packed into it, and then the actual results of the search in a JSON array called items. Here's the first member of that array:
"items": [
{
"kind": "customsearch#result",
"title": "EE364a: Lecture Videos",
"htmlTitle": "EE364a: \u003cb\u003eLecture\u003c/b\u003e Videos",
"link": "http://www.stanford.edu/class/ee364a/videos.html",
"displayLink": "www.stanford.edu",
"snippet": "Apr 7, 2010 ... Course materials. Lecture slides •
Lecture videos (2008) • Review sessions. Assignments. Homework •
Reading. Exams. Final exam ...",
"htmlSnippet": "Apr 7, 2010 \u003cb\u003e...\u003c/b\u003e Course materials.
\u003cb\u003eLecture\u003c/b\u003e slides • \u003cb\u003eLecture\u003c/b\u003e
videos (2008) • Review sessions. \u003cbr\u003e Assignments. Homework • Reading.
Exams. Final exam \u003cb\u003e...\u003c/b\u003e"
},
(So I imagine somebody at Stanford University has been getting a lot of clicks to his web site since it appears in the sample.)
As a web develop and trainer over at Cogsmedia.com, I've done a lot of JavaScript and jQuery programming, and have interacted with a lot of students in my jQuery courses, and I can say this is perfect--both for my professional work and for students. JSON is so easy to work with in JavaScript, as you just call the parser and it all becomes the very data structures that you see. That makes processing this a snap, at least if you're writing JavaScript code. (It's a bit more trouble in other languages, but there are libraries available that essentially create a similar data structure in the language you're using.) Either way, you don't have to traverse down into an XML structure, which in my opinion makes it a bit easier.
That was all it took to activate the API. Normally when I write product reviews, I have to give a great deal of detail about the pros and cons, but in this case, there's not a lot to it. It's easy to use and just there.
The Translate API was equally easy. When you activate it, Google gives you a sample URL to try out. Here's the one I tried out. (You'll have to fill in your own key if you want to try it out, which you'll get when you sign up with the console.) https://www.googleapis.com/language/translate/v2?
key=________________&q=flowers&source=en&target=fr
&callback=handleResponse&prettyprint=true
The text to translate is the single word "flowers", and the languages are English (denoted en) to French (denote fr). The "prettyprint=true" option means the resulting JSON will be formatted with indentations and carriage returns. (In general you won't need that on when processing results programmatically. But for debugging purposes, that makes it easy for you to visually see what you're getting.)
Here's the results of this particular call:
handleResponse({
"data": {
"translations": [
{
"translatedText": "fleurs"
}
]
}
}
);
Now notice a couple things in this and the previous JSON results: They both have a function call, handleResponse. That name came right from the URL. You can provide any name you want, and the idea is that when you call the JSON parser, it will in turn call that function—a function that you provide. That way you can immediately send these results right into a function that you create, in this case one called handleResponse. If you don't want that, just leave it off. I left off that parameter and here's the result:
{
"data": {
"translations": [
{
"translatedText": "fleurs"
}
]
}
}
It's just a straight JSON block of data. And here's the results with the prettyprint left off:
{"data":{"translations":[{"translatedText":"fleurs"}]}}
Back to the console, if you want to create more projects, you can do so simply through an Add Project button. You can also rename any existing project. You can create a team for collaborative work where you can add other people who have Google accounts. There's also a section for traffic controls, and this is where you can request an increase from Google. (The initial limit is 100 queries per day for a search, and 100,000 characters per day for a translation.) You also get traffic reports. This section isn't very sophisticated (compared, for example, to the AdWords reports). And finally, you can manage your keys right from within the console. You can generate a new key, for example, if you find your existing key is being used by an unauthorized user and you need to scrap it.
All in all, this whole thing is really easy to use. There's not much of a learning curve, so if you want to give a try, by all means do so. You'll need to create a Google account if you don't already have one (which is free). Then you log into the console here: https://code.google.com/apis/console/
And from there you'll be up and running in no time. Have fun!