API Directory › 📚 Data, Text & Words › Wikidata
No authHTTPSCORS

Wikidata API

Query the structured knowledge base behind Wikipedia via SPARQL/REST.

Auth
No auth needed
HTTPS
Yes
CORS
Yes
Open Wikidata docs ↗

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

https://www.wikidata.org/w/api.php

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 (get an item)
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)"
Python (SPARQL)
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.valueThe item’s English label
entities.Q42.descriptions / aliasesShort description and alternate names
entities.Q42.claims.P569Statements for a property (here, date of birth)
(SPARQL) results.bindings[]Rows; each variable has a value (and type)
sitelinksLinks to the corresponding Wikipedia articles per language

Common errors

(SPARQL) 500 QueryTimeoutExceptionQuery too expensive — add LIMIT, narrow with more specific triples, or use a dump.
(SPARQL) 400 MalformedQueryExceptionSPARQL syntax error — validate in the query.wikidata.org web UI.
429Rate 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.

🛠️ Handy LarzOS tools

Free tools to help you work with this API.

Word CounterJSON Formatter

Related Data, Text & Words APIs

← Back

More Data, Text & Words APIs · All 196 APIs