Write files to a site

write_file

Writes one or more source files straight into the running preview of a site, replacing whatever was at each path.

Scopecode:writeDestructive

It takes up to 20 files per call. Each one replaces the whole file rather than patching it, so read the file first with read_file, and a path that does not exist yet is created.

The preview has to be running: with nothing to write to, the call is refused rather than queued, so open the preview first. Each file is confirmed individually by the preview, and the response lists the paths that were written in written and the ones that failed, with the reason, in failed, so read that rather than assuming a success means everything landed.

It is also refused, with the same message send_message and publish_site give, while the site code is being moved into GitHub or back to Modulify, when Modulify has lost access to the GitHub repository that holds the site or that repository was archived or deleted, and when the code lives in the workspace's own GitHub but the workspace is on the Free plan. See Move your code to GitHub.

This does not go through the site AI, so nothing about the change is explained or reviewed. Once the preview confirms the files they are saved to the site's code straight away, as a Preview sync entry in the code backup history, so read_file, list_files and the File Explorer show them, and saved comes back true. When that save cannot happen yet, for example while a chat is running, saved comes back false and the files stay in the preview until its next save: when the chat ends, when the editor opens or just before a publish.

On a site with several chats it works differently. The files are saved to the site's code straight away, with a Preview sync entry in the code backup history, and the preview does not have to be running. Chats that are running merge their own changes with these when their turns end. While another chat is merging, the call waits, and after 30 seconds it is refused with a 409 reading Another chat is merging changes right now. Try again in a moment! While the site finishes a restore or move, or its preview moves to a new environment or a bigger machine, it is refused with a 409 reading Changes cannot be saved to this site while it finishes a restore or move. Try again in a moment! See Multiple chats.

Use send_message instead when someone described what they want rather than exactly what to write, because the AI reads the surrounding code and this does not. See Backups.

Inputs

Input Type Required Description
projectId string Yes The site id.
files array of objects Yes The files to write, at most 20 per call. Each object has Path, such as app/page.tsx, Content with the complete new contents, and an optional Encoding, utf8 by default or base64 for a binary file. A leading slash in Path is stripped, and a path stepping outside the site or into a .git folder is refused.