DEV Community

Alex Spinov
Alex Spinov

Posted on

Tauri v2 Has a Free API — Here's How to Build Cross-Platform Desktop Apps

Tauri v2 lets you build desktop and mobile apps using web technologies with a Rust backend. Apps are tiny (< 5MB), fast, and secure — unlike Electron's 200MB+ bundles.

Getting Started

npm create tauri-app@latest
cd my-app
npm install
npm run tauri dev
Enter fullscreen mode Exit fullscreen mode

Invoke Rust Commands from JavaScript

// src-tauri/src/lib.rs
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You have been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
Enter fullscreen mode Exit fullscreen mode
// Frontend
import { invoke } from "@tauri-apps/api/core";

const greeting = await invoke("greet", { name: "World" });
console.log(greeting); // Hello, World! You have been greeted from Rust!
Enter fullscreen mode Exit fullscreen mode

File System Access

import { readTextFile, writeTextFile, BaseDirectory } from "@tauri-apps/plugin-fs";

// Read a file
const content = await readTextFile("config.json", { baseDir: BaseDirectory.AppData });

// Write a file
await writeTextFile("output.txt", "Hello from Tauri", { baseDir: BaseDirectory.Desktop });
Enter fullscreen mode Exit fullscreen mode

System Dialogs

import { open, save } from "@tauri-apps/plugin-dialog";

// Open file picker
const file = await open({
  multiple: false,
  filters: [{ name: "Images", extensions: ["png", "jpg"] }]
});

// Save dialog
const savePath = await save({ defaultPath: "export.json" });
Enter fullscreen mode Exit fullscreen mode

Events — Frontend ↔ Backend Communication

import { listen, emit } from "@tauri-apps/api/event";

// Listen for events from Rust
await listen("download-progress", (event) => {
  console.log(`Progress: ${event.payload}%`);
});

// Emit event to Rust
await emit("start-download", { url: "https://example.com/file.zip" });
Enter fullscreen mode Exit fullscreen mode

Window Management

import { getCurrentWindow } from "@tauri-apps/api/window";

const win = getCurrentWindow();
await win.setTitle("My App");
await win.setSize(new LogicalSize(800, 600));
await win.center();
Enter fullscreen mode Exit fullscreen mode

Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.

Top comments (0)