Quick start
This page takes you from zero to a mini app that talks to the i-Catholic app. You need a web project that uses a bundler (Vite, esbuild, ...) and Node. The SDK has no runtime dependencies and ships as ESM only. The root package never imports React, so Vue, Svelte or plain JavaScript all work; you need React 18 or later only if you want the hooks.
Three things to remember before you read the example below:
getSDK()returnsnulloutside the app (in a desktop browser). Always check fornullbefore every call, or use the mock bridge during development.- Call
sdk.ready()as soon as your first screen has rendered, so the app removes its loading screen. After 15 seconds, the app shows Mini app took too long to load. - Permissions are requested per mini app; always handle
PERMISSION_DENIED(Lifecycle and permissions).
npm install @i-catholic/mini-app-sdk
| Entry | Contents | Needs React |
|---|---|---|
@i-catholic/mini-app-sdk | types, getSDK, events, errors, earnPoints, scanQr, parseICatholicQr | no |
@i-catholic/mini-app-sdk/react | useSDK, useTheme, useUser, usePoints, useSafeArea | yes (>= 18, optional peer) |
The root entry never imports React. Vanilla JS, Vue, Svelte or any other stack can use it without installing React.
The package has no runtime dependencies and ships ESM only.
Quick start
- Vanilla (TS/JS)
- React
import { getSDK, getSDKErrorCode, whenNativeReady } from '@i-catholic/mini-app-sdk'
// 1. Subscribe to the launch event as early as possible (module top level).
const ready = whenNativeReady().catch(() => null) // rejects outside the app
const sdk = getSDK() // null outside the i-Catholic app — guard every call
async function main() {
renderApp()
if (!sdk) return // running in a desktop browser: show a fallback, or use a mock (see below)
// 2. Tell the host your first screen is painted, so it removes its loading overlay.
sdk.ready()
// 3. Use the bridge.
try {
const user = await sdk.getUser() // asks the user for `user.profile` the first time
sdk.showToast({ title: `Xin chào ${user?.name ?? ''}`, action: 'success' })
} catch (e) {
if (getSDKErrorCode(e) === 'PERMISSION_DENIED') {
// the user refused — carry on without the profile
}
}
// 4. Opened from a share link? Route to it.
const launch = await ready
if (launch?.deepLinkPath) openRoute(launch.deepLinkPath)
}
main()
import { useEffect } from 'react'
import { useSafeArea, useSDK, useUser } from '@i-catholic/mini-app-sdk/react'
export function App() {
const { sdk, inApp, theme, locale } = useSDK()
const { user, loading } = useUser()
const insets = useSafeArea()
useEffect(() => {
sdk?.ready() // first render done — hide the host's loading overlay
}, [sdk])
return (
<main className={theme} style={{ paddingTop: insets.top, paddingBottom: insets.bottom }}>
{!inApp && <p>Open this page in the i-Catholic app.</p>}
{loading ? <p>…</p> : <h1>Xin chào {user?.name ?? 'bạn'}</h1>}
<p>Locale: {locale}</p>
</main>
)
}