Skip to main content

Quickstart

Get the full Managed Agents workflow running in five minutes: create an Agent and Environment, open a Session, submit a task, and stream the results.

Prerequisites

  1. You have activated Model Studio and created an API Key (sk-xxx), and obtained your workspace ID (ws_xxxxxxxxxxxx). For details, see Overview and authentication.
  2. Export the API Key and Endpoint as environment variables. The following examples reference them directly:
export DASHSCOPE_API_KEY="sk-xxx"
export AGENTSTUDIO_URL="https://ws_xxxxxxxxxxxx.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio"

Workflow

A complete task execution consists of five steps:
  1. Create an Agent: Define the model and system prompt to obtain an agent_xxx. An Agent is typically created once and reused.
  2. Create an Environment: Define the runtime environment to obtain an env_xxx. An Environment is typically created once and reused.
  3. Create a Session: Bind an Agent to an Environment to obtain a sesn_xxx. Create a new Session for each task.
  4. Send an Event: Submit a task instruction to the Session, which triggers the Agent to enter the running state.
  5. Subscribe to SSE: Receive execution results via an event stream until the Session returns to idle.

Install the SDK

Before using the Python or Java SDK, install the corresponding DashScope package. The Managed Agents module requires Python SDK v1.26.2 or later, and Java SDK v2.22.24 or later. If you have an older version installed, re-run the install command to upgrade.
pip install dashscope

End-to-end example

The following example creates a code-generation Agent, opens a Session, submits a coding task, and streams the execution results.
## 1. Create an Agent
AGENT_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/agents" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "quickstart-coder",
    "model": {"id": "qwen-plus"},
    "system": "You are a Python expert. When the user submits a coding task, output concise, runnable Python code directly."
  }' | jq -r '.id')
echo "Agent: $AGENT_ID"

## 2. Create an Environment
ENV_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/environments" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "quickstart-env", "config": {"type": "cloud"}}' | jq -r '.id')
echo "Environment: $ENV_ID"

## 3. Create a Session
SESSION_ID=$(curl -s -X POST "$AGENTSTUDIO_URL/sessions" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"agent\": \"$AGENT_ID\", \"environment_id\": \"$ENV_ID\"}" | jq -r '.id')
echo "Session: $SESSION_ID"

## 4. Submit a task
curl -X POST "$AGENTSTUDIO_URL/sessions/$SESSION_ID/events" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [
      {"role": "user", "type": "message",
       "content": [{"type": "text", "text": "Write a Python function that takes a list of integers and returns a sorted list with duplicates removed."}]}
    ]
  }'

## 5. Subscribe to the SSE event stream to receive results
curl -N "$AGENTSTUDIO_URL/sessions/$SESSION_ID/events/stream" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Accept: text/event-stream"
Managed Agent API
Flow Agent API
Sandbox API
RAG API
Long-term Memory
ParseX API
Security API
Framework Integration