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);

See also

Pythagorean Theorem Poster, English-Labeled
$19.99

A poster illustrating the Pythagorean theorem.

Helicene Molecule Poster, Ball-and-Stick Model, English-Labeled
$19.99

A poster featuring the ball-and-stick model of the helicene molecule.

In development
Dictionary of Arabic Roots

A comprehensive guide to Arabic roots and their meanings.

synecdoche IPA Transcription Poster
$14.99

A poster featuring the phonetic transcription of the word synecdoche in the International Phonetic Alphabet (IPA).

Ligatures Poster, English-Labeled
$19.99

A poster showcasing stylistic Latin-script ligatures.

Wikidata's ontology

Inspecting the ontology of Wikidata.

Let's explore the Nobel Prize dataset

An overview of the official Nobel Prize Linked Data dataset with some example SPARQL queries.

Why federation is a game-changing feature of SPARQL

SPARQL federation is an incredibly useful feature for querying distributed RDF graphs.

SPARQL Parse Tree Viewer

Visualize the parse tree of SPARQL queries.

SPARQL Client

Query SPARQL endpoints online.

All prices listed are in United States Dollars (USD). Visual representations of products are intended for illustrative purposes. Actual products may exhibit variations in color, texture, or other characteristics inherent to the manufacturing process. The products' design and underlying technology are protected by applicable intellectual property laws. Unauthorized reproduction or distribution is prohibited.