The API example application (examples/api/) serves as a comprehensive reference implementation demonstrating Tauri's core features and patterns. It showcases real-world usage of commands, events, channels, plugins, window management, menus, tray icons, and mobile support. This application is both a validation tool for the Tauri framework and a learning resource for developers building Tauri applications.
For information about defining custom commands, see Command Definition and Registration. For details on IPC mechanisms, see IPC and Frontend-Backend Communication.
The API example follows the standard Tauri project layout with separate frontend and backend directories:
Sources: examples/api/src-tauri/Cargo.toml1-43 examples/api/src-tauri/src/lib.rs1-221 examples/api/src-tauri/src/main.rs1-11 examples/api/src/App.svelte1-220
The backend configuration declares key dependencies and features:
| Dependency | Purpose |
|---|---|
tauri | Core framework with features: protocol-asset, image-ico, image-png, isolation, macos-private-api, tray-icon |
tauri-build | Build-time code generation with codegen and isolation features |
tauri-plugin-sample | Example plugin demonstrating plugin architecture |
tauri-plugin-log | Logging infrastructure |
serde / serde_json | Serialization for IPC |
The prod feature flag enables tauri/custom-protocol for production builds.
Sources: examples/api/src-tauri/Cargo.toml13-43
The application entry point is minimal, delegating to api_lib::run() which calls run_app() with default configuration.
Sources: examples/api/src-tauri/src/main.rs8-10 examples/api/src-tauri/src/lib.rs32-35 examples/api/src-tauri/src/lib.rs37-205
The run_app() function orchestrates application initialization through several stages:
tauri_plugin_log and tauri_plugin_sample examples/api/src-tauri/src/lib.rs43-48desktop and not test) examples/api/src-tauri/src/lib.rs50-55AppMenu and PopupMenu state via app.manage() examples/api/src-tauri/src/lib.rs57-67WebviewWindowBuilder examples/api/src-tauri/src/lib.rs69-105ping() method with a Channel examples/api/src-tauri/src/lib.rs111-117Sources: examples/api/src-tauri/src/lib.rs41-143
The application registers four command handlers using tauri::generate_handler!:
These commands are defined in the cmd module and handle various operations including logging with scope validation, complex requests, echoing data, and streaming data via Channel.
Sources: examples/api/src-tauri/src/lib.rs146-152 examples/api/src-tauri/src/cmd.rs23-63
The application demonstrates bidirectional event communication:
The page load handler registers an event listener that responds to JavaScript events with Rust events, demonstrating the pub-sub pattern using webview.listen and webview.emit.
Sources: examples/api/src-tauri/src/lib.rs129-143
The application implements custom window lifecycle management in the app.run loop:
| Event | Handler Behavior |
|---|---|
ExitRequested | Prevents exit to keep event loop running for tray icon events (desktop only) examples/api/src-tauri/src/lib.rs168-170 |
CloseRequested | Prevents default close, manually calls destroy() on the window examples/api/src-tauri/src/lib.rs172-186 |
SceneRequested | On iOS, creates a new WebviewWindow for each scene request examples/api/src-tauri/src/lib.rs188-197 |
DocumentTitleChanged | Logs title changes for main window examples/api/src-tauri/src/lib.rs70-72 |
Sources: examples/api/src-tauri/src/lib.rs161-204 examples/api/src-tauri/src/lib.rs70-72
The application demonstrates plugin usage through the tauri-plugin-sample:
This showcases the SampleExt trait that extends App with plugin-specific methods and demonstrates the Channel API for streaming communication.
Sources: examples/api/src-tauri/src/lib.rs48 examples/api/src-tauri/src/lib.rs111-117
The main window is configured with comprehensive options:
The on_new_window handler intercepts window.open() requests from JavaScript, creating a new WebviewWindow with specific features and tracking.
Sources: examples/api/src-tauri/src/lib.rs69-103
The frontend is built with Svelte and organized into a modular view system:
Desktop-specific views (App, Window, Menu, Tray) are conditionally included based on user agent detection.
Sources: examples/api/src/App.svelte8-70
The frontend demonstrates various IPC patterns:
| Pattern | Implementation | Example |
|---|---|---|
| Command Invocation | invoke() from @tauri-apps/api/core | Menu toggle via `invoke('plugin:app-menu |
| Window Access | getCurrentWebviewWindow() | Register drag-drop event handlers examples/api/src/App.svelte22-25 |
| Theme Management | setTheme() from @tauri-apps/api/app | Sync theme with native system examples/api/src/App.svelte91 |
Sources: examples/api/src/App.svelte4-6 examples/api/src/App.svelte18 examples/api/src/App.svelte22-25 examples/api/src/App.svelte91
The application detects mobile platforms and adjusts the UI:
android or iphone in navigator.userAgent examples/api/src/App.svelte27-28Sources: examples/api/src/App.svelte27-28 examples/api/src/App.svelte64 examples/api/src/App.svelte155-220
The application includes a test that verifies basic functionality:
The test uses tauri::test::mock_builder() to create a test environment, launches the application, and closes the window from a background thread to verify shutdown.
Sources: examples/api/src-tauri/src/lib.rs208-221
The build script integrates tauri-build for compile-time code generation and security configuration:
This configuration enables the isolation pattern and explicitly defines allowed commands for the application manifest and inlined plugins.
Sources: examples/api/src-tauri/build.rs8-23 examples/api/src-tauri/Cargo.toml13-17
Refresh this wiki