Skip to main content

command

Attribute Macro command 

Source
#[command]
Expand description

Mark a function as a command handler. It creates a wrapper function with the necessary glue code.

The wrapped function can then be passed to generate_handler! so it can be called from the frontend with invoke().

ⓘ
#[tauri::command]
fn greet(name: String) -> String {
  format!("Hello, {name}!")
}

§Options

The attribute accepts a comma separated list of the following options:

§async

Runs the command on the async runtime instead of blocking the main thread.

async fn commands are always executed asynchronously, so this option is only needed for synchronous functions that should not run on the main thread:

ⓘ
#[tauri::command(async)]
fn expensive_computation() -> u64 {
  // the body runs on the async runtime, so the main thread is not blocked
  42
}

§rename_all

Sets the case convention used to match the command arguments with the keys of the payload sent by the frontend. Either "camelCase" (default) or "snake_case".

ⓘ
// called from JavaScript with `invoke("send_message", { messageBody: "Hello" })`
#[tauri::command]
fn send_message(message_body: String) {}

// called from JavaScript with `invoke("send_message", { message_body: "Hello" })`
#[tauri::command(rename_all = "snake_case")]
fn send_message_snake(message_body: String) {}

§rename

Changes the name used to call the command from the frontend. By default it is the name of the function.

ⓘ
// called from JavaScript with `invoke("greetUser")`
// and still registered as `generate_handler![greet_user]`
#[tauri::command(rename = "greetUser")]
fn greet_user() {}

§root

Path to the tauri crate, used when it is renamed in Cargo.toml or re-exported by another crate. Defaults to ::tauri, and the special value "crate" resolves to $crate (used internally by Tauri itself).

ⓘ
// Cargo.toml: tauri_framework = { package = "tauri", version = "2" }
#[tauri::command(root = "tauri_framework")]
fn my_command() {}

§Inline plugins

When the command belongs to a plugin that is part of your application instead of a standalone crate, annotate it in the generate_handler! list with the #![plugin(your_plugin_name)] inner attribute so build > removeUnusedCommands can match it against the plugin permissions.

§Stability

The output of this macro is managed internally by Tauri, and should not be accessed directly on normal applications. It may have breaking changes in the future.