# Export semantic entries

Export knowledge base definitions and semantic entries for review or migration.

```text
GET https://moi.matrixorigin.cn/newmoi/semantic-models/{model_id}/export
```

## Preparation before calling

First [query the knowledge base list](list-knowledge-bases.md) to obtain the knowledge base ID. 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`: Target workspace ID, passed through `X-Workspace-ID` Header.
- `$MODEL_ID`: The knowledge base ID of the definition to be exported.

## Request example

```bash
curl "https://moi.matrixorigin.cn/newmoi/semantic-models/$MODEL_ID/export" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID"
```

## Path parameters

| Parameters | Type | Description |
| --- | --- | --- |
| `model_id` | integer | The knowledge base ID of the definition to be exported. |

## Successful response

Returns `200` on success. Before migrating, export the current state and confirm that the import target has no semantic entries.

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "model": {
      "id": 401,
      "name": "product_docs",
      "source_counts": {
        "files": 2,
        "tables": 1,
        "total": 3
      }
    },
    "entries": [
      {
        "id": 501,
        "kind": "metric",
        "key": "total_rows",
        "tables": ["orders"],
        "spec": {
          "expr": "COUNT(*)"
        }
      }
    ]
  }
}
```

The response fields are as follows.

| Field | Type | Description |
| --- | --- | --- |
| `code` | string | `OK` on success. |
| `msg` | string | `OK` on success. |
| `data.model` | object | Knowledge base definition. |
| `data.model.id` | integer | Knowledge base ID. |
| `data.model.name` | string | Knowledge base name. |
| `data.model.source_counts` | object | Source quantity statistics. |
| `data.entries` | object[] | The semantic entry in the current knowledge base. |
| `data.entries[].id` | integer | Entry ID. |
| `data.entries[].kind` | string | Entry type. |
| `data.entries[].key` | string | Stable reference key. |
| `data.entries[].tables` | array[string] | Association table name; may be omitted if not set. |
| `data.entries[].spec` | object | Type-specific configuration object corresponding to `data.entries[].kind`; see [Create semantic entry](create-semantic-entry.md#spec-configuration) for fields. |

In field paths, `[]` means each item in an array. For example, `data.entries[].id` is the `id` field of each item in `data.entries`.

## Error response

```json
{
  "code": "ErrNotFound",
  "msg": "resource not found",
  "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`
  - `ErrParamInvalid`
  - `model_id` is invalid.
  - Use the knowledge base ID from the response.
* - `401`
  - `ErrUnauthorized`
  - The API Key is invalid or has expired.
  - Check API Key.
* - `403`
  - `ErrForbidden`
  - The caller does not have read permission.
  - Check workspace and object authorization.
* - `404`
  - `ErrNotFound`
  - The knowledge base does not exist or is not visible.
  - Reconfirm `model_id`.
* - `500`
  - `ErrServer`
  - The service failed to export the definition.
  - Keep the desensitized response information and try again.
```

## Follow-up operations

Before migrating or batch writing, use the export results to compare the target knowledge base without semantic entries, and then [Import semantic entries](import-semantic-entries.md).
