# al-orc library

The `@assemblyline/al-orc` package is also the Orchestrator's JavaScript client. The [al-orc CLI](/docs/reference/al-orc) is built on it and calls the same methods.

```js
import { alOrc } from '@assemblyline/al-orc';

const orc = alOrc({ api_key, api_secret });
const { data: stations } = await orc.stations.list({ line: 'IN' });
const { work_record_id } = await orc.stations.run('IN1', { overrides: { dry_run: true } });
```

## `alOrc(credentials)`

| Field | Required | Default | What it is |
|---|---|---|---|
| `api_key` | yes | | An Orchestrator API key |
| `api_secret` | yes | | Its secret |
| `api_url` | no | `https://orchestrator.assemblyline.sh` | The Orchestrator's origin |

The client never reads `process.env` or the CLI's profiles: pass the credentials in. A program that serves several organisations makes one client per organisation. A call without `api_key` or `api_secret` throws.

## Return shapes

| Method | Returns |
|---|---|
| `list(params)` | One page: `{ data, page_context }` |
| `getAll(params)` | Every row, walking the pages, as a flat array. Stops at 15,000 rows. |
| A single record (`get`, `create`, `update`, `archive`, `unarchive`) | The record. A write the factory session must restart for adds `warnings`. |

A request the Orchestrator refuses throws an `ApiError` with `message`, `status` (the HTTP status, such as `404`) and `code`.

## Namespaces

`<id>` is a record's id. A line also takes its code, and a station its short code.

### `orc.lines`

| Method | What it does |
|---|---|
| `list({ location, include_archived, page, limit })` | One page of lines |
| `getAll({ location, include_archived })` | Every line |
| `get(id)` | One line |
| `create({ code, location, name, description })` | Create a line |
| `update(id, data)` | Change a line's fields |
| `archive(id)` | Archive a line; refused while a station on it isn't archived |
| `unarchive(id)` | Unarchive a line; its stations stay archived |
| `delete(id)` | Delete a line; refused while stations are on it |

### `orc.stations`

| Method | What it does |
|---|---|
| `list({ tag, line, include_archived, page, limit })` | One page of stations |
| `getAll({ tag, line, include_archived })` | Every station |
| `get(id)` | One station |
| `create(data)` | Create a station; `data` uses the API's field names |
| `update(id, data)` | Change a station's fields |
| `archive(id)`, `unarchive(id)` | Archive keeps its history, hides it from `list` and blocks its runs |
| `delete(id, { cascade })` | Delete a station; refused while it has work records, unless `cascade: true` deletes them too |
| `run(id, { overrides })` | Start a run. `overrides` changes top-level config keys for this run. Returns `{ work_record_id }`. |
| `getTags(id)` | The station's tags |
| `setTags(id, tag_ids)` | Replace its tags with this set of tag ids |

### `orc.workRecords`

| Method | What it does |
|---|---|
| `list({ status, station, line, tag, page, limit })` | One page of work records. `station` is an id or short code; `line` matches every station on it. |
| `getAll({ status, station, line, tag })` | Every matching work record |
| `get(id, { include })` | One work record; `include: ['report', 'supporting_docs']` adds those |
| `activity(id)` | Its activity events, as an array |
| `cancel(id)` | Cancel a running or pending work record |
| `getTags(id)`, `setTags(id, tag_ids)` | Its tags, as for stations |

### `orc.tags`

| Method | What it does |
|---|---|
| `list()` | All tags: `{ data, page_context }` |
| `create({ name, color, description })` | Create a tag |
| `update(id, data)` | Change a tag |
| `delete(id)` | Delete a tag |
| `resolveNames(names)` | Tag ids for these names; throws `Unknown tag: <name>` for a name that doesn't exist |
| `ensure(name)` | The tag with this name, created when there is none |

### `orc.supportingDocs`

| Method | What it does |
|---|---|
| `get(id)` | A supporting document's metadata, with `content` for a markdown document |
| `downloadStream(id)` | `{ response, url }`: the file's download as a fetch `Response`, for you to write to disk |

### `orc.factorySessions`

| Method | What it does |
|---|---|
| `list({ status, location, factory_name, limit })` | One page of factory sessions, most recently seen first; one per session start |
| `getAll({ status, location, factory_name })` | Every factory session |
| `get(id)` | One session, with `station_breakdown` (completed, failed and other runs per station) and `recent_runs` |

### On the client

| Method | Returns |
|---|---|
| `orc.me()` | `{ org, api_key }`: the organisation and key the credentials belong to |
| `orc.checkConnection()` | `{ connected: true, org, api_key }`, or `{ connected: false, status, error }`. Never throws. |

## Lower-level calls

For an endpoint no namespace covers, the package also exports `apiGet`, `apiPost`, `apiPut`, `apiPatch`, `apiDelete`, `apiGetAll` and `apiGetStream`, which take the path, a body for a write, and `{ credentials, searchParams }`, plus `createTransport`, `buildAuthedUrl`, `ApiError`, `DEFAULT_API_URL` and `MAX_ALL_ROWS`. The endpoints are in the [Orchestrator's API docs](https://orchestrator.assemblyline.sh/docs).
