How to query Wikidata using SPARQL in JavaScript
The Wikidata Query Service allows you to get data from Wikidata using SPARQL. There are many interesting SPARQL queries you can run, such as finding all the paintings by a specific artist or all the cities in a specific country, and you can use this data to create visualizations, build applications, and so on.
The service is available at https://query.wikidata.org/sparql and supports CORS, so you can send your queries directly on the client. Here's an example of how you can run a query in JS:
const response = await fetch("https://query.wikidata.org/sparql", {
method: "POST", // It's better to use POST because the query can be long
headers: {
Accept: "application/sparql-results+json", // This makes sure you get JSON results
"Content-Type": "application/x-www-form-urlencoded",
},
body: `query=${encodeURIComponent(`
SELECT ?item ?itemLabel
WHERE {
?item wdt:P31 wd:Q3305213.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10
`)}`,
});
const json = await response.json();
console.log(json);Recommended for you
Product
Ea.
coffee IPA Transcription Poster
теплопроводность Morphemic Analysis Poster
Adenine Molecule Poster, Ball-and-Stick Model, English-Labeled
Asturian Alphabet Chart, 8.3 x 11.7 in (21.0 x 29.7 cm, Desk Chart, Laminated), English-Labeled
2026 Wall Calendar, English-Labeled, Sunday-Start Layout, Poster / Wall Print, 23.4 x 33.1 in (59.4 x 84.1 cm)