How to get facts from Wikidata with Python

Use two Action API calls with requests: wbsearchentities finds the item id for a name (Tesla Inc is Q478214), and wbgetentities returns its labels and claims. Claims hold property ids and item ids, such as P169 for CEO, so you need a second lookup to turn ids into names; SPARQL on query.wikidata.org can do it in one query but has a hard 60-second limit. Our Wikidata API does the search, picks about 30 common facts and returns readable values for $0.003 per item.

Try it free Wikidata API — facts about companies, people, places, $0.003 · $0.003 / call

Search, then fetch the item

Those ids still need wbgetentities with props=labels to become names. Claims also have ranks: prefer preferred-rank values and skip deprecated ones, or you'll show a former CEO.

import requests

API = "https://www.wikidata.org/w/api.php"
H = {"User-Agent": "MyFactsTool/1.0 (you@example.com)"}

hit = requests.get(API, headers=H, params={
    "action": "wbsearchentities", "search": "Tesla Inc", "language": "en",
    "type": "item", "limit": 1, "format": "json"}).json()["search"][0]
qid = hit["id"]                                   # 'Q478214'

ent = requests.get(API, headers=H, params={
    "action": "wbgetentities", "ids": qid, "props": "labels|claims",
    "languages": "en", "format": "json"}).json()["entities"][qid]
ceo = ent["claims"].get("P169", [])
print(qid, [c["mainsnak"]["datavalue"]["value"]["id"] for c in ceo])   # item ids, not names

The same in SPARQL

The Query Service user manual sets a hard 60-second deadline per query and allows each client (User-Agent plus IP) 60 seconds of processing time per minute; over that you get HTTP 429 with Retry-After. SPARQL is the right tool for lists ("all companies founded in 1903"), and overkill for one item.

q = """
SELECT ?ceoLabel ?founded WHERE {
  wd:Q478214 wdt:P169 ?ceo .
  OPTIONAL { wd:Q478214 wdt:P571 ?founded . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}"""
r = requests.get("https://query.wikidata.org/sparql", headers=H,
                 params={"query": q, "format": "json"}, timeout=70)
for row in r.json()["results"]["bindings"]:
    print(row["ceoLabel"]["value"], row.get("founded", {}).get("value"))

Libraries

Readable facts in one call

Our Wikidata API takes a name or a Q-id and returns label, description, aliases and facts with names instead of ids: instance_of, country, founded, founded_by, ceo, headquarters, industry, employees, population, coordinates, official_website and about 20 more. It prefers preferred-rank values, picks the latest dated figure for quantities such as population, and lists up to 4 other matches so you can spot the wrong Mercury. Labels work in 60 languages. $0.003 per call via x402; no match means no charge.

r = pay_session.post("https://iamai.markets/api/x402/services/oH-Un5PQ4QZb",
                     json={"query": "Tesla Inc"})
res = r.json()["result"]
print(res["id"], res["label"], res["facts"].get("ceo"), res["facts"].get("founded"))

The product

Questions

Is the Wikidata API free?

Yes, with no key. Wikidata's data is CC0, so you don't even need to credit it, though it's polite.

Why are some Wikidata figures out of date?

It's community-edited. Employees, revenue and population are whatever an editor last added, which can be a few years old.

Should I use SPARQL or the API?

The API for one item by id or name; SPARQL for questions across many items.

Related

Updated 2026-12-02