Creating Napacitor Plugins
Plugins in Napacitor enable JavaScript to interface directly with native APIs. A Napacitor plugin has two halves:
- A TypeScript package published to npm: the API definition (
definitions.ts), the registration entry (index.ts), and an optional browser implementation (web.ts). Itsdist/output ships as CJS +.d.ts, which keeps it compatible with legacy build chains such as Vue CLI 4 / webpack 4 (see the legacy build chain guide). - A C# class library (a
maui/folder inside the npm package) targetingnet10.0and referencingMicrosoft.Maui.Essentials. One C# implementation runs on all Napacitor platforms — Android, iOS, Mac Catalyst, and Windows. There is no Swift/Kotlin code to maintain.
This guide will help you get started creating a shareable Napacitor plugin which will be published on npm. You can also create plugins local to your app. See the MAUI platform guide for details on the host project structure.
Plugin Structure
A Napacitor plugin package looks like this:
my-plugin/
├── src/
│ ├── definitions.ts # API contract (interfaces, enums)
│ ├── web.ts # browser implementation
│ └── index.ts # registerPlugin entry
├── dist/ # built CJS + d.ts (committed)
├── maui/
│ ├── MyPlugin.cs # C# implementation ([NapacitorPlugin])
│ └── Napacitor.MyPlugin.csproj
├── package.json
└── README.md
When the app runs nap sync, the CLI regenerates the host's napacitor.build.props with a <ProjectReference> for each installed plugin's maui/ project. At startup the host's PluginRegistry reflects over all Napacitor* assemblies, finds classes marked with [NapacitorPlugin], and instantiates them — zero manual registration.
Philosophies
Small in Scope
Keep plugins reasonably small. Napacitor plugins add native code to apps that may or may not be used. By keeping the scope small, apps carry a minimal amount of native code, which avoids unnecessary app bloat and App Store rejections due to APIs without usage descriptions.
Unified and Idiomatic
Strive to provide a unified experience across platforms that is familiar to JavaScript developers:
- Prefer
undefinedovernulland other nonvalues. - Prefer camelCase for property names; the Napacitor serializer handles the mapping from C# PascalCase automatically.
- Prefer enums over strings where a fixed set of values exists.
Minimum Viable Plugin
src/definitions.ts:
export interface EchoPlugin {
echo(options: { value: string }): Promise<{ value: string }>;
}
src/index.ts:
import { registerPlugin } from '@napacitor/core';
import type { EchoPlugin } from './definitions';
const Echo = registerPlugin<EchoPlugin>('Echo', {
web: () => import('./web').then((m) => new m.EchoWeb()),
});
export * from './definitions';
export { Echo };
maui/EchoPlugin.cs:
using Napacitor.Plugins;
namespace Napacitor.Plugins.Echo;
[NapacitorPlugin("Echo")]
public class EchoPlugin : NapacitorPluginBase
{
[NapacitorMethod]
public Task<EchoResult> Echo(EchoOptions options)
{
return Task.FromResult(new EchoResult { Value = options.Value });
}
}
public class EchoOptions
{
public string Value { get; set; } = string.Empty;
}
public class EchoResult
{
public string Value { get; set; } = string.Empty;
}
The C# side is covered in depth in the C# Plugin Guide; the browser side in the Web/PWA Guide.