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.
if (tik.visible()) start();tik.on('visible', start);tik.on('hidden', pause);| Member | What it gives | Present |
|---|---|---|
visible(), on, off | Whether the tik is on screen, and its changes | Always |
tikId(), author(), version | This tik's id, who made it, the API version | Always |
profile() | The viewer's username and avatar, for display | With the profile grant |
spacetimedb() | The URL and name of the tik's database | With a backend |
token() | The viewer's identity for the backend | With a backend and auth |
users.search() | Find users who opened the tik | With token() |
openrouter, google, providers | The viewer's own AI key or Google Drive | When 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
tik.visible(): booleantik.on(event: 'visible' | 'hidden', handler: () => void): voidtik.off(event: 'visible' | 'hidden', handler: () => void): voidThe 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
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
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.
if (tik.profile) { const me = await tik.profile(); greet(me.username);}spacetimedb
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
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.
users.search
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
tik.openrouter?: TikOpenRoutertik.google?: TikGoogletik.providers?: TikProvidersThe 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:
| Code | Meaning |
|---|---|
unavailable | token() or users.search() cannot answer right now |
Provider calls have codes of their own, listed in Providers.