API reference

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

4 MIN READView Markdown ↗

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

FileImport fromRuns on
x.api.tsarkapi/serverBun, on the server
X.ets screensarkapi and the generated ./x.apiThe device
arkapi.config.tsarkapiYour machine, for the CLI

Backend: arkapi/server#

TypeScript
import {
  ApiError, caller, online, event,
  files, every, daily, type Routes,
} from 'arkapi/server'
NameWhat 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.
ApiErrorthrow new ApiError(code, message) fails a call with a code. See Errors.
files.writewrite(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.
everyevery(interval, fn) runs fn on an interval: milliseconds or '30s', '5m', '1h'.
dailydaily('HH:MM', fn) runs fn once a day, in UTC.
RoutesType of export const routes: plain HTTP handlers. See HTTP routes.

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#

TypeScript
import { AsyncValue, LiveValue, ApiError, FileRef, Listening } from 'arkapi'
NameWhat 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.
ApiErrorWhat a failed call rejects with: code and message. NETWORK is added on the device.
FileRef{ id, url }, returned by uploadFile.
ListeningReturned by an event listener; call stop() when the screen goes away.

See Calling from ArkTS.

Screens: the generated ./x.api#

TypeScript
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#

TypeScript
import type { Config } from 'arkapi'

export const config: Config = {}
FieldDefaultMeaning
appwritten by the first deployThe hosted app id.
hosthttps://arkapi.emincanozcan.comArkAPI host.
apiFilesentry/src/main/ets/**/*.api.tsWhere api files are.
port3000Port of watch; if it is busy, watch takes the next free one.
deviceHost10.0.2.2How the device reaches watch; the emulator default. Use your LAN IP for a phone.

CLI: bunx arkapi#

CommandWhat 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, whoamiAccount on the host.
watchLocal server; stubs point at it while it runs.
deployBundles, 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.
openOpens the app's dashboard.
build [--standalone]Bundles without deploying.
Tab to navigate results · Enter to open · Esc to close