Prompt for AIHome

Kapsula bridge API

Kapsula runs HTML, plain JavaScript or a standalone TypeScript script in a sandboxed iframe and injects one global object, window.kapsula. Every method returns a Promise. An HTML file can also run in a desktop browser — check if (window.kapsula) before using it.

Formats in Kapsula 0.5.0

Import one self-contained HTML file with CSS/JavaScript, or a plain JavaScript .js script. When pasting JavaScript, select JavaScript (.js); selecting a .js file sets its format. Kapsula wraps the script locally in the same sandbox, provides document.getElementById('app') for your UI and displays console.log/info/warn/error output below it. Top-level await works inside the runner. Add styles with a style element.

Version 0.5.0 also imports one standalone .ts file or pasted code with TypeScript (.ts) selected. Kapsula converts it offline on the device, then runs it in the same sandbox. This removes TypeScript syntax, but is not full type checking. Source is limited to 256 Ki characters. No imports (including import type), exports, npm dependencies, JSX/React, namespaces, decorators or Python. The native bridge is unchanged: no camera, files/share, location or network API was added.

TypeScript guide, workout.ts and AI prompt → · Русская инструкция TypeScript → · SDK declarations

JavaScript guide, prompt and downloadable scripts → · Русская инструкция →

Edit saved source and keep project data

Open the existing project’s settings → Update code. Saved HTML, JavaScript or TypeScript source is loaded into the editor; URL projects show their saved URL. Updating that project keeps its ID and storage. Preserve storage keys and handle older records if you change the data format. A new project gets separate storage. There is no version history or automatic rollback yet: keep your own working source copy.

Your own projects need Kapsula Pro. The built-in demo is free. Prices and the monthly/trial/lifetime products are unchanged in 0.5.0.

Quick facts

Reference

kapsula.app.info()

→ { name, projectId, platform, version, lang, limits }. platform is 'android', 'ios' or 'web'; lang is the shell's interface language ('en', 'ru', 'es', …); limits.alarms is the alarm cap (16). The language is also available synchronously as window.__KAPSULA_PROJECT__.lang.

kapsula.notify.schedule({ at, title, body })

Schedules an exact OS alarm. at — epoch milliseconds or a Date, must be in the future; title ≤ 120 chars (defaults to the project name); body ≤ 300 chars. Returns { id }. On first use the system asks the user for notification permission; if denied, the Promise rejects. Sound and vibration are handled by Kapsula (a built-in alarm sound on both platforms), you don't configure them.

Scheduling requests an OS notification; it does not guarantee delivery or sound. Notification permissions, Focus / Do Not Disturb, Android notification channels and battery settings still apply. Android force-stop can prevent delivery. Test a short notification on your device before relying on it. The HTML page's JavaScript is not a background service.

const { id } = await kapsula.notify.schedule({
  at: Date.now() + 25 * 60 * 1000,
  title: 'Break time',
  body: 'Stand up, look away from the screen'
});

kapsula.notify.cancel(id?)

Cancels one alarm by id, or all alarms of this project when called without arguments. Returns { cancelled }. Tip: call cancel() before re-scheduling so the 16-slot limit doesn't fill up.

kapsula.notify.list()

→ [{ id, at, title, body }] — alarms still pending for this project.

kapsula.storage.get(key) · set(key, value) · remove(key)

Per-project key–value storage that survives app restarts and code updates. value is any JSON-serialisable value; get resolves to it or null. Keep it small (settings, session state), not megabytes.

await kapsula.storage.set('session', { startedAt: Date.now(), step: 3 });
const s = await kapsula.storage.get('session');   // → object or null

kapsula.sound.play(preset)

Plays a short built-in sound while the page is open: 'beep' (default), 'triple', 'alarm'. For sounds while the app is closed, use an alarm instead.

kapsula.haptics.vibrate(ms?)

Vibrates for ms milliseconds (default 300).

Common mistakes in AI-generated code

Assistants that don't know the bridge write a normal browser page. It runs in Kapsula, but the alerts never arrive. Symptoms and fixes:

The quickest fix is to give the assistant the Kapsula prompt and ask it to rewrite the page.

Error handling

Rejections carry a message: time in the past, alarm limit reached, notification permission denied, unknown id. Wrap calls in try/catch and show the message in your UI. In 0.4.0, uncaught syntax/runtime errors appear in the runner. Use Copy the error for your AI and paste it with your source into your own assistant chat. Kapsula does not send it automatically. Handled errors should still be shown by your code. Plain .js scripts also have visible console output; this is not a full debugger.

Complete example — a tea timer

Works in a browser tab (without alarms) and in Kapsula (with a real alarm). Save as tea.html, or paste the code into Kapsula.

<!DOCTYPE html>
<html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tea timer</title>
<style>
  body{font-family:system-ui;background:#0A111F;color:#F0EBE0;margin:0;padding:24px;text-align:center}
  button{font:inherit;font-size:18px;padding:14px 22px;border:0;border-radius:12px;background:#FFB020;margin:8px}
  #left{font-size:64px;font-variant-numeric:tabular-nums;margin:24px 0}
</style></head>
<body>
  <h1>Tea timer</h1>
  <div id="left">–</div>
  <button onclick="start(3)">3 min</button>
  <button onclick="start(5)">5 min</button>
  <button onclick="stop()">Stop</button>
<script>
const K = window.kapsula || null;   // null in a plain browser
let endAt = 0, timer = null;

async function save(){ if (K) await K.storage.set('endAt', endAt); }
async function load(){ endAt = K ? (await K.storage.get('endAt')) || 0 : 0; tick(); }

async function start(min){
  endAt = Date.now() + min*60*1000;
  await save();
  if (K) {
    try {
      await K.notify.cancel();                       // free the slot
      await K.notify.schedule({ at: endAt, title: 'Tea is ready', body: min + ' minutes are up' });
    } catch (e) { alert(e.message); }
  }
  tick();
}
async function stop(){ endAt = 0; await save(); if (K) K.notify.cancel(); tick(); }

function tick(){
  clearTimeout(timer);
  const left = endAt - Date.now();
  if (!endAt) { document.getElementById('left').textContent = '–'; return; }
  if (left <= 0) { document.getElementById('left').textContent = 'Ready!'; if (K) K.sound.play('triple'); endAt = 0; save(); return; }
  const m = Math.floor(left/60000), s = Math.floor(left/1000)%60;
  document.getElementById('left').textContent = m + ':' + String(s).padStart(2,'0');
  timer = setTimeout(tick, 250);
}
load();
</script>
</body></html>

Roadmap (not yet available)

Kapsula executes only code you add yourself. Questions: hello@kapsula.app. See also the prompt for AI assistants.

Try the complete focus-timer walkthrough: prompt, HTML download and phone test