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.
npm install sparql-http-clientVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize the default `SparqlClient`, execute a `SELECT` query against a public SPARQL endpoint (Wikidata), and process the streaming results asynchronously. It logs each binding from the result rows.
Migrate your project to use ES modules (`import`/`export`) or use dynamic `import()` for CommonJS contexts, if absolutely necessary, though direct `require` is not supported.
Ensure the client is initialized with at least one URL, for example: `new SparqlClient({ endpointUrl: 'http://example.org/sparql' })`.Always check the return type for the specific query operation. For streams, ensure proper stream consumption. For Promises, use `await` or `.then()` to handle the result.
Change 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`).Provide the SPARQL endpoint URL when initializing the client: `const client = new SparqlClient({ endpointUrl: 'https://query.wikidata.org/sparql' })`.Ensure 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.