# Create a calculation instance

Create a compute instance in the current workspace.

```text
POST https://moi.matrixorigin.cn/newmoi/compute-resources
```

## Preparation before calling

First [list the computing specifications](../compute-resource-specs/list-compute-resource-specs.md) to obtain the available `spec_id`; when you need to specify the image, then [list the Worker image](list-worker-images.md)]. Prepare a personal access token and target workspace ID that has access to the target workspace.

The example below uses:

- `$AI_STUDIO_API_KEY`: The actual personal access token, passed through the `X-API-Key` Header.
- `$WORKSPACE_ID`: Workspace ID to create the compute instance, passed through the `X-Workspace-ID` Header.
- `$SPEC_ID`: The compute spec ID to use, taken from the response of [List compute specs](../compute-resource-specs/list-compute-resource-specs.md), specifying the instance spec in the request body.

## Request example

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/compute-resources" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "data-processing",
    "spec_id": "'"$SPEC_ID"'",
    "min_replicas": 0,
    "max_replicas": 1,
    "worker_images": [
      {
        "worker_type": "go-worker",
        "image_id": "$GO_WORKER_IMAGE_ID",
        "platform": "linux/amd64"
      },
      {
        "worker_type": "python-worker",
        "image_id": "$PYTHON_WORKER_IMAGE_ID",
        "platform": "linux/amd64"
      },
      {
        "worker_type": "java-worker",
        "image_id": "$JAVA_WORKER_IMAGE_ID",
        "platform": "linux/amd64"
      },
      {
        "worker_type": "custom-tool-worker",
        "image_id": "$CUSTOM_TOOL_WORKER_IMAGE_ID",
        "platform": "linux/amd64"
      }
    ]
  }'
```

## Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | Yes | Compute instance name. |
| `spec_id` | string | Yes | Calculated specification ID. |
| `kind` | string | No | Compatibility prompt; the instance type shall be subject to specifications. |
| `description` | string | No | Usage description. |
| `cpu` | integer | No | Compatible input; the actual capacity is determined by the corresponding specifications of `spec_id`. |
| `memory_gib` | integer | No | Compatible input; the actual capacity is determined by the corresponding specifications of `spec_id`. |
| `gpu` | integer | No | Compatible input; the actual capacity is determined by the corresponding specifications of `spec_id`. |
| `min_replicas` | integer | No | Serverless instances only accept `0`, or `0` when omitted. |
| `max_replicas` | integer | No | The upper limit of copies, must be greater than `0`; when omitted, it is `1`. |
| `auto_suspend_minutes` | integer | No | Automatic pause waiting time. |
| `go_worker_image_id` | string | No | Compatible Go Worker image ID. |
| `python_worker_image_id` | string | No | Compatible Python Worker image ID. |
| `platform` | string | No | Compatible operating platform. |
| `worker_images` | object[] | Yes | Worker image selection, must cover all currently active `worker_type` in the image list. |
| `worker_images[].worker_type` | string | Yes | Worker type, taken from `worker_type` in the mirror list. |
| `worker_images[].image_id` | string | Yes | Image ID, taken from `id` in the image list. |
| `worker_images[].platform` | string | Yes | Image running platform, taken from `platform` in the image list. |
| `worker_images[].kind` | string | No | Compatible alias; should be consistent with `worker_type` when passed in. |

In this document, `[]` after a type means an array; for example, `object[]` is an array of objects. In field paths, `[]` means each item in an array; for example, `worker_images[].worker_type` is the `worker_type` field of each item in `worker_images`.

`worker_type`, `image_id` and `platform` in `worker_images` should come from the mirror list. Each creation requires re-reading the mirror list and submitting one entry for each active Worker type; you cannot submit just one of them or rely on server-side auto-completion.

## Successful response

Returns `200` on success. Save `data.id`. The new Serverless instance is initially `IDLE`, and the Worker will not be deployed immediately; subsequent workload requirements will activate the required Worker type.

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "id": "resource-123",
    "name": "data-processing",
    "spec_id": "task-standard-small",
    "status": "IDLE",
    "status_message": "",
    "min_replicas": 0,
    "max_replicas": 1,
    "current_replicas": 0
  }
}
```

The response fields are as follows.

| Field | Type | Description |
| --- | --- | --- |
| `code` | string | `OK` on success. |
| `msg` | string | `OK` on success. |
| `data.id` | string | ID of the new computing instance. |
| `data.workspace_id` | string | ID of the workspace to which it belongs. |
| `data.name` | string | Compute instance name. |
| `data.description` | string | Example description; returned when there is a value. |
| `data.spec_id` | string | The compute specification ID to use. |
| `data.kind` | string | Instance type. |
| `data.cpu` | integer | CPU configuration value. |
| `data.memory_gib` | integer | Configuration value expressed in GiB of memory. |
| `data.gpu` | integer | GPU configuration value. |
| `data.cpu_milli` | integer | CPU configuration value in millicores. |
| `data.memory_mib` | integer | Configuration value expressed in MiB of memory. |
| `data.gpu_count` | integer | Number of GPUs. |
| `data.gpu_memory_mib` | integer | MiB of video memory for a single GPU. |
| `data.gpu_cores` | integer | Number of GPU cores. |
| `data.min_replicas` | integer | Serverless instances are fixed to `0`. |
| `data.max_replicas` | integer | Maximum number of copies. |
| `data.desired_replicas` | integer | Number of target copies. |
| `data.current_replicas` | integer | The current number of copies. |
| `data.go_worker_image_id` | string | Go Worker image ID; returned if there is a value. |
| `data.python_worker_image_id` | string | Python Worker image ID; returned if there is a value. |
| `data.worker_images` | object[] | Worker image selection; returned when there is a value. |
| `data.worker_images[].worker_type` | string | Worker type. |
| `data.worker_images[].image_id` | string | Image ID. |
| `data.worker_images[].platform` | string | Running platform. |
| `data.platform` | string | Instance running platform; returned if there is a value. |
| `data.auto_suspend_minutes` | integer | Automatic pause waiting time. |
| `data.is_default` | boolean | Whether it is the default computing instance of the workspace. |
| `data.status` | string | Current life cycle status. |
| `data.status_message` | string | Supplementary status information; returned if there is a value. |
| `data.scale_reason` | string | The scaling reason provided by the server; returned if there is a value. |
| `data.last_activation_at` | string | Last activation time; returned if there is a value. |
| `data.last_active_at` | string | Last active time; returned if there is a value. |
| `data.created_by` | string | Creator ID; returned if there is a value. |
| `data.created_at` | string | Creation time; returned if there is a value. |
| `data.updated_at` | string | Last updated time; returned if there is a value. |

## Error response

```json
{
  "code": "INVALID_PARAMS",
  "msg": "请求参数无效",
  "data": null
}
```

### Common HTTP errors

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP status code
  - error code
  - Common causes
  - Recommended actions
* - `400`
  - `INVALID_PARAMS`
  - The request body cannot be parsed, or the required `name` and `spec_id` are missing.
  - Check request fields and calculation specifications.
* - `401`
  - —
  - Missing or invalid access credentials.
  - Check API Key and Workspace Header.
* - `403`
  - `ErrForbidden`
  - The current identity does not have permission to create instances.
  - Use valid credentials, or contact your administrator for authorization.
* - `409`
  - `COMPUTE_RESOURCE_NAME_EXISTS`
  - An instance with the same name already exists in the workspace.
  - Replace `name` and try again.
* - `500`
  - `CREATE_FAILED`
  - The server failed to create an instance.
  - Record the request time and error message and try again.
```

## Follow-up operations

[Query calculation instance details](get-compute-resource.md) until the status meets the task requirements.
