Write the code that reads and writes files in a sandbox.
Before you start
- You need an API key with the
sandboxes:filesscope. See API keys. - The sandbox must be
running. See Create, get, and delete a sandbox. - Each path is an absolute path in the sandbox, such as
/home/user/app/index.ts. Send it in thepathquery parameter.
Write a file
SendPUT /v1/sandboxes/{id}/files?path= with the content of the file as the body. If the file exists, the content replaces it. If a folder of the path does not exist, it is created.
- Send
&mode=0755to set the mode, for example for a script. A new file gets0644. A replaced file keeps its mode. - A file in
/home/user, and each folder that the write makes there, belongs touser. A file in another folder belongs to root. - The body can be up to 128 MiB. A larger body gets
413.
Read a file
SendGET /v1/sandboxes/{id}/files?path=. The answer is the content of the file, with the content type application/octet-stream.
404. The path of a folder answers 409. To see the content of a folder, run ls.
Delete a file or a folder
SendDELETE /v1/sandboxes/{id}/files?path=. A folder is deleted with all of its content. You cannot delete the root folder /.
{"sandboxId": "...", "path": "/home/user/app/dist", "deleted": true}.
Upload a tar
SendPOST /v1/sandboxes/{id}/files/tar?path= with a tar as the body. The path is the folder for the content of the tar. If the folder does not exist, it is created. A file of the tar replaces the file with the same path.
Send the content type application/x-tar for a tar, or application/gzip for a gzip tar. The body can be up to 128 MiB.
.. or an absolute path stays in the folder. In /home/user, each file and folder of the tar belongs to user.
Errors
A revoked API key can work for 30 more seconds on these endpoints.