Create environment
Create a managed runtime environment with a container, dependencies, network policy, initial files, and skills.
Endpoint
Creates a managed runtime environment. A successful call returns 201, which means the environment is registered and asynchronous preparation has started. Creating a session that references the environment before preparation completes can fail.
/agents/environmentsRequest body
environmentobjectRequiredEnvironment configuration, see below
Properties
typestringRequiredEnvironment type, fixed as openai_hosted
environment_template_idstringOptionalEnvironment template ID; the template must belong to the current workspace. Manage templates through Environment templates.
envobjectOptionalEnvironment variables; names must match [A-Za-z_][A-Za-z0-9_]*, cannot be PATH, and cannot start with a reserved prefix; values cannot contain NUL
packagesobjectOptionalPackages grouped by npm, python, and system
networkobjectOptionalNetwork policy, see below
Properties
accessstringOptionalNetwork access mode, enabled, disabled, or restricted
allowed_domainsarray<string>OptionalAllowed domains, required and non-empty when access=restricted; host names only, with no scheme, port, or wildcard
filesarrayOptionalInitial files; currently uses file_id to reference source files
Properties
typestringRequiredFile reference type, fixed as file_id
pathstringRequiredAbsolute path in the environment; must be under /workspace/
file_idstringRequiredSource file ID returned by POST /agents/file
desktopobjectOptionalDesktop settings; only enabled=false is accepted, and enabled=true returns 400
setup_commandsarrayOptionalSetup commands executed in order. Each item requires command and accepts an optional cwd; cwd defaults to /workspace
skillsarrayOptionalSkills; currently uses skill_reference, up to 32 items
Properties
typestringRequiredSkill reference type, fixed as skill_reference
skill_idstringRequiredSkill resource ID
versionstringOptionalSkill version; if omitted, latest is resolved and frozen to a specific version
capability_directoriesarrayOptionalCapability discovery directories; must be under /workspace/
vault_idsarray<string> / nullOptionalVaults to attach, up to 10
A non-empty plugins array is rejected. desktop.enabled=true is rejected. skill_id comes from the Skill API. Create or list skills first: Create a Skill or List Skills.
Create accepts an optional Idempotency-Key header. The same key and request body in the same workspace return the existing resource; the same key with a different body returns 409.
When mounting an unrestricted credential, network.access must be restricted, and allowed_domains must be non-empty and valid.
Example: "skills": [{"type": "skill_reference", "skill_id": "skill_xxx", "version": "1.0"}]
Request example
curl -sS -X POST "$BASE_URL/agents/environments" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "OpenAI-Beta: agents=v1" \
-H "Content-Type: application/json" \
-d '{
"environment": {
"type": "openai_hosted",
"packages": {"python": ["pandas"]}
}
}'
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DASHSCOPE_API_KEY"],
base_url=os.environ["BASE_URL"],
)
environment = client.beta.agents.environments.create(
environment={
"type": "openai_hosted",
"packages": {"python": ["pandas"]},
},
)
print(environment.id, environment.status)
Response fields
idstringEnvironment ID
objectstringEnvironment object type, fixed as agent.environment
typestringEnvironment type, fixed as openai_hosted
statusstringEnvironment status: pending, ready, connected, disconnected, suspended, expired, or failed
filesarraySafe metadata for installed files; file content is not returned.
Properties
idstringEnvironment file ID
typestringFile reference type, fixed as file_id
pathstringFile path in the environment
size_bytesintegerFile size in bytes
file_idstringOriginal source file ID
skillsarraySafe metadata for installed skills; package content is not returned.
Properties
typestringSkill reference type, fixed as skill_reference
namestringSkill name
descriptionstringSkill description
skill_idstringSkill resource ID
versionstringFrozen version
pluginsarrayPlugin list; currently [] for managed environments; plugin fields in historical records do not mean plugins can be created
A 201 does not mean the environment is ready. Retrieve the status and confirm ready before use.
Response example
{
"id": "ccarenv_xxx",
"object": "agent.environment",
"type": "openai_hosted",
"status": "pending",
"files": [],
"skills": [],
"plugins": []
}
Error codes
| HTTP status | Error type / code | Solution |
|---|---|---|
400 | invalid_request_error / missing_required_parameter, param=environment | Provide environment in the request body |
400 | invalid_request_error / invalid_request_error | Fix type and the configuration as described for environment |
404 | not_found_error / not_found_error | Use a template or environment that exists and is accessible in the current workspace |
409 | conflict_error / conflict_error | Use the same request body with the same idempotency key |
503 | server_error / environment_unavailable or template_storage_unavailable | 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. Keep the HTTP status, x-request-id, and a redacted request body for troubleshooting.