Backend?It's just a function.
Export a TypeScript function. Call it from ArkTS, fully typed. Deploy with one command.
export function addNote(text: string): void {
db.run('insert into notes (text) values (?)', [text])
}import { addNote } from './notes.api'
Button('Add').onClick(() => addNote(this.draft))Less code. Fewer tokens.
The same notes app, built with ArkAPI and with a separate backend plus a hand-written client.
less code
ArkAPI64 linesSeparate backend435 lines
fewer AI tokens
ArkAPI685Separate backend3,657
fewer files
ArkAPI2Separate backend13
networking code
ArkAPI0 linesSeparate backend116 lines
Both versions compile. Blank lines, comments and the generated client are not counted; tokens use o200k. Counted by a script you can run ↗
A feature is two files.
A backend file and the screen that calls it.
import { Database } from 'bun:sqlite'
import { ApiError } from 'arkapi/server'
export interface Note { id: number; text: string }
const db = new Database('app.db')
db.run(`create table if not exists notes (
id integer primary key,
text text
)`)
export function listNotes(): Note[] {
return db.query<Note, []>('select * from notes').all()
}
export function addNote(text: string): number {
if (!text.trim()) {
throw new ApiError('BAD_REQUEST', 'Empty note')
}
const sql = 'insert into notes (text) values (?)'
return Number(db.run(sql, [text]).lastInsertRowid)
}import { AsyncValue, LiveValue } from 'arkapi'
import { addNote, listNotes, Note } from './notes.api'
@Entry
@Component
struct Notes {
// Updates by itself when anyone adds a note.
@State notes: LiveValue<Note[]> = new LiveValue(listNotes)
@State added: AsyncValue<number> = new AsyncValue()
@State draft: string = ''
add() {
this.added.run(addNote(this.draft))
}
build() {
Column() {
ForEach(this.notes.data ?? [], (note: Note) => {
Text(note.text)
}, (note: Note) => `${note.id}`)
TextInput({ text: $$this.draft })
Button('Add').onClick(() => this.add())
}
}
}import { Database } from 'bun:sqlite'
import { files } from 'arkapi/server'
export interface Photo { id: number; url: string }
const db = new Database('app.db')
db.run(`create table if not exists photos (
id integer primary key,
file text
)`)
type Row = { id: number; file: string }
export function listPhotos(): Photo[] {
const rows = db.query<Row, []>('select * from photos').all()
return rows.map((r) => ({ id: r.id, url: files.url(r.file) }))
}
export function addPhoto(fileId: string): void {
db.run('insert into photos (file) values (?)', [fileId])
}import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { LiveValue } from 'arkapi'
import { addPhoto, listPhotos, Photo, uploadFile } from './photos.api'
@Entry
@Component
struct Photos {
@State photos: LiveValue<Photo[]> = new LiveValue(listPhotos)
async add(): Promise<void> {
try {
const picked = await new photoAccessHelper.PhotoViewPicker().select()
if (picked.photoUris.length === 0) return
const file = await uploadFile(picked.photoUris[0]) // stores the image, returns { id, url }
await addPhoto(file.id)
} catch (e) {
console.error(`Photo not added: ${e}`)
}
}
build() {
Column() {
ForEach(this.photos.data ?? [], (photo: Photo) => {
Image(photo.url)
}, (photo: Photo) => `${photo.id}`)
Button('Add photo').onClick(() => this.add())
}
}
}import { Database } from 'bun:sqlite'
import { ApiError, caller } from 'arkapi/server'
const db = new Database('app.db')
db.run(`create table if not exists profiles (
device text primary key,
name text
)`)
export function getName(): string {
const sql = 'select name from profiles where device = ?'
const me = caller().deviceId
const row = db.query<{ name: string }, [string]>(sql).get(me)
return row?.name ?? ''
}
export function setName(name: string): void {
if (name.length > 20) {
throw new ApiError('BAD_REQUEST', 'At most 20 characters')
}
const device = caller().deviceId
db.run('replace into profiles values (?, ?)', [device, name])
}import { AsyncValue, LiveValue } from 'arkapi'
import { getName, setName } from './profile.api'
@Entry
@Component
struct Profile {
@State name: LiveValue<string> = new LiveValue(getName)
@State saved: AsyncValue<void> = new AsyncValue()
@State draft: string = ''
save() {
this.saved.run(setName(this.draft))
}
build() {
Column() {
Text(`Hello, ${this.name.data ?? ''}`)
TextInput({ text: $$this.draft })
Button('Save').onClick(() => this.save())
Text(this.saved.error ?? '')
}
}
}End-to-end static typing.
Rename a field on the backend, and every screen that still reads it stops compiling.
export interface Note {
id: number
- text: string
+ body: string
}Notes.ets:34:25 Property 'text' does not exist on type 'Note'. COMPILE RESULT:FAIL
With a separate backend and a hand-written client, this build passes and every note shows up blank.
Hosting is included.
Every deploy gets traffic, errors, logs, data, secrets and rollback.
v32 min agolivev2yesterdayRoll backWhat a real app needs.
One line each.
Start in two commands.
bun add -d git+ssh://[email protected]/emincanozcan/arkapi.git bunx arkapi init
In active development. One machine, fixed limits, no user sign-in yet.