[SOLVED] Help with the Wikipedia API Sandbox

I’m having trouble with the experimentation part of the Wikipedia Api. I sort of need some more assistance in this endeavor. How do I get the title, content, and main article image? Thanks in advance.

The image is a bit advanced, I never tried that myself.
the wikipedia api sandbox is pretty confusing, (https://en.wikipedia.org/wiki/Special:ApiSandbox) but basically from that page, for your purposes, you want to do the following.

in Main in the dropdown menu for action you want to scroll to and select opensearch
below action you will find format you want this set to json

next in the lefthand column below main, go to action=opensearch
here you can type in an example search term under search, for example "REPLACE ME"
next, if you want you can go to limit in the same section, tick the box and set the max number of search results to return.

finally click the Make Request button up top, copy the field that says Request URL
your search url for your ajax will be “https://en.wikipedia.org” + the copied request url,
Don’t forget to replace “REPLACE+ME” with your actual search term.

There are a lot of other things the api can apparently do, but I don’t understand much beyond these terms.

I never implemented images on my project, but hypothetically I could help you with the pieces to figure it out.
I know that “https://en.wikipedia.org/w/api.php?action=query&format=json&prop=pageimages&titles=+Wiki page’s Title here +"&piprop=original" will give you the main image for Some pages with images, but not all pages with images work.
You have to give it the title of the wikipedia page not a search term.
You would have to do this as an additional ajax request in a loop through each title after getting the titles from the 1st ajax request.

Hope that helps,
Good Luck!

1 Like

Maybe the first reply is enough for you to help your on your way, but I did it differently and maybe my way and explanation can help you as well.

Firstly, this page has a lot of information (maybe too much) https://www.mediawiki.org/wiki/API:Main_page . Reading it will help deepen your knowledge, but it boils down to this:

If you call the JSON, you get data returned, in an array. It is sorted as follows:
Heading: console.log(data[1][0]);
Description: console.log(data[2][0]);
Link: console.log(data[3][0])

You can also check this by calling the JSON in your browser (i.e. https://en.wikipedia.org/w/api.php?action=opensearch&search=god&format=json&callback=? )

Then with some more JS, loops, appending etc. this should be enough to help you on your way.

Feel free to ask any more questions.


1 Like

Thank you so much for responding! I don’t need it to have the image, it would be a nice add-on, but thanks for proviing a suggestion.