# Getting Started with JS/TS

## Installation[​](#installation "Direct link to Installation")

<!-- -->

Use your preferred package manager to install the client library:

* npm
* pnpm
* Yarn
* Bun

```
npm i @ikomia/ikclient
```

```
pnpm add @ikomia/ikclient
```

```
yarn add @ikomia/ikclient
```

```
bun add @ikomia/ikclient
```

## Instantiating the client[​](#instantiating-the-client "Direct link to Instantiating the client")

```
import {Client} from '@ikomia/ikclient';

const fluxDeployment = new Client({
  url: 'https://your.scale.endpoint.url',
  token: 'your-api-token', // default to process.env.IKOMIA_TOKEN
});

const results = await fluxDeployment.run({
  parameters: {prompt: 'A cat sitting on a mat'},
});
```

warning

We strongly recommend avoiding exposing your personal token in your code. You can set `IKOMIA_TOKEN` as an environment variable to avoid passing it explicitly.

## Running deployment[​](#running-deployment "Direct link to Running deployment")

For deployment of straightforward workflows, you can simply call `client.run()` optionally passing inputs and parameters.

```
const results = await client.run({
  inputs: [
    'https://raw.githubusercontent.com/Ikomia-dev/notebooks/main/examples/img/img_work.jpg',
    'https://raw.githubusercontent.com/Ikomia-dev/notebooks/main/examples/img/img_starry_night.jpg',
  ],
  parameters: {model_name: 'yolov8m'},
});
```

The `Results` object contains the output of your deployment. The number of outputs, their type and values depends on the deployment you are running and the inputs and parameters you provided.

By default, the results will contains all outputs returned by the *first leaf task* in your workflow. In the common use case where your workflow is a simple chain of algorithms (or just one), this will correspond to the outputs of the last algorithm of the chain.

## Tracking progress[​](#tracking-progress "Direct link to Tracking progress")

You can track the progress of your deployment by adding an `onProgress` callback:

```
const results = await client.run({
  inputs: [
    'https://raw.githubusercontent.com/Ikomia-dev/notebooks/main/examples/img/img_work.jpg',
  ],
  onProgress: progress => {
    // {"run_id": "v4d4mg96bu", "name": "Object Detection Workflow", "uuid": "c0315bef-3642-44ba-9e94-5749881fc297", "state": "PENDING", "eta": [1000, 2000]}
    // ...
    // {"run_id": "v4d4mg96bu", "name": "Object Detection Workflow", "uuid": "c0315bef-3642-44ba-9e94-5749881fc297", "state": "SUCCESS", "eta": [0, 0], "results": Results(...)}
    console.log(progress);
  },
});
```

This callback will be called repeatedly during the polling process of the deployment. It can be useful for logging or updating UIs while the deployment runs.

## Accessing the results[​](#accessing-the-results "Direct link to Accessing the results")

Once the deployment is complete, you can retrieve output data through the `results.getOutput()` method:

```
import {ImageIO} from '@ikomia/ikclient';

const firstOutput = results.getOutput(); // shortcut for results.getOutput(0);
const outputImage = results.getOutput(1, ImageIO);
```

You can pass an optional type argument to `results.getOutput()` to assert what type of output you expect to retrieve. We recommend doing so when you know the expected type of the output. During development, it will provide better type hints in your editor and during runtime, it will throw an error if the output is not of the expected type.

We provide the following standard types:

* `ImageIO`: for image outputs
* `StorageObjectIO`: for outputs saved to your Project's storage

Other output types (e.g. object detection, segmentation, ocr, non-standard outputs, etc.) will be returned as the generic `TaskIO`.

### Image outputs[​](#image-outputs "Direct link to Image outputs")

We provide some utility methods for working with image outputs:

```
const outputImage = results.getOutput(1, ImageIO);

outputImage.toDataURL(); // data:image/png;base64,...
outputImage.toBlob(); // Blob
outputImage.toArrayBuffer(); // ArrayBuffer
```
