Files
Write a file
Write a file to a cloud browser’s machine for the agent to use.
POST
/
api
/
v2
/
sessions
/
{session_id}
/
files
/
write_file
import base64
from hai_agents import Client
client = Client()
with open("cv.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode()
client.session_files.write_file(session_id, path="~/Downloads/cv.pdf", content_base64=content)
import { readFileSync } from "node:fs";
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
await client.sessionFiles.writeFile({
sessionId,
path: "~/Downloads/cv.pdf",
contentBase64: readFileSync("cv.pdf").toString("base64"),
});
curl -X POST https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/files/write_file \
-H "Authorization: Bearer $HAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"path\": \"~/Downloads/cv.pdf\", \"content_base64\": \"$(base64 -i cv.pdf)\"}"
{
"path": "/home/hai/Downloads/cv.pdf",
"size_bytes": 182044
}
Behavior
Any existing file at the same path is overwritten. Files over 8 MiB are rejected. Only~/Downloads is writable, while the cloud browser runs.
Tell the agent where the file is, for example with a message: “Upload ~/Downloads/cv.pdf in the application form.”
Path parameters
string
required
The agent session ID, or the browser session ID from its
RunnerSessionEvent.Request body
string
required
Destination path. Absolute, or relative to the browser’s home (
~/Downloads/cv.pdf).string
required
The file’s bytes, base64-encoded.
Response
200 with the written file.
string
The written file’s resolved path.
integer
Bytes written.
import base64
from hai_agents import Client
client = Client()
with open("cv.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode()
client.session_files.write_file(session_id, path="~/Downloads/cv.pdf", content_base64=content)
import { readFileSync } from "node:fs";
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
await client.sessionFiles.writeFile({
sessionId,
path: "~/Downloads/cv.pdf",
contentBase64: readFileSync("cv.pdf").toString("base64"),
});
curl -X POST https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/files/write_file \
-H "Authorization: Bearer $HAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"path\": \"~/Downloads/cv.pdf\", \"content_base64\": \"$(base64 -i cv.pdf)\"}"
{
"path": "/home/hai/Downloads/cv.pdf",
"size_bytes": 182044
}
~/Downloads/cv.pdf in the application form.”
Errors
| Status | Cause |
|---|---|
400 | content_base64 is not valid base64. |
403 | Path outside ~/Downloads, or not a file. |
404 | No browser session with this id. |
413 | File over 8 MiB. |
Was this page helpful?
⌘I
import base64
from hai_agents import Client
client = Client()
with open("cv.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode()
client.session_files.write_file(session_id, path="~/Downloads/cv.pdf", content_base64=content)
import { readFileSync } from "node:fs";
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
await client.sessionFiles.writeFile({
sessionId,
path: "~/Downloads/cv.pdf",
contentBase64: readFileSync("cv.pdf").toString("base64"),
});
curl -X POST https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/files/write_file \
-H "Authorization: Bearer $HAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"path\": \"~/Downloads/cv.pdf\", \"content_base64\": \"$(base64 -i cv.pdf)\"}"
{
"path": "/home/hai/Downloads/cv.pdf",
"size_bytes": 182044
}