Cross-platform runtime library that enables web applications to run natively on iOS, Android, Web, and other platforms with unified JavaScript APIs
npx @tessl/cli install tessl/npm-capacitor--core@7.4.0Capacitor Core is the foundational runtime library for Capacitor, a cross-platform framework that enables web applications to run natively on iOS, Android, Web, and other platforms. It provides a unified JavaScript API for accessing native device features and manages the bridge between web and native code implementations.
npm install @capacitor/coreimport { Capacitor, registerPlugin } from "@capacitor/core";
import { WebPlugin } from "@capacitor/core";
import { CapacitorCookies, CapacitorHttp, WebView } from "@capacitor/core";For CommonJS:
const { Capacitor, registerPlugin, WebPlugin } = require("@capacitor/core");import { Capacitor, registerPlugin } from "@capacitor/core";
// Check platform
const platform = Capacitor.getPlatform(); // 'android', 'ios', or 'web'
const isNative = Capacitor.isNativePlatform(); // true for native platforms
// Check plugin availability
const available = Capacitor.isPluginAvailable('Camera');
// Register a custom plugin
interface MyPlugin {
echo(options: { value: string }): Promise<{ value: string }>;
}
const MyPlugin = registerPlugin<MyPlugin>('MyPlugin', {
web: () => import('./web/my-plugin').then(m => new m.MyPluginWeb()),
});
// Use built-in HTTP client
import { CapacitorHttp } from "@capacitor/core";
const response = await CapacitorHttp.get({
url: 'https://api.example.com/data',
headers: {
'Content-Type': 'application/json',
},
});Capacitor Core is built around several key components:
Core platform detection, plugin management, and runtime functionality. Essential for all Capacitor applications.
interface CapacitorGlobal {
getPlatform(): string;
isNativePlatform(): boolean;
isPluginAvailable(name: string): boolean;
registerPlugin: RegisterPlugin;
convertFileSrc(filePath: string): string;
Exception: typeof CapacitorException;
DEBUG?: boolean;
isLoggingEnabled?: boolean;
}
type RegisterPlugin = <T>(
pluginName: string,
implementations?: PluginImplementations
) => T;Base classes and utilities for developing custom Capacitor plugins with cross-platform support.
interface Plugin {
addListener(eventName: string, listenerFunc: Function): Promise<PluginListenerHandle>;
removeAllListeners(): Promise<void>;
}
class WebPlugin implements Plugin {
addListener(eventName: string, listenerFunc: ListenerCallback): Promise<PluginListenerHandle>;
removeAllListeners(): Promise<void>;
protected notifyListeners(eventName: string, data: any, retainUntilConsumed?: boolean): void;
protected unimplemented(msg?: string): CapacitorException;
protected unavailable(msg?: string): CapacitorException;
}Native HTTP client with web fallback for making network requests with advanced features like custom headers, timeouts, and response type handling.
interface CapacitorHttpPlugin {
request(options: HttpOptions): Promise<HttpResponse>;
get(options: HttpOptions): Promise<HttpResponse>;
post(options: HttpOptions): Promise<HttpResponse>;
put(options: HttpOptions): Promise<HttpResponse>;
patch(options: HttpOptions): Promise<HttpResponse>;
delete(options: HttpOptions): Promise<HttpResponse>;
}
interface HttpOptions {
url: string;
method?: string;
data?: any;
headers?: HttpHeaders;
params?: HttpParams;
readTimeout?: number;
connectTimeout?: number;
responseType?: HttpResponseType;
}
interface HttpResponse {
data: any;
status: number;
headers: HttpHeaders;
url: string;
}
function buildRequestInit(options: HttpOptions, extra?: RequestInit): RequestInit;Cross-platform cookie management with native implementations and web fallbacks.
interface CapacitorCookiesPlugin {
getCookies(options?: GetCookieOptions): Promise<HttpCookieMap>;
setCookie(options: SetCookieOptions): Promise<void>;
deleteCookie(options: DeleteCookieOptions): Promise<void>;
clearCookies(options: ClearCookieOptions): Promise<void>;
clearAllCookies(): Promise<void>;
}
type SetCookieOptions = {
key: string;
value: string;
url?: string;
path?: string;
expires?: string;
};WebView configuration and path management for hybrid applications.
interface WebViewPlugin extends Plugin {
setServerAssetPath(options: WebViewPath): Promise<void>;
setServerBasePath(options: WebViewPath): Promise<void>;
getServerBasePath(): Promise<WebViewPath>;
persistServerBasePath(): Promise<void>;
}
interface WebViewPath {
path: string;
}type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';
interface PluginListenerHandle {
remove(): Promise<void>;
}
type PluginCallback = (data: PluginResultData, error?: PluginResultError) => void;
type ListenerCallback = (err: any, ...args: any[]) => void;
interface PluginResultData {
[key: string]: any;
}
interface PluginResultError {
message: string;
}
interface PluginImplementations {
[platform: string]: (() => Promise<any>) | any;
}
enum ExceptionCode {
Unimplemented = 'UNIMPLEMENTED',
Unavailable = 'UNAVAILABLE',
}
class CapacitorException extends Error {
constructor(
readonly message: string,
readonly code?: ExceptionCode,
readonly data?: ExceptionData,
);
}
interface ExceptionData {
[key: string]: any;
}