githubEdit

nodeNode.js SDK

The Node.js SDK spice.jsarrow-up-right is the easiest way to use and query Spice.aiarrow-up-right with Node.js.

It uses Apache Arrow Flightarrow-up-right to efficiently stream data to the client and Apache Arrowarrow-up-right Records as data frames which are then easily converted to JavaScript objects/arrays or JSON.

Requirements

Installation

npm install @spiceai/spice@latest --save

Usage

Import SpiceClient and instantiate a new instance with your configuration.

You can then submit queries using the sql method.

import { SpiceClient } from "@spiceai/spice";

const spiceClient = new SpiceClient({
  apiKey: 'API_KEY',
  httpUrl: 'https://data.spiceai.io',
  flightUrl: 'flight.spiceai.io:443',
});
const table = await spiceClient.sql('SHOW TABLES;');
console.table(table.toArray());

Or using the shorthand with just an API key:

SpiceClient accepts a config object or a string API key:

  • apiKey (string, optional): API key to authenticate with the endpoint.

  • flightUrl (string, optional): URL of the Flight endpoint (default: localhost:50051).

  • httpUrl (string, optional): URL of the HTTP endpoint (default: http://localhost:8090).

  • logging (boolean, optional): Enable or disable logging output (default: true).

sqlJson(query: string) - Execute SQL queries with JSON results

The sqlJson() method executes SQL queries and returns results in a JSON format with schema information.

The response includes:

  • row_count: Number of rows returned

  • schema: Schema information with field names and types

  • data: Array of row objects

  • execution_time_ms: Query execution time in milliseconds

Usage with local Spice runtime

Follow the quickstart guidearrow-up-right to install and run spice locally.

Check Spice OSS documentationarrow-up-right to learn more.

Connection retry

From version 1.0.1arrow-up-right the SpiceClient implements connection retry mechanism (3 attempts by default). The number of attempts can be configured via setMaxRetries:

Retries are performed for connection and system internal errors. It is the SDK user's responsibility to properly handle other errors, for example RESOURCE_EXHAUSTED (HTTP 429).

Contributing

Contribute to or file an issue with the spice.js library at: https://github.com/spiceai/spice.jsarrow-up-right.

Last updated

Was this helpful?