Skip to main content

Create environment

Create a managed runtime environment with a container, dependencies, network policy, initial files, and skills.

Endpoint

POST /agents/environments 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.

Request body

FieldRequiredTypeDescription
environmentYesobjectEnvironment configuration, see below
vault_idsNoarray<string> / nullVaults to attach, up to 10
environment.typeYesstringEnvironment type, fixed as openai_hosted
environment.environment_template_idNostringEnvironment template ID; the template must belong to the current workspace. Manage templates through Environment templates.
environment.envNoobjectEnvironment 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
environment.packagesNoobjectPackages grouped by npm, python, and system
environment.networkNoobjectNetwork policy, see below
environment.filesNoarrayInitial files; currently uses file_id to reference source files
environment.desktopNoobjectDesktop settings; only enabled=false is accepted, and enabled=true returns 400
environment.setup_commandsNoarraySetup commands executed in order. Each item requires command and accepts an optional cwd; cwd defaults to /workspace
environment.skillsNoarraySkills; currently uses skill_reference, up to 32 items
environment.capability_directoriesNoarrayCapability discovery directories; must be under /workspace/
environment.network.accessNostringNetwork access mode, enabled, disabled, or restricted
environment.network.allowed_domainsNoarray<string>Allowed domains, required and non-empty when access=restricted; host names only, with no scheme, port, or wildcard
environment.files[].typeYesstringFile reference type, fixed as file_id
environment.files[].pathYesstringAbsolute path in the environment; must be under /workspace/
environment.files[].file_idYesstringSource file ID returned by POST /agents/file
environment.skills[].typeYesstringSkill reference type, fixed as skill_reference
environment.skills[].skill_idYesstringSkill resource ID
environment.skills[].versionNostringSkill version; if omitted, latest is resolved and frozen to a specific version
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"]}
    }
  }'

Response example

{
  "id": "ccarenv_xxx",
  "object": "agent.environment",
  "type": "openai_hosted",
  "status": "pending",
  "files": [],
  "skills": [],
  "plugins": []
}

Response fields

FieldTypeDescription
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.
files[].idstringEnvironment file ID
files[].typestringFile reference type, fixed as file_id
files[].pathstringFile path in the environment
files[].size_bytesintegerFile size in bytes
files[].file_idstringOriginal source file ID
skillsarraySafe metadata for installed skills; package content is not returned.
skills[].typestringSkill reference type, fixed as skill_reference
skills[].namestringSkill name
skills[].descriptionstringSkill description
skills[].skill_idstringSkill resource ID
skills[].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.

Error codes

HTTP statusError type / codeSolution
400invalid_request_error / missing_required_parameter, param=environmentProvide environment in the request body
400invalid_request_error / invalid_request_errorFix type and the configuration as described for environment
404not_found_error / not_found_errorUse a template or environment that exists and is accessible in the current workspace
409conflict_error / conflict_errorUse the same request body with the same idempotency key
503server_error / environment_unavailable or template_storage_unavailableRetry 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.