Using the Wikipedia API - Live Demo with Source Code
Wikipedia recently introduced a new auto-complete search feature that offers keyword suggestions in real-time as you type the characters in the Wikipedia search box.
For instance, if you type “GOO”, the drop down will show titles of all Wikipedia entries that begin with the word “GOO” (see screenshot).
This Google Suggest like feature is actually built on an undocumented but public Wikipedia API that can easily be used to retrieve useful information from the Wikipedia database. To demonstrate the use of Wikipedia API, here is a simple Wikipedia spell checker app:
<iframe marginwidth="0" marginheight="0" src="http://img.labnol.org/di/images/wikipedia-json.html" frameborder="0" width="400" scrolling="no" height="40"></iframe>
The above spell checker fires an Wikipedia API query to see if Wikipedia has an entry about the word you just entered in the search box and says “Correct” if it finds one.
For the geeks, here’s the actual source code:
var url = 'http://en.wikipedia.org/w/api.php?action=opensearch&search=**value**&format=json&callback=**spellcheck**';
var elem = document.createElement ('script');
elem.setAttribute ('src', url);
elem.setAttribute ('type','text/javascript');
document.getElementsByTagName ('head') [0].appendChild (elem);
A related example - if you want to know about Wikipedia articles that begin with the term “India”, just the following URL in your web browser:
http://en.wikipedia.org/w/api.php?action=opensearch&search=india
The above Wikipedia URL would return the following set of words:
[“india”,[“India”,“INDIA”,“India (album)”,“India (cat)”,“India (disambiguation)”,“India (name)”,“India (porn star)”,“India (region)”,“India (singer)”,“India (subcontinent)”]]
So what else could one do with the Wikipedia API ? Here are some more uses:
1. A Wordpress plugin which auto links terms to Wikipedia articles.
2. A “better” Wikipedia search which hooks up Google ”Did you mean” with the Wikipedia API, so that misspellings also direct to the correct article.
3. A Firefox extension which allows single click querying of terms.
The Wikipedia API was dissected by Pratham who maintains a very interesting tumbleblog at pratham.name and tweets here. Note that this not an official API (Wikipedia is not talking about this yet), therefore use on a production system is not advised.
Amit Agarwal
Google Developer Expert, Google Cloud Champion
Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory