Skip to main content

Napacitor Web API

Napacitor has several JavaScript utilities useful for ensuring apps run successfully across multiple platforms with the same codebase. To use them, import from @napacitor/core:

import { Napacitor, registerPlugin, NapacitorException } from '@napacitor/core';

Napacitor Object​

The Napacitor object is a container for several utility functions. It is available as window.Napacitor, but the preferred usage for modern JavaScript apps is to import it.

convertFileSrc​

convertFileSrc: (filePath: string) => string;

In Capacitor this rewrites device file paths (file://…) into WebView-friendly URLs because the WebView serves the app on a special protocol. Napacitor apps run in a MAUI BlazorWebView where static assets share the same origin as the page, so the path is returned unchanged — the same relative URL works in the WebView directly. It exists for API compatibility; no rewriting is performed.

getPlatform()​

getPlatform: () => string;

Get the name of the platform the app is currently running on: web, ios, android, windows, or macos.

if (Napacitor.getPlatform() === 'windows') {
// do something
}

isNativePlatform()​

isNativePlatform: () => boolean;

Check whether the currently running platform is native (ios, android, windows, macos).

if (Napacitor.isNativePlatform()) {
// do something
}

isPluginAvailable(...)​

isPluginAvailable: (name: string) => boolean;

Check if a plugin is available on the currently running platform. The plugin name is used in the plugin registry, which means it also works with custom plugins.

const isAvailable = Napacitor.isPluginAvailable('Camera');

if (!isAvailable) {
// Have the user upload a file instead
} else {
// Otherwise, make the call:
const photos = await Camera.pickPhotos();
}

registerPlugin(...)​

Creates a plugin proxy. On native platforms, calls are forwarded over the injected native bridge (window.NapacitorNative); in the browser, the plugin's web implementation is used:

import { registerPlugin } from '@napacitor/core';

const Echo = registerPlugin('Echo', {
web: () => import('./echo-web').then((m) => new m.EchoWeb()),
});

await Echo.echo({ value: 'hi' });

NapacitorException​

Plugin calls reject with a NapacitorException carrying an error code:

try {
await Echo.echo({ value: 'hi' });
} catch (err) {
if (err instanceof NapacitorException && err.code === 'NOT_IMPLEMENTED') {
// the plugin (or this method) is not available on this platform
}
}

Platform-only methods (like the native-only plugins listed in the Web documentation) throw this exception in the browser.