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.

Snippets below are aligned with the runnable amba-sdks/examples/ kitchen-sinks — each platform's example app is a clone-and-go reference that exercises the same operations end-to-end against staging. A canonical source-path map is at the bottom of this page so you can jump from any snippet to the full surrounding context (auth setup, error handling, prerequisites). Automated import-from-source synchronization is on the roadmap (Task #28 follow-up); until then, deviations between this page and the kitchen-sinks should be filed as docs bugs.

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');

Source-of-truth map

Each snippet above is a focused excerpt; the running version (with surrounding setup, error handling, and prerequisites) lives in the matching amba-sdks/examples/ kitchen-sink. Use the table below to jump straight to the canonical source for any cell.

LanguageInitIdentifyTrack eventCollection insertStorage uploadPush register
Webexamples/web/src/lib/amba.tsexamples/web/src/main.ts (btn-anon)examples/web/src/main.ts (btn-anon)examples/web/src/main.ts (btn-insert)gated on staging server route — see web.mdx Implementation statusgated on staging server route — see web.mdx Implementation status
Nodeexamples/node/src/amba.tsexamples/node/src/index.ts (§ 3)examples/node/src/routes/webhook-stripe.tsexamples/node/src/routes/orders.tsexamples/node/src/index.ts (§ Storage)examples/node/src/index.ts (§ Push fan-out)
Reactexamples/react/src/lib/amba.ts + src/main.tsxexamples/react/src/components/UseUserSection.tsxexamples/react/src/components/UseTrackOnMountSection.tsxexamples/react/src/components/UseCollectionSection.tsxexamples/react/src/components/UseAmbaSection.tsx (escape hatch)shared with Web — same Amba.push.register surface
React Nativeexamples/react-native/App.tsxexamples/react-native/App.tsx (signInAnonymously)examples/react-native/App.tsx (trackEvent)examples/react-native/App.tsx (insertCollection)bare-RN-specific blob handling — see react-native.mdx § 7examples/react-native/App.tsx + react-native.mdx § 6
Swiftexamples/ios/AmbaKitchenSink/AmbaKitchenSinkApp.swiftexamples/ios/AmbaKitchenSink/ContentView.swift (signInAnonymously)examples/ios/AmbaKitchenSink/ContentView.swift (events.track)examples/ios/AmbaKitchenSink/ContentView.swift (collections.insert)examples/ios/AmbaKitchenSink/ContentView.swift (storage section)examples/ios/AmbaKitchenSink/AppDelegate.swift (APNs callback)
Kotlinexamples/android/app/src/main/java/com/layers/amba/example/MainActivity.ktexamples/android/app/src/main/java/com/layers/amba/example/MainActivity.ktexamples/android/app/src/main/java/com/layers/amba/example/MainActivity.ktexamples/android/app/src/main/java/com/layers/amba/example/MainActivity.ktAndroid storage — see android.mdxexamples/android/app/src/main/java/com/layers/amba/example/MainActivity.kt (FCM)
Dartexamples/flutter/lib/main.dart (main())examples/flutter/lib/main.dartexamples/flutter/lib/main.dartexamples/flutter/lib/main.dartFlutter storage — see flutter.mdxexamples/flutter/lib/main.dart
C#examples/unity/AmbaKitchenSink/Assets/Scripts/AmbaBootstrap.csexamples/unity/AmbaKitchenSink/Assets/Scripts/KitchenSinkUI.csexamples/unity/AmbaKitchenSink/Assets/Scripts/KitchenSinkUI.csexamples/unity/AmbaKitchenSink/Assets/Scripts/KitchenSinkUI.csUnity storage — see unity.mdxexamples/unity/AmbaKitchenSink/Assets/Scripts/KitchenSinkUI.cs

Cells marked gated on staging server route or see <platform>.mdx correspond to surfaces that are SDK-ready but still landing server-side. The per-platform doc pages carry the in-flight status; once each server route ships, the matching kitchen-sink and this snippet block both round-trip.

See also

On this page