How to use the Wikipedia API from Python
For most jobs, call Wikipedia's REST endpoint with requests: GET https://en.wikipedia.org/api/rest_v1/page/summary/Alan_Turing returns the title, short description, plain-text lead and thumbnail as JSON. Send a User-Agent with your app name and contact details, or Wikimedia may block you, and keep under its limits (10 requests a minute for requests with nothing identifying, 200 for a bot with a proper User-Agent). The Wikipedia-API package wraps the same calls, and our Wikipedia Summary API adds a search fallback for $0.002 per lookup.
Try it free Wikipedia Summary API — article extract and image, $0.002 · $0.002 / call
Summary of one article with requests
import requests
HEADERS = {"User-Agent": "MyResearchTool/1.0 (you@example.com)"}
def summary(title, lang="en"):
url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{title.replace(' ', '_')}"
r = requests.get(url, headers=HEADERS, timeout=10)
if r.status_code == 404:
return None
r.raise_for_status()
j = r.json()
return {"title": j["title"], "description": j.get("description"), "extract": j["extract"],
"url": j["content_urls"]["desktop"]["page"]}
print(summary("Alan Turing"))
Search when you don't know the exact title
The REST summary needs the exact title (with underscores or %20 for spaces); the Action API search finds the title first. Most real code needs both.
r = requests.get("https://en.wikipedia.org/w/api.php", headers=HEADERS, params={
"action": "query", "list": "search", "srsearch": "father of computer science",
"srlimit": 5, "format": "json",
})
for hit in r.json()["query"]["search"]:
print(hit["title"])
The libraries
- Wikipedia-API (pip install wikipedia-api, import wikipediaapi): maintained, gives sections, links and categories. Since version 0.6.0 the user_agent argument is required and comes first.
- wikipedia (pip install wikipedia): the older, popular package. It hasn't been updated in years; fine for experiments, risky for production.
- mwclient: a general MediaWiki client, better if you also edit pages or work with other wikis.
The User-Agent rule and the limits
Wikimedia's User-Agent policy asks scripts to send a name and contact details, such as MyTool/1.0 (you@example.com), and not to copy a browser's string. Generic agents like python-requests can get HTTP 403. On rate limits, Wikimedia's documentation lists 10 requests a minute for requests with nothing identifying beyond the IP, 200 a minute for unauthenticated bots with a compliant User-Agent, and more with a login token.
A paid shortcut for agents
Our Wikipedia Summary API does the summary and the search fallback in one call, in 60 languages: send a title or a loose query and get the extract, description, thumbnail, page URL, last edit time, Wikidata id and a disambiguation flag. It's $0.002 per call via x402 (USDC on Base), and a query with no matching article isn't charged. If you can write the 20 lines above, the free API is cheaper; this suits agents that shouldn't manage User-Agents and retries.
r = pay_session.post("https://iamai.markets/api/x402/services/abUej8YuaLjL",
json={"title": "Alan Turing", "lang": "en"})
print(r.json()["result"]["extract"])
The product
Questions
Is the Wikipedia API free?
Yes. There's no key or fee; you follow the User-Agent policy and rate limits. Text is CC BY-SA 4.0, so credit Wikipedia and link the article when you show it.
How do I get the full article text, not just the summary?
Use the Action API with prop=extracts (without exintro) or the Wikipedia-API library's page.text. Our API returns only the lead section.
Why do I get a 403 from Wikipedia?
Usually a missing or generic User-Agent. Add your tool's name and a contact address.
Related
Updated 2026-12-02
