tensorone logo

SDKs

Javascript Endpoints

JavaScript Endpoints Guide

Interacting with Tensor One endpoints is a core feature of the JavaScript SDK. This guide covers both synchronous and asynchronous execution methods, polling for status, streaming output, and more.


Prerequisites

  • Installed the tensorone-sdk package
  • Configured your API key
  • Set your Endpoint ID
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 Asynchronous Run

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

To stream results, your handler must support return_aggregate_stream: true.

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 Status of a Job

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 ms
)
console.log(result)

Set an Execution Policy

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

Purge Queue

const purgeResult = await endpoint.purgeQueue()
console.log(purgeResult)

Use these methods to effectively manage and integrate your endpoints in any JavaScript-based application.

Previous
Javascript