Your first feature

A shared list. Two files. One deploy.

3 MIN READView Markdown ↗

Start in an existing HarmonyOS project, or in an empty folder: init --create makes a blank phone project first. You need Bun, DevEco Studio and access to the ArkAPI GitHub repository. The current install uses your GitHub SSH key.

1. Add ArkAPI#

Run these commands in the project root:

Terminal
bun add -d git+ssh://[email protected]/emincanozcan/arkapi.git
bunx arkapi init
bunx arkapi signup --email [email protected]

Already have an account? Run bunx arkapi login --email [email protected] instead of signup. The CLI prompts for your password.

init adds the client HAR, arkapi.config.ts, a small example and AGENTS.md. The guide is also there for your coding agent.

In an empty folder, run bunx arkapi init --create instead. The project is named after the folder.

2. Write the backend#

Create entry/src/main/ets/pages/todos.api.ts beside your screen:

TypeScript
import { Database } from 'bun:sqlite'
import { ApiError } from 'arkapi/server'

interface Todo {
  id: number
  title: string
}

const db = new Database('app.db')
db.run('create table if not exists todos (id integer primary key, title text)')

export function listTodos(): Todo[] {
  return db.query<Todo, []>('select id, title from todos order by id').all()
}

export function addTodo(title: string): number {
  if (!title.trim()) throw new ApiError('BAD_REQUEST', 'Add a title')
  return Number(db.run('insert into todos (title) values (?)', [title.trim()]).lastInsertRowid)
}

The data is shared by everyone using this app. It survives deploys because app.db lives in the app's working directory. This example has no user authentication; device identity can help partition anonymous data but is not a trusted login.

3. Deploy the functions#

Terminal
bunx arkapi deploy

The first deploy creates your hosted app and writes its ID to arkapi.config.ts. It also generates todos.api.ets with typed client functions and the hosted URL. Later deploys activate a new version after it answers its readiness probe.

4. Connect your screen#

Use this as a page, or copy its state and UI into an existing page:

TypeScript
import { AsyncValue, LiveValue } from 'arkapi'
import { addTodo, listTodos, Todo } from './todos.api'

@Entry
@Component
struct TodosPage {
  @State todos: LiveValue<Todo[]> = new LiveValue(listTodos)
  @State added: AsyncValue<number> = new AsyncValue()
  @State draft: string = ''

  build() {
    Column({ space: 12 }) {
      if (!this.todos.connected) {
        LoadingProgress().width(20)
      }
      ForEach(this.todos.data ?? [], (todo: Todo) => {
        Text(todo.title)
      }, (todo: Todo) => `${todo.id}`)
      TextInput({ text: $$this.draft, placeholder: 'What are we building?' })
      Button('Add')
        .enabled(!this.added.loading)
        .onClick(() => { this.added.run(addTodo(this.draft)) })
      Text(this.todos.error ?? this.added.error ?? '')
    }
    .padding(24)
    .width('100%')
  }
}

addTodo is a mutation. After it runs, ArkAPI re-runs listTodos and sends changed results to subscribed screens. You do not need a manual refresh call. See ArkTS and live data for connection and error states.

5. Build and run the client#

Terminal
ohpm install
hvigorw assembleHap --mode module -p product=default -p buildMode=debug --no-daemon

Use DevEco Studio to configure signing and run on a device or emulator. Build and install after deploy: the backend URL and client types are compiled into the app. Compilation checks the code; running it on a device checks networking and rendering.

Keep building#

For local iteration, run bunx arkapi watch. It regenerates stubs and restarts the local server when backend files change. Emulator stubs use 10.0.2.2; for a phone on your LAN, set deviceHost in arkapi.config.ts. Deploy again before building a client that should use the hosted URL.

Open bunx arkapi open to inspect traffic, logs, environment variables and stored data. Next, add photos and files, HTTP routes or scheduled jobs.

Tab to navigate results · Enter to open · Esc to close