Introduction to the Clipboard API
Understand the system clipboard, set up the Tauri clipboard plugin, and read or write text from your React frontend
What the Clipboard API Is
Every desktop operating system maintains a shared, temporary storage space called the system clipboard. It holds whatever the user last copied — text, an image, a file reference — and makes that data available to any application that asks for it. The clipboard is what makes Ctrl+C and Ctrl+V work across different programs.
In a Tauri desktop application, your React frontend runs inside a webview. A webview alone has no direct access to the operating system clipboard; the browser security model intentionally blocks that. The Tauri Clipboard API bridges this gap. It gives your React code a safe, permission‑controlled way to read from and write to the system clipboard, exactly like a native desktop app would.
The official plugin is tauri-plugin-clipboard-manager. It exposes a small set of commands — read_text, write_text, write_html, read_image, write_image, and clear — that your frontend can call just like regular JavaScript functions. Under the hood, each call becomes an IPC message to the Rust backend, which then performs the actual clipboard operation on the OS. Dedicated pages cover writing and reading.
Desktop only – with exceptions:
Full clipboard access (text, images, HTML) is available on Windows, macOS, and Linux. Android and iOS support only plain text through this plugin. If your app targets mobile, design your clipboard features accordingly.
Required Plugin
The clipboard plugin must be added to both the Rust backend and the JavaScript frontend. Choose your preferred approach below.
Run a single command from your project root. It installs the Rust crate, registers the plugin in lib.rs, adds the npm package, and creates a default permission file.
npm run tauri add clipboard-manager
(Use yarn, pnpm, or bun equivalents if that’s your package manager.)
After the command completes, skip to the Required Permissions section.
Required Permissions
Tauri v2 does not allow plugins to access sensitive system resources by default. You must explicitly declare which clipboard operations your app is allowed to use. This is done through capability files (JSON files inside src-tauri/capabilities).
The automatic setup creates a capability file for you, often named something like desktop-capability.json. A minimal capability granting read and write text access looks like this:
{
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"clipboard-manager:allow-read-text",
"clipboard-manager:allow-write-text"
]
}
Each permission string corresponds to one command. The plugin provides pairs for each operation:
| Permission identifier | Effect |
|---|---|
clipboard-manager:allow-read-text | Enables readText() in the frontend |
clipboard-manager:allow-write-text | Enables writeText() |
clipboard-manager:allow-read-image | Enables reading images (desktop only) |
clipboard-manager:allow-write-image | Enables writing images |
clipboard-manager:allow-write-html | Enables writeHtml() |
clipboard-manager:allow-clear | Enables clear() |
The deny-* variants exist for explicitly disallowing an operation, but omitting the permission entirely has the same effect.
Only grant what you need:
The clipboard may contain passwords, API keys, or other sensitive data. If your app only writes content, do not add allow-read-text. This follows the principle of least privilege and reduces the surface for accidental data leaks.
First Clipboard Operations
With the plugin installed and permissions configured, you can read and write text directly from your React components.
A Simple Copy‑and‑Paste Component
The example below shows a text field with two buttons. One writes the field content to the clipboard, the other reads whatever is currently on the clipboard and displays it.
import { useState } from "react";
import { writeText, readText } from "@tauri-apps/plugin-clipboard-manager";
function App() {
const [inputText, setInputText] = useState("");
const [clipboardContent, setClipboardContent] = useState("");
const handleCopy = async () => {
await writeText(inputText);
};
const handlePaste = async () => {
const text = await readText();
setClipboardContent(text);
};
return (
<div style={{ padding: "2rem", fontFamily: "sans-serif" }}>
<h1>Tauri Clipboard Demo</h1>
<div style={{ marginBottom: "1rem" }}>
<input
type="text"
value={inputText}
onChange={(e) => setInputText(e.target.value)}
placeholder="Type something to copy"
style={{ padding: "0.5rem", marginRight: "0.5rem" }}
/>
<button onClick={handleCopy}>Copy</button>
</div>
<div>
<button onClick={handlePaste}>Paste from Clipboard</button>
<p>
<strong>Clipboard content:</strong> {clipboardContent || "(empty)"}
</p>
</div>
</div>
);
}
export default App;
When the Copy button is pressed, writeText sends the string to the operating system clipboard, replacing whatever was there before. The Paste button reads whatever text is currently stored and displays it. If the clipboard is empty (or contains a non‑text format like an image), readText returns an empty string.
Confirming it works:
After running the app, type a short phrase, click Copy, then open any other application (Notepad, TextEdit, a browser text field) and paste. The text you copied should appear. Go back to your app and click Paste from Clipboard — the same text should show on the page. This confirms the plugin is installed, permissions are correct, and the clipboard bridge is functional.
Clearing the Clipboard
The clear() function removes all content from the clipboard. Use it to avoid leaving sensitive data there after your app has finished with it.
import { clear } from "@tauri-apps/plugin-clipboard-manager";
async function wipeClipboard() {
await clear();
}
Missing permission stops the app:
If you call a clipboard function without the corresponding capability entry, Tauri will throw an error similar to Permission denied or command not allowed. Always check that your capability file includes the exact permission identifier for the operation you intend to use.
How Beginners Should Think About the Clipboard
The clipboard is shared mutable state — a single slot that any program can overwrite at any time. Your app never owns the clipboard data; it only reads a snapshot or writes a new value. There is no locking, and there is no guarantee that data you just wrote will still be there a moment later, because another application may have copied something else in between.
Two consequences follow:
- Never assume data you read is the same data you wrote. Always check the returned value. An empty string from
readText()means either the clipboard was empty or it held non‑text content. - Minimize the time sensitive data lives on the clipboard. If your app copies a password or API key, clear the clipboard afterward, and design the workflow so the user pastes immediately.
Common Pitfalls
Confusing readText return value:
readText() resolves to an empty string "" when no text is on the clipboard. This is not an error. Always handle the empty case, especially if you plan to pass the value to another API that expects a meaningful string.
Forgetting the plugin initializer:
Skipping .plugin(tauri_plugin_clipboard_manager::init()) in lib.rs leads to obscure IPC errors at runtime. If writeText or readText fails with a message about an unknown command, verify the init line is present and that the Rust crate is correctly installed.
Over‑granting permissions in capability files:
Adding allow-read-text when your app only writes clipboard data creates an unnecessary security hole. A user who later audits the capability file will see a read permission they cannot account for. Keep permission sets minimal.
Where Clipboard Access Appears in Real Applications
Clipboard integration is everywhere in desktop tools. Password managers copy credentials to the clipboard and clear them after a timeout. Code snippet managers let you copy a formatted block with one click. Note‑taking apps import text from the clipboard when you press a hotkey. Collaborative editing tools keep a local clipboard in sync with a remote peer. Any of these workflows can be built with the Tauri clipboard plugin.
Summary
The clipboard plugin gives your React frontend full native clipboard access, but that access is locked behind explicit permissions you control. The core text operations — writeText, readText, clear — are the foundation. Images and HTML build on the same pattern.
- Writing Clipboard covers
writeText,writeHtml,writeImage, and how to handle binary image data. - Reading Clipboard explains
readText,readImage, and how to detect which formats are available. - Common Use Cases shows real‑world patterns like password copy‑with‑clear and importing images from the clipboard.