Wikipedia API examples. For advanced use see Official Mediawiki API documentation
Quick intro:
- All client requests should contain
&origin=*
because of CORS. - Search term should be capitalize (
Tori Amos
nottori amos
) if multiple words. - You could request any wiki language you like
To GET full article for the requested title (titles=belgrade
), with images and URL (inprop=url
). Also, follows redirection (redirects
) if necessary:
Previous request with minimal params:
https://en.wikipedia.org/w/api.php?action=query&titles=belgrade&prop=extracts&format=json
To GET first paragraph of an article:
https://en.wikipedia.org/w/api.php?action=query&titles=Belgrade&prop=extracts&format=json&exintro=1
To GET first 10 search results with extract and thumbnail image (prop=extracts|pageimages
). Results is HTML by default, but we want json
format:
To GET first 20 search results (srlimit
) with short info (prop=info
):
To GET first 20 search results (gsrlimit
) with extract and thumbnail image (prop=extracts|pageimages
). This time, article extract is set to plain text (explaintext
):
Advanced search params:
gsrsearch=intitle:belgrade
(word "belgrade" is in title)gsrsearch=prefix:belgrade
(article's title starts with the word "belgrade")
If you have problems, append &origin=*
at the end of the route.
To get source of the main image of the article:
To get all images from the article:
https://en.wikipedia.org/w/api.php?action=query&titles=belgrade&prop=images&format=json
To GET first 20 image files (gsrnamespace=6
) from Wikimedia Commons with the term "Belgrade" in the filename (gsrsearch=intitle:Belgrade
). Requested thumbnail size is 250px (pithumbsize=250
):
Previous request without some params:
Remember, if you have problems, append &origin=*
at the end of the route.
To GET wiki quotes for requested term (titles=Zen
). The API is the same, just the domain is different (wikiquote.org
):