JavaScript Endpoints Guide

Interacting with Tensor One Endpoints is a core feature of the JavaScript SDK. This guide covers synchronous & asynchronous execution, polling status, streaming output, health checks and job control.

Prerequisites

Before continuing, ensure you have:
  • Installed the SDK: tensorone-sdk
  • Set your API key and Endpoint ID

SDK Initialization

import tensoroneSdk from 'tensorone-sdk'

const { TENSORONE_API_KEY, ENDPOINT_ID } = process.env

const tensorone = tensoroneSdk(TENSORONE_API_KEY)
const endpoint = tensorone.endpoint(ENDPOINT_ID)

Run the Endpoint

Synchronous Execution

const result = await endpoint.runSync({
  input: { prompt: 'Hello, World!' },
})
console.log(result)

Asynchronous Execution

const result = await endpoint.run({
  input: { prompt: 'Hello, World!' },
})
console.log(result)

Polling Status of an Async Job

const { id } = await endpoint.run({ input: { prompt: 'Hello, World!' } })

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

while (true) {
  const status = await endpoint.status(id)
  console.log(`Status: ${status.status}`)

  if (status.status === 'COMPLETED') {
    console.log('Output:', status.output)
    break
  } else if (status.status === 'FAILED') {
    console.error('Error:', status)
    break
  }

  await sleep(5000)
}

Streaming Output

Your handler must support return_aggregate_stream: true to enable streaming:
const result = await endpoint.run({ input: { prompt: 'Hello, World!' } })

for await (const update of endpoint.stream(result.id)) {
  console.log(JSON.stringify(update, null, 2))
}

Check Endpoint Health

const health = await endpoint.health()
console.log(health)

Check Job Status

const { id } = await endpoint.run({ input: { prompt: 'Hello, World!' } })
const status = await endpoint.status(id)
console.log(status)

Cancel a Job

const { id } = await endpoint.run({ input: { prompt: 'Hello, World!' } })
const cancel = await endpoint.cancel(id)
console.log(cancel)

Set a Timeout for Execution

const result = await endpoint.run(
  { input: { prompt: 'Hello, World!' } },
  5000 // Timeout in milliseconds
)
console.log(result)

Set an Execution Policy

const result = await endpoint.run({
  input: { prompt: 'Hello, World!' },
  policy: {
    executionTimeout: 5000,
  },
})
console.log(result)

Purge the Queue

const purgeResult = await endpoint.purgeQueue()
console.log(purgeResult)
Use these SDK methods to efficiently integrate, monitor, and control your serverless endpoints in any JavaScript-based application.