Clipboard API is the set of browser interfaces that let a web page copy text and other data to the system clipboard, and paste it back, asynchronously through navigator.clipboard. It is defined by the W3C Clipboard API and Events specification and works only in secure contexts. The call most people search for is navigator.clipboard.writeText, which returns a Promise.
navigator.clipboard is a Clipboard object with four methods. writeText and readText handle plain strings. write and read handle ClipboardItem objects, which map a MIME type to a Blob, so one item can carry text/plain, text/html or image/png at once. Browsers commonly support those three formats.
The page also receives copy, cut and paste events. Their clipboardData property is the older, synchronous route and lets you replace what the browser would copy. The asynchronous methods are preferred over the deprecated document.execCommand.
Permission rules differ between reading and writing:
async function copyText(text) {
if (!navigator.clipboard || !globalThis.isSecureContext) return 'unavailable';
try { await navigator.clipboard.writeText(text); return 'copied'; }
catch (e) { return e.name; }
}
console.log(typeof navigator.clipboard, typeof ClipboardItem);
copyText('x').then(console.log);
This was run in Node.js 22.22.0, which has no clipboard, so it shows the guard path. In a browser click handler on an HTTPS page the same function resolves to copied.
undefined undefined
unavailable
It is undefined when the page is not a secure context, which means a plain http: page that is not localhost, and in some embedded or older environments. Serve the page over HTTPS. Check globalThis.isSecureContext, and keep a fallback for the rest.
It fails because the call did not happen during a user action, the document lacks focus, or a policy blocked it. Call writeText directly from a click handler, before any await that waits on slow work. If the text must be fetched first, pass a ClipboardItem containing a Promise of the Blob to write, so the copy is requested inside the gesture.