SPARQL HTTP Client

3.1.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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)

view raw JSON →