A Tauri plugin that integrates the Axum web framework directly into your Tauri application. It provides a convenient way to expose APIs via custom protocols or through an HTTP-like interface inside the Tauri WebView.
-
Custom protocol registration
// Important: if you use a custom protocol, register it *before* the setup hook. // https://docs.rs/tauri/2.8.2/tauri/plugin/struct.Builder.html#known-limitations tauri::Builder::default().plugin(tauri_plugin_axum::init(Router::new()))
Once registered, you can access your Axum routes via:
- macOS, iOS, and Linux:
axum://localhost/<path>
- Windows and Android:
http://axum.localhost/<path>
(default)
⚠️ Note: Custom protocols currently do not support streaming. (#1404)
-
Partial stream body support
Supports streaming responses using either the provided fetch API or an Axios adapter:
import { fetch } from "@mcitem/tauri-plugin-axum/fetch"; import { Adapter } from "@mcitem/tauri-plugin-axum/axios";
Rust crate:
cargo add tauri-plugin-axum
npm package:
# Optional
pnpm i @mcitem/tauri-plugin-axum
Add required capability in src-tauri/capabilities/default.json
:
// URI scheme protocols are registered when the WebView is created.
// If the plugin is registered after a WebView has been created,
// the protocol will not be available.
//
// macOS, iOS, Linux: axum://localhost/<path>
// Windows, Android: http://axum.localhost/<path> (default)
tauri::Builder::default().plugin(tauri_plugin_axum::init(Router::new()));
window.fetch("http://axum.localhost/");
#[derive(Serialize, Deserialize)]
struct Greet {
axum: String,
tauri: String,
}
async fn post_handle(Json(j): Json<Greet>) -> Json<Greet> {
Json(Greet {
axum: format!("axum, {}!", j.axum),
tauri: format!("tauri, {}!", j.tauri),
})
}
// Initialize router asynchronously
tauri::Builder::default()
.setup(|app| {
let path = app.path().app_config_dir()?;
app.handle().plugin(tauri_plugin_axum::block_init(async {
println!("Application config path: {:?}", path);
router::router()
}))?;
Ok(())
});
// Using fetch
import { fetch } from "@mcitem/tauri-plugin-axum/fetch";
fetch("/", { method: "GET" })
.then((res) => res.text())
.then((res) => console.log(res));
// Using Axios adapter
import axios from "axios";
import { Adapter } from "@mcitem/tauri-plugin-axum/axios";
const instance = axios.create({ adapter: Adapter });
git clone https://github.com/mcitem/tauri-plugin-axum
cd tauri-plugin-axum
pnpm install
pnpm build
pnpm --filter tauri-app install
pnpm --filter tauri-app tauri dev