SPARQL HTTP Client
sparql-http-client is a JavaScript client library designed to simplify interactions with SPARQL endpoints, covering SPARQL Queries, Updates, and Graph Store operations. It abstracts the complexities of the SPARQL Protocol and SPARQL Graph Store Protocol, offering a streamlined API. The current stable version is 3.1.0, and the project appears to be actively maintained, indicated by recent version updates and a healthy GitHub repository. A key differentiator is its provision of multiple client 'flavors': the default `SparqlClient` (also known as `StreamClient`) which leverages Node.js streams for processing results from `SELECT`, `CONSTRUCT`, and `DESCRIBE` queries; a `SimpleClient` that aligns more closely with the `fetch` API for direct response handling; and a `ParsingClient` that automatically wraps results into RDF/JS DatasetCore objects or arrays. This flexibility allows developers to choose the most suitable interface for their specific use case, whether it's processing large result sets via streams or quickly retrieving parsed RDF data.
Common errors
-
ReferenceError: require is not defined
cause `sparql-http-client` version 3.x and above is an ES module and does not support CommonJS `require` syntax directly.fixChange your import statements from `const SparqlClient = require('sparql-http-client')` to `import SparqlClient from 'sparql-http-client'` and ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`). -
TypeError: Missing 'endpointUrl' option
cause The `SparqlClient` (or other client variants) constructor was called without providing the necessary `endpointUrl` (or `updateUrl`/`storeUrl`) in the configuration object.fixProvide the SPARQL endpoint URL when initializing the client: `const client = new SparqlClient({ endpointUrl: 'https://query.wikidata.org/sparql' })`. -
No data received from SPARQL SELECT query stream
cause When using `client.query.select()` or `client.query.construct()`, the method returns a Node.js readable stream. If the stream is not actively consumed (e.g., listening for 'data' events or using async iterators), no data will be processed.fixEnsure you are consuming the stream by attaching 'data' event listeners and an 'end' listener, or by using `for await (const row of stream)` if your environment supports async iterators.
Warnings
- breaking Version 3.x of `sparql-http-client` transitioned to being an ESM-only package. Direct `require()` statements for importing the library will no longer work, leading to runtime errors.
- gotcha All client constructors (`SparqlClient`, `SimpleClient`, `ParsingClient`) require at least one URL argument (`endpointUrl`, `updateUrl`, or `storeUrl`) in their options object. Omitting all of them will result in an error or disable specific capabilities.
- gotcha Different query types return different result formats and mechanisms. `client.query.select()` and `client.query.construct()` return Node.js readable streams, which must be consumed (e.g., using `stream.on('data')` or `for await...of`). `client.query.ask()` and `client.query.update()` return Promises that resolve to a boolean or void, respectively.
Install
-
npm install sparql-http-client -
yarn add sparql-http-client -
pnpm add sparql-http-client
Imports
- SparqlClient
const SparqlClient = require('sparql-http-client')import SparqlClient from 'sparql-http-client'
- SimpleClient
import SimpleClient from 'sparql-http-client'
import { SimpleClient } from 'sparql-http-client' - ParsingClient
import { ParsingClient } from 'sparql-http-client'
Quickstart
import SparqlClient from 'sparql-http-client'
const endpointUrl = 'https://query.wikidata.org/sparql'
const query = `
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?value WHERE {
wd:Q243 p:P2048 ?height.
?height pq:P518 wd:Q24192182;
ps:P2048 ?value .
}`
async function runQuery() {
const client = new SparqlClient({ endpointUrl })
const stream = await client.query.select(query)
return new Promise((resolve, reject) => {
stream.on('data', row => {
for (const [key, value] of Object.entries(row)) {
console.log(`${key}: ${value.value} (${value.termType})`)
}
})
stream.on('end', () => {
console.log('Query finished.')
resolve()
})
stream.on('error', err => {
console.error('Stream error:', err)
reject(err)
})
})
}
runQuery().catch(console.error)