Write an environment file
Write a source file in the current workspace to a path in a managed environment.
Endpoint
Writes a source file to a /workspace/ path in the specified environment. A successful call returns 201 and environment file information. A successful write means the file is on disk; it does not mean the model has read it.
/agents/environments/{environment_id}/filesPath parameters
environment_idstringRequiredEnvironment ID, an environment in the current workspace
Request body
typestringRequiredFile reference type, fixed as file_id
file_idstringRequiredSource file ID, from an Agent API upload in the current workspace
pathstringRequiredNormalized absolute path under /workspace/; directory traversal is not allowed
The request body does not accept extra fields. The source file must come from an Agent API upload in the current workspace.
Request example
curl -sS -X POST "$BASE_URL/agents/environments/ccarenv_xxx/files" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "OpenAI-Beta: agents=v1" \
-H "Content-Type: application/json" \
-d '{
"type": "file_id",
"file_id": "file_xxx",
"path": "/workspace/input/input.txt"
}'
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DASHSCOPE_API_KEY"],
base_url=os.environ["BASE_URL"],
)
environment_file = client.beta.agents.environments.files.create(
"ccarenv_xxx",
type="file_id",
file_id="file_xxx",
path="/workspace/input/input.txt",
)
print(environment_file.path, environment_file.size_bytes)
Response fields
environment_idstringEnvironment ID
objectstringEnvironment file object type, fixed as agent.environment.file
pathstringFile path in the environment
size_bytesintegerNumber of bytes written
The response does not contain a new source file ID and is not an artifact publication receipt.
Response example
{
"environment_id": "ccarenv_xxx",
"object": "agent.environment.file",
"path": "/workspace/input/input.txt",
"size_bytes": 18
}
Error codes
| HTTP status | Error type / code | Solution |
|---|---|---|
400 | invalid_request_error / invalid_request_error | Add required fields and fix type and path as described for each field |
404 | not_found_error / not_found_error | Use a source file ID that exists and is visible in the current workspace |
400 | invalid_request_error / invalid_request_error, param=file_id | Use a source file uploaded through POST /agents/file |
409 | invalid_request_error / file_not_available, param=file_id | Use a source file that passed review and is available |
504 | server_error / environment_file_timeout | Retry later. If it keeps failing, submit a ticket with x-request-id |
The error body has the shape {"error":{"type","code","message","param"}}. Gateway errors can use a different structure.