Docs›Reference

window.tik

The API the app injects into every tik: lifecycle, the viewer, identity for the backend, and providers.

The app injects window.tik before any of your scripts run: no import, no setup, no existence check. It is the same on iOS, Android and the web, and its types are in src/tik.d.ts, which codetik check --fix keeps up to date.

TypeScript
if (tik.visible()) start();tik.on('visible', start);tik.on('hidden', pause);
MemberWhat it givesPresent
visible(), on, offWhether the tik is on screen, and its changesAlways
tikId(), author(), versionThis tik's id, who made it, the API versionAlways
profile()The viewer's username and avatar, for displayWith the profile grant
spacetimedb()The URL and name of the tik's databaseWith a backend
token()The viewer's identity for the backendWith a backend and auth
users.search()Find users who opened the tikWith token()
openrouter, google, providersThe viewer's own AI key or Google DriveWhen the provider is declared

An optional member exists only when its grant is on, so presence is the feature test: if (tik.profile). Locale and time come from the standard web APIs (navigator.language, Intl).

Lifecycle

TypeScript
tik.visible(): booleantik.on(event: 'visible' | 'hidden', handler: () => void): voidtik.off(event: 'visible' | 'hidden', handler: () => void): void

The feed loads tiks and slides before anyone reaches them, and document.hidden stays false off screen, so visible() is the only reliable signal. Its value is right at first paint, and events fire only on later changes, never when you subscribe: read it and subscribe. Run loops, timers, sound and backend traffic only while visible.

The tik

TypeScript
tik.version: stringtik.tikId(): stringtik.author(): { username: string; avatarUrl: string | null; link: string }

tikId() is stable for the life of the page. author() is who made the tik, with link to their profile page. Both are for display: a fork can say anything, so neither proves who someone is.

profile

TypeScript
tik.profile?(): Promise<{ username: string; avatarUrl: string | null; link: string }>

The signed-in viewer, with the profile grant. Absent when nobody is signed in. It is page data for display; a backend cannot trust it and reads the signed name from token() instead.

TypeScript
if (tik.profile) {  const me = await tik.profile();  greet(me.username);}

spacetimedb

TypeScript
tik.spacetimedb?(): Promise<{ url: string; database: string }>

Where the tik's own database lives, with a backend on. A promise because the app may set it up first. Never hardcode either value; they differ between development and the published tik. See Backend.

token

TypeScript
tik.token?(): Promise<string>

A short-lived token that identifies the viewer to the tik's own backend, with a backend and the auth grant. Call it for each connection and never store it: it expires, and the app caches it while it is valid. Every other database refuses it.

When the app cannot mint one it rejects with .code === 'unavailable'; treat the viewer as anonymous then. An anonymous viewer gets a new identity on every connection, so nothing survives a reload.

TypeScript
tik.users?.search(query: string): Promise<{ username: string; avatarUrl: string | null; identity: string }[]>

People who opened this tik, the author first, with their backend identity as hex. Present with token(). It answers the author, or every viewer when options.publicUsers is on in tik.yaml, and rejects with .code === 'unavailable' otherwise. Use it to let the author pick admins or invite users.

Providers

TypeScript
tik.openrouter?: TikOpenRoutertik.google?: TikGoogletik.providers?: TikProviders

The viewer's own OpenRouter account and Google Drive, present only when the tik declares the provider. See Providers.

Errors

A rejected call carries a .code:

CodeMeaning
unavailabletoken() or users.search() cannot answer right now

Provider calls have codes of their own, listed in Providers.