---
title: "API reference"
description: "Everything ArkAPI gives you, and where each name is imported from."
url: "https://arkapi.emincanozcan.com/docs/api"
---

# API reference

Everything ArkAPI gives you, and where each name is imported from.

ArkAPI code is imported from three places. Each one belongs to one kind of file.

| File | Import from | Runs on |
|---|---|---|
| `x.api.ts` | `arkapi/server` | Bun, on the server |
| `X.ets` screens | `arkapi` and the generated `./x.api` | The device |
| `arkapi.config.ts` | `arkapi` | Your machine, for the CLI |

## Backend: `arkapi/server`

```ts
import {
  ApiError, caller, online, event,
  files, every, daily, type Routes,
} from 'arkapi/server'
```

| Name | What it does |
|---|---|
| `caller()` | `{ deviceId }` of the device making the current call. Not available in jobs. |
| `online()` | Device ids with a live connection open right now. |
| `event<T>()` | Export it to create an event; `.send(deviceIds, payload)` pushes it to those devices. |
| `ApiError` | `throw new ApiError(code, message)` fails a call with a code. See [Errors](/docs/errors.md). |
| `files.write` | `write(data, contentType)` stores an `ArrayBuffer` or string and returns its id. |
| `files.read(id)` | Returns the `ArrayBuffer`; `NOT_FOUND` if absent. |
| `files.url(id)` | Public download URL. |
| `files.list()` | `{ id, size }[]` |
| `files.remove(id)` | Deletes the file. |
| `every` | `every(interval, fn)` runs `fn` on an interval: milliseconds or `'30s'`, `'5m'`, `'1h'`. |
| `daily` | `daily('HH:MM', fn)` runs `fn` once a day, in UTC. |
| `Routes` | Type of `export const routes`: plain HTTP handlers. See [HTTP routes](/docs/http.md). |

Types: `Caller`, `ErrorCode`.

Also available in api files: `Database` from `bun:sqlite` for state, `process.env` for secrets, `fetch`, Bun APIs
and npm packages.

## Rules for api files

- Every exported function is an endpoint. Unexported functions stay private.
- A name starting with `get`, `list`, `find`, `search`, `count`, `has`, `is` or `load`, followed by an uppercase
  letter or the end of the name, is a query. Everything else is a mutation. Only queries can back a `LiveValue`.
- Arguments and return values: `string`, `number`, `boolean`, arrays, interfaces, `| null`, `Record<string, T>`,
  and `void` as a return type. Not `any`, `unknown`, `Date`, `Map` or classes.
- Reserved export names: `uploadFile`, `routes`, and `onX` when the file exports an event `x`.

## Screens: `arkapi`

```ts
import { AsyncValue, LiveValue, ApiError, FileRef, Listening } from 'arkapi'
```

| Name | What it does |
|---|---|
| `LiveValue<R>` | `new LiveValue(listTodos)`: data that stays current. Fields: `data`, `loaded`, `error`, `errorCode`, `connected`; `stop()` ends it. |
| `AsyncValue<R>` | For actions: `run(promise)` sets `loading`, then `data` or `error` and `errorCode`. |
| `ApiError` | What a failed call rejects with: `code` and `message`. `NETWORK` is added on the device. |
| `FileRef` | `{ id, url }`, returned by `uploadFile`. |
| `Listening` | Returned by an event listener; call `stop()` when the screen goes away. |

See [Calling from ArkTS](/docs/client.md).

## Screens: the generated `./x.api`

```ts
import { listTodos, addTodo, Todo, uploadFile, onTyping } from './todo.api'
```

`arkapi generate`, `watch` and `deploy` write `x.api.ets` next to each `x.api.ts`. It exports:

- every exported function, returning a `Promise`;
- every interface those functions use;
- `uploadFile(uri)`, returning a `FileRef`;
- `on` + the name of every exported event, for example `onTyping(listener)`, returning a `Listening`.

Never edit it; regenerate instead.

## Config: `arkapi`

```ts
import type { Config } from 'arkapi'

export const config: Config = {}
```

| Field | Default | Meaning |
|---|---|---|
| `app` | written by the first deploy | The hosted app id. |
| `host` | `https://arkapi.emincanozcan.com` | ArkAPI host. |
| `apiFiles` | `entry/src/main/ets/**/*.api.ts` | Where api files are. |
| `port` | `3000` | Port of `watch`; if it is busy, `watch` takes the next free one. |
| `deviceHost` | `10.0.2.2` | How the device reaches `watch`; the emulator default. Use your LAN IP for a phone. |

## CLI: `bunx arkapi`

| Command | What it does |
|---|---|
| `init [--create] [--module entry] [--from <spec>]` | Adds ArkAPI to the HarmonyOS project in this directory. `--create` first makes a blank phone project here (existing files are kept). `--from` installs the CLI from another spec, like a git URL. |
| `signup`, `login [--email E --password P]`, `logout`, `whoami` | Account on the host. |
| `watch` | Local server; stubs point at it while it runs. |
| `deploy` | Bundles, uploads and points stubs at the hosted app. |
| `generate [--local]` | Only regenerates the stubs. |
| `logs [-f]` | Output of the deployed backend. |
| `env [set KEY=VALUE…, unset KEY…]` | Environment variables of the deployed backend. |
| `open` | Opens the app's dashboard. |
| `build [--standalone]` | Bundles without deploying. |
