Amba

Code samples

Side-by-side code samples for the 6 canonical amba operations — init, identify, track, collection insert, storage upload, push register — across all 8 SDK languages.

Every amba SDK exposes the same 6 canonical operations. The snippets below show the idiomatic shape per language so you can compare ergonomics before committing to a stack.

API key placeholder is amb_dev_ck_XXXX throughout — replace with the dev key from amba init or app.amba.dev before running.

Each snippet shows the idiomatic shape per language. The per-platform quickstart pages (Web, Node, React, React Native, Expo, iOS, Android, Flutter, Unity) carry the full setup — auth flow, error handling, push registration — surrounding each operation.

1. Init

Configure the SDK once at app start.

import { Amba } from '@layers/amba-web';
 
await Amba.configure({
  apiKey: import.meta.env.VITE_AMBA_API_KEY!, // 'amb_dev_ck_XXXX'
});

2. Identify (sign in)

Establish an authenticated appUserId you can attach data to. Sample shown for anonymous sign-in; every SDK also supports email, Sign in with Apple, and Sign in with Google.

const session = await Amba.auth.signInAnonymously();
console.log('signed in as', session.user.id);

3. Track an event

await Amba.events.track('app_opened', { source: 'direct' });

4. Collection insert

Insert a row into a collection already created via amba collections create posts --field title:text --field body:text. The server stamps user_id from the active session.

const post = await Amba.collections.insert<{ id: string; title: string; body: string }>('posts', {
  title: 'Hello amba',
  body: 'first post',
});

5. Storage upload

const asset = await Amba.storage.upload({
  bucket: 'avatars',
  file, // File | Blob from <input type="file">
});
console.log(asset.url);

6. Push register

const registration = await navigator.serviceWorker.register('/amba-sw.js');
const subscription = await registration.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: 'YOUR_VAPID_PUBLIC_KEY',
});
await Amba.push.register(JSON.stringify(subscription), 'web');

See also

On this page