# fetch-sparql-endpoint

> A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.

Latest version **7.2.0** (published 2026-09-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install fetch-sparql-endpoint
pnpm add fetch-sparql-endpoint
yarn add fetch-sparql-endpoint
bun add fetch-sparql-endpoint
```

Provides the command `fetch-sparql-endpoint`.

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 7.2.0 |
| Published | 2026-09-01 |
| First published | 2018-08-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 73.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Ruben Taelman |
| Maintainers | rubensworks |
| Keywords | sparql, query, semantic web, rdf, rdfjs, streaming |

## Links

- npm: https://www.npmjs.com/package/fetch-sparql-endpoint
- Repository: https://github.com/rubensworks/fetch-sparql-endpoint.js
- Homepage: https://github.com/rubensworks/fetch-sparql-endpoint.js#readme
- Issues: https://github.com/rubensworks/fetch-sparql-endpoint.js/issues
- Funding: https://github.com/sponsors/rubensworks/
- npm.io page: https://npm.io/package/fetch-sparql-endpoint

## Dependencies (12)

- [n3](https://npm.io/package/n3.md) ^2.0.0
- [yargs](https://npm.io/package/yargs.md) ^17.0.0
- [@types/n3](https://npm.io/package/@types/n3.md) ^1.0.0
- [is-stream](https://npm.io/package/is-stream.md) ^2.0.0
- [rdf-string](https://npm.io/package/rdf-string.md) ^2.0.0
- [sparqlxml-parse](https://npm.io/package/sparqlxml-parse.md) ^3.3.0
- [sparqljson-parse](https://npm.io/package/sparqljson-parse.md) ^3.3.0
- [stream-to-string](https://npm.io/package/stream-to-string.md) ^1.0.0
- [readable-from-web](https://npm.io/package/readable-from-web.md) ^1.0.0
- [@types/readable-stream](https://npm.io/package/@types/readable-stream.md) ^4.0.0
- [@traqula/rules-sparql-1-2](https://npm.io/package/@traqula/rules-sparql-1-2.md) ^1.0.0
- [@traqula/parser-sparql-1-2](https://npm.io/package/@traqula/parser-sparql-1-2.md) ^1.0.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 7.2.0 (latest) — 2026-09-01
- 7.0.0-beta.3 (next) — 2025-10-24
- 7.1.2 — 2026-08-31
- 7.1.1 — 2026-06-18
- 7.1.0 — 2026-01-07
- 7.0.0 — 2026-01-07
- 7.0.0-beta.2 — 2025-10-23
- 7.0.0-beta.1 — 2025-10-21
- 6.2.0 — 2025-08-22
- 6.1.0 — 2025-07-10
- 6.0.0 — 2025-01-08
- 5.1.0 — 2024-07-02
- 5.0.0 — 2024-04-23
- 4.2.1 — 2024-04-23
- 4.2.0 — 2024-04-23
- … 40 more at https://npm.io/package/fetch-sparql-endpoint/versions

## README

# Fetch SPARQL Endpoint

[![Build status](https://github.com/rubensworks/fetch-sparql-endpoint.js/workflows/CI/badge.svg)](https://github.com/rubensworks/fetch-sparql-endpoint.js/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/fetch-sparql-endpoint.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/fetch-sparql-endpoint.js?branch=master)
[![npm version](https://badge.fury.io/js/fetch-sparql-endpoint.svg)](https://www.npmjs.com/package/fetch-sparql-endpoint)

A simple, lightweight module to send queries to [_SPARQL endpoints_](https://www.w3.org/TR/sparql11-protocol/) and retrieve their results in a _streaming_ fashion.

All results are compatible with the [RDFJS specification](http://rdf.js.org/).

All SPARQL queries are supported, such as `SELECT`, `ASK`, `CONSTRUCT` `DESCRIBE`, `INSERT`, `DELETE`, ...

Internally, this library supports SPARQL results in
[SPARQL JSON](https://www.w3.org/TR/sparql11-results-json/),
[SPARQL XML](https://www.w3.org/TR/rdf-sparql-XMLres/),
and [Turtle](https://www.w3.org/TR/turtle/).

## Install

This package can be installed via [npm](https://www.npmjs.com/package/jsonld-context-parser).

```bash
$ npm install fetch-sparql-endpoint
```

This package also works out-of-the-box in browsers via tools such as [webpack](https://webpack.js.org/) and [browserify](http://browserify.org/).

## Usage

### API

#### Create a new fetcher

```js
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';

const myFetcher = new SparqlEndpointFetcher();
```

Optionally, you can pass an options object with the following optional entries:
```js
const myFetcher = new SparqlEndpointFetcher({
  // A custom HTTP method for issuing (non-update) queries, defaults to POST. Update queries are always issued via POST.
  // Use 'QUERY' to use the HTTP QUERY method as defined in RFC 10008 (sends the query in the request body as application/sparql-query).
  method: 'POST',
  // A set of additional parameters that well be added to fetchAsk, fetchBindings & fetchTriples requests
  // With a GET request, these are encoded in the URL.
  // With a POST request when directPost is true (see below), these are also encoded in the URL.
  // With other POST requests, it's encoded in the body.
  additionalUrlParams: new URLSearchParams({ infer: 'true', sameAs: 'false' }),
  // Optional default headers that will be included in each request
  defaultHeaders: new Headers(),
  // A custom fetch-API-supporting function
  fetch,
  // A custom RDFJS data factory
  dataFactory: DataFactory,
  // If variable names in bindings should be prefixed with '?', defaults to false
  prefixVariableQuestionMark: false,
  // Timeout for setting up server connection (Once a connection has been made, and the response is being parsed, the timeout does not apply anymore).
  timeout: 5000,
  // If the url length is below this number, HTTP GET is used regardless of the value of this.method, defaults to 0.
  forceGetIfUrlLengthBelow: 600,
  // If true, POST requests will be sent as application/sparql-query (unencoded query string).
  // If false, POST requests will be sent as application/x-www-form-urlencoded (URL-encoded query string).
  // Defaults to false.
  directPost: false,
  // A SPARQL query parser used by the endpoint fetcher to detect query types.
  sparqlQueryParser: new SparqlParser({ lexerConfig: { positionTracking: 'onlyOffset' }}),
  // Setting this flag to false will make the parser not throw an error if unknown versions are encountered.
  parseUnsupportedVersions: false,
});
```

### Fetch bindings

[SPARQL SELECT](https://www.w3.org/TR/rdf-sparql-query/#select) queries returns a (promise to a) stream of bindings.

```js
const bindingsStream = await fetcher.fetchBindings('https://dbpedia.org/sparql', 'SELECT * WHERE { ?s ?p ?o } LIMIT 100');
bindingsStream.on('data', bindings => console.log(bindings));
```

This will output bindings in the following form,
where keys correspond to variables in the queries,
and values and [RDFJS terms](http://rdf.js.org/#term-interface):
```
{ s: namedNode('s1'), p: namedNode('p1'), o: namedNode('o1') }
{ s: namedNode('s2'), p: namedNode('p2'), o: namedNode('o2') }
{ s: namedNode('s3'), p: namedNode('p3'), o: namedNode('o3') }
...
```

Optionally, you can obtain a list of variables by listening to the `'variables'` event:
```js
const bindingsStream = await fetcher.fetchBindings('https://dbpedia.org/sparql', 'SELECT * WHERE { ?s ?p ?o } LIMIT 100');
bindingsStream.on('data', bindings => console.log(bindings));
// Will print [ variable('s'), variable('p'), variable('o') ]
bindingsStream.on('variables', variables => console.log(variables));
```

### Fetch ask

[SPARQL ASK](https://www.w3.org/TR/rdf-sparql-query/#ask) queries answer with a (promise to a) boolean value.

```js
const answer = await fetcher.fetchAsk('https://dbpedia.org/sparql', 'ASK WHERE { ?s ?p ?o }');
```

This will output `true` or `false`.

### Fetch triples

[SPARQL CONSTRUCT](https://www.w3.org/TR/rdf-sparql-query/#construct) and [SPARQL DESCRIBE](https://www.w3.org/TR/rdf-sparql-query/#describe)
queries returns a (promise to a) stream of triples.

```js
const tripleStream = await fetcher.fetchTriples('https://dbpedia.org/sparql', 'CONSTRUCT { ?s ?p ?o } LIMIT 100');
tripleStream.on('data', triple => console.log(triple));
```

This will output [RDFJS triples](http://rdf.js.org/#triple-interface) as follows:
```
triple(namedNode('s1'), namedNode('p1'), namedNode('o1'))
triple(namedNode('s2'), namedNode('p2'), namedNode('o2'))
triple(namedNode('s3'), namedNode('p3'), namedNode('o3'))
...
```

### Fetch update

[SPARQL Update](https://www.w3.org/TR/sparql11-update/) queries answer with a void promise.

```js
await fetcher.fetchUpdate('https://dbpedia.org/sparql', 'INSERT DATA { <ex:s> <ex:p> <ex:o> }');
```

The `await` will throw an error if the update has failed.

### Detect query type

If you want to know the query type
in order to determine which of the above fetch methods to call,
then you can use the `getQueryType` method as follows:

```js
// Outputs 'SELECT'
fetcher.getQueryType('SELECT * WHERE { ?s ?p ?o } LIMIT 100');
// Outputs 'ASK'
fetcher.getQueryType('ASK WHERE { ?s ?p ?o }');
// Outputs 'CONSTRUCT'
fetcher.getQueryType('CONSTRUCT { ?s ?p ?o } LIMIT 100');
```

This method will also throw an error if the query contains a syntax error.

### Command-line

A command-line tool is provided to quickly query or update any SPARQL endpoint.
With basic authentication, the username and password should be made available
via process-scoped environment variables `SPARQL_USERNAME` and `SPARQL_PASSWORD`.

Usage:
```
Options:
  --endpoint          Send the query to this SPARQL endpoint [string] [required]
  --query             Evaluate the given SPARQL query string            [string]
  --file              Evaluate the SPARQL query in the given file       [string]
  --get               Send query via HTTP GET instead of POST
                                                      [boolean] [default: false]
  --httpQuery         Send query via HTTP QUERY method (RFC 10008) instead of
                      POST                            [boolean] [default: false]
  --timeout           The timeout value in seconds to finish the query  [number]
  --auth              The type of authentication to use       [choices: "basic"]
  --parseUnsupported  If no error should be emitted on unsupported versions
                                                      [boolean] [default: false]
  --version           Show version number                              [boolean]
  --help              Show help                                        [boolean]

Examples:
  fetch-sparql-endpoint.js --endpoint       Fetch 100 triples from the DBPedia
  https://dbpedia.org/sparql --query        SPARQL endpoint
  'SELECT * WHERE { ?s ?p ?o } LIMIT 100'
  fetch-sparql-endpoint.js --endpoint       Run the SPARQL query from query.rq
  https://dbpedia.org/sparql --file         against the DBPedia SPARQL endpoint
  query.rq
  cat query.rq | fetch-sparql-endpoint.js   Run the SPARQL query from query.rq
  --endpoint https://dbpedia.org/sparql     against the DBPedia SPARQL endpoint
```

## License
This software is written by [Ruben Taelman](http://rubensworks.net/).

This code is released under the [MIT license](http://opensource.org/licenses/MIT).

---
_Source: https://npm.io/package/fetch-sparql-endpoint · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
