> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qoder.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Session の作成

> Agent と Environment にバインドされた新しい Session を作成します。

`POST /api/v1/cloud/sessions`

Session を作成します。新しい Session は `idle` 状態で開始します。イベントを送信すると処理が始まります。

## ヘッダー

| ヘッダー            | 必須 | 説明                  |
| --------------- | -- | ------------------- |
| `Authorization` | はい | `Bearer $QODER_PAT` |
| `Content-Type`  | はい | `application/json`  |

## リクエストボディ

| フィールド                           | 型                 | 必須  | 説明                                                                                                                                                                                                  |
| ------------------------------- | ----------------- | --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `agent`                         | string または object | はい  | Agent ID、または `id`、必須の `type: "agent"`、任意の `version` を含むオブジェクト。[Agent reference](/ja/cloud-agents/api/sessions/schemas#agent-reference) を参照                                                          |
| `environment_id`                | string            | はい  | `env_` プレフィックスを持つ既存の Environment ID                                                                                                                                                                 |
| `title`                         | string \| null    | いいえ | Session タイトル                                                                                                                                                                                        |
| `metadata`                      | object            | いいえ | Session メタデータ                                                                                                                                                                                       |
| `environment_variables`         | string            | いいえ | agent ランタイムに注入する Session レベルの環境変数。`KEY=VALUE` ペアを `;`(または改行、全角 `;` も受け付け)で区切った単一文字列で指定します。レスポンスのフィールドは JSON オブジェクトです — [検証](#environment-variables-validation) を参照。self-hosted 環境では非対応              |
| `incremental_streaming_enabled` | boolean           | いいえ | この Session の増分ストリーミングイベントを有効にします。デフォルト: `false`。`true` の場合、list および stream のイベント API は、最終的な完全イベントに加えて `agent.message_start`、`agent.content_block_delta`、`agent.message_stop` などの増分 agent イベントを公開します |
| `resources`                     | array             | いいえ | 作成時にアタッチする file、GitHub リポジトリ、または Memory Store リソース                                                                                                                                                  |
| `vault_ids`                     | array             | いいえ | Session が利用できる Vault ID                                                                                                                                                                             |

レガシーリクエストフィールド `environment`、`delta_flush_interval_ms`、`vaults`、`memory_store_ids` はサポートされません。Memory Store をアタッチするには `resources[]` で `type: "memory_store"` を使用してください。

### environment variables の検証

名前は `[A-Za-z_][A-Za-z0-9_]*` に一致する必要があります。予約名 `SERVER_ENDPOINT`、`USER_ID`、`WORK_DIR`、および `CAW_` または `QODER_` プレフィックスを持つ名前は拒否されます。各値は 8 KiB を超えられません。フィールド全体で 64 エントリまたは合計 64 KiB を超えられません。重複キー、不正な形式のエントリ、または self-hosted 環境での使用は 400 `invalid_request_error` を返します。

## リソースパラメータ

| リソース種別              | 必須フィールド                              | 任意フィールド                  |
| ------------------- | ------------------------------------ | ------------------------ |
| `file`              | `type`, `file_id`                    | `mount_path`             |
| `github_repository` | `type`, `url`, `authorization_token` | `mount_path`, `checkout` |
| `memory_store`      | `type`, `memory_store_id`            | `access`, `instructions` |

self-hosted 環境は現在 Session resources をサポートしていません。

## リクエスト例

```bash theme={null}
curl -X POST https://api.qoder.com/api/v1/cloud/sessions \
  -H "Authorization: Bearer $QODER_PAT" \
  -H "Content-Type: application/json" \
  -d '{
    "agent": {"id": "agent_019e390add9f7bac9b6cc806db46fcbd", "type": "agent", "version": 2},
    "environment_id": "env_019e2590d33f711fabf42f2857cecd8a",
    "title": "Code review session",
    "metadata": {"purpose": "review"},
    "environment_variables": "FEATURE_FLAG=on;LOG_LEVEL=debug",
    "incremental_streaming_enabled": true,
    "resources": [
      {
        "type": "github_repository",
        "url": "https://github.com/your-org/your-repo",
        "mount_path": "/data/workspace/your-repo",
        "authorization_token": "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      },
      {
        "type": "memory_store",
        "memory_store_id": "memstore_019eed05b61e78cea61bfd366e072878",
        "access": "read_write",
        "instructions": "Use this memory for long-lived project context."
      }
    ],
    "vault_ids": ["vault_019eed0519807975983d0ffc4b4b4c79"]
  }'
```

## レスポンス例

**HTTP 200 OK**

[Session オブジェクト](/ja/cloud-agents/api/sessions/schemas#session-object)を返します。

```json theme={null}
{
  "id": "sess_019e3bb1e8c171fd9abbb1477ffb84cc",
  "type": "session",
  "agent": {
    "id": "agent_019e390add9f7bac9b6cc806db46fcbd",
    "type": "agent",
    "name": "doc-test-agent",
    "description": "Documentation example Agent",
    "model": {
      "id": "ultimate",
      "effective_context_window": 200000
    },
    "system": "You are a test assistant.",
    "tools": [],
    "skills": [],
    "mcp_servers": [],
    "version": 2
  },
  "environment_id": "env_019e2590d33f711fabf42f2857cecd8a",
  "title": "Code review session",
  "status": "idle",
  "metadata": {"purpose": "review"},
  "environment_variables": {"FEATURE_FLAG": "on", "LOG_LEVEL": "debug"},
  "incremental_streaming_enabled": true,
  "resources": [],
  "vault_ids": ["vault_019eed0519807975983d0ffc4b4b4c79"],
  "deployment_id": null,
  "outcome_evaluations": [],
  "stats": {"active_seconds": 0, "duration_seconds": 0},
  "archived_at": null,
  "created_at": "2026-05-18T15:26:15.747298Z",
  "updated_at": "2026-05-18T15:26:15.747298Z"
}
```

## エラー

| HTTP | Type                    | 発生条件                                                 |
| ---- | ----------------------- | ---------------------------------------------------- |
| 400  | `invalid_request_error` | リクエストの形式エラー、必須フィールドの欠落、サポートされないレガシーフィールド、または無効なリソース  |
| 401  | `authentication_error`  | PAT が無効または期限切れ                                       |
| 404  | `not_found_error`       | Agent、Environment、file、Memory Store、または Vault が存在しない |
| 409  | `invalid_request_error` | 参照したファイルが ready でない、またはリソースのパス/ID が競合                |

**HTTP 400 Bad Request**

```json theme={null}
{
  "type": "error",
  "request_id": "cb80235f-76a2-4ff3-9e28-5aa2da12dc14",
  "error": {
    "type": "invalid_request_error",
    "message": "Field 'agent' is required."
  }
}
```

**HTTP 404 Not Found**

```json theme={null}
{
  "type": "error",
  "request_id": "cb80235f-76a2-4ff3-9e28-5aa2da12dc14",
  "error": {
    "type": "not_found_error",
    "message": "Agent 'agent_does_not_exist_0000000000000000000000000000' was not found."
  }
}
```

注意: `agent_` プレフィックスを持たない `agent` 値(または `env_` プレフィックスを持たない `environment_id`)も、400 ではなく 404 `not_found_error` として解決されます。

完全なエラーエンベロープについては [Errors](/ja/cloud-agents/api/conventions/errors) を参照してください。

## 関連項目

<CardGroup cols={2}>
  <Card title="Session の起動" icon="play" href="/ja/cloud-agents/sessions">
    Agent を環境で実行し、ステートフルな対話を行う。
  </Card>
</CardGroup>
