Wikidata API
Query the structured knowledge base behind Wikipedia via SPARQL/REST.
What it does
Wikidata is the structured-data sister of Wikipedia: millions of items (people, places, works, species, concepts) identified by Q-numbers, each with statements built from properties (P-numbers). You can fetch an item’s data directly or run SPARQL queries across the whole graph.
Base URL
Authentication
None, but send a real User-Agent. Two main routes: the Action API action=wbgetentities&ids=Q42 for one or more items, and the SPARQL endpoint https://query.wikidata.org/sparql?query=…&format=json for graph queries.
Code examples
curl -s "https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q42&format=json&props=labels|claims" \ -H "User-Agent: larzos-demo/1.0 (contact@example.com)"
import requests
q = """SELECT ?city ?cityLabel ?pop WHERE {
?city wdt:P31 wd:Q515; wdt:P1082 ?pop; wdt:P17 wd:Q30 .
} ORDER BY DESC(?pop) LIMIT 5"""
r = requests.get("https://query.wikidata.org/sparql",
params={"query": q, "format": "json"},
headers={"User-Agent": "larzos-demo/1.0 (contact@example.com)"}, timeout=30)
for row in r.json()["results"]["bindings"]:
print(row["cityLabel"]["value"], row["pop"]["value"])Rate limits & quotas
SPARQL queries have a 60-second timeout and the public endpoint is shared — heavy or frequent querying (roughly beyond 30–60/minute) gets throttled (HTTP 429). For big workloads, use the Wikidata Query Service dumps or the wbgetentities API in batches of up to 50 ids.
Key response fields
| entities.Q42.labels.en.value | The item’s English label |
| entities.Q42.descriptions / aliases | Short description and alternate names |
| entities.Q42.claims.P569 | Statements for a property (here, date of birth) |
| (SPARQL) results.bindings[] | Rows; each variable has a value (and type) |
| sitelinks | Links to the corresponding Wikipedia articles per language |
Common errors
| (SPARQL) 500 QueryTimeoutException | Query too expensive — add LIMIT, narrow with more specific triples, or use a dump. |
| (SPARQL) 400 MalformedQueryException | SPARQL syntax error — validate in the query.wikidata.org web UI. |
| 429 | Rate limited — slow down; add a contact User-Agent so you aren’t blocked outright. |
| (no entity) | Unknown Q-id returns an entity with a missing flag. |
When to use it (and alternatives)
Use it when you need facts as data — a person’s birth date and place, a country’s population and capital, a book’s author and publication year — or cross-domain queries. For readable prose, use Wikipedia; the two link via sitelinks.
FAQ
What are Q-numbers and P-numbers?
Every item has a Q-id (Q42 = Douglas Adams) and every property has a P-id (P569 = date of birth). Statements are item → property → value triples. Labels are multilingual; the ids are the stable keys.
How do I turn a name into a Q-id?
Use action=wbsearchentities&search=Douglas Adams&language=en on the Action API, then use the top result’s id.
My SPARQL query keeps timing out.
Add a LIMIT, filter early with the most selective triple, avoid unbounded OPTIONAL/regex, and prototype in the web query editor which shows the execution plan.
New to APIs?
Short guides for working with this API — and any other.