> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tensorone.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript Endpoints

> Complete guide to using the Tensor One JavaScript SDK for endpoint interaction, including synchronous/asynchronous execution, streaming, job management, and health monitoring.

***

# 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

```js
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

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

### Asynchronous Execution

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

***

## Polling Status of an Async Job

```js
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:

```js
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

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

***

## Check Job Status

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

***

## Cancel a Job

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

***

## Set a Timeout for Execution

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

***

## Set an Execution Policy

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

***

## Purge the Queue

```js
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.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.