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

# 创建记录

### 路径

POST /api/table/\{tableId}/record

### 请求

#### 路径参数

* tableId (string): 表的唯一标识符。

#### 请求体

* **records（必填）**
  * 描述：要创建的记录数组
  * 类型：数组
  * 示例：
    ```json theme={null}
    [
      {
        "fields": {
          "Name": "John Doe",
          "Age": 30,
          "Email": "john@example.com"
        }
      },
      {
        "fields": {
          "Name": "Jane Smith",
          "Age": 28,
          "Email": "jane@example.com"
        }
      }
    ]
    ```
  * 说明：每个记录都是一个包含 `fields` 对象的项。`fields` 对象包含字段名和对应的值，每一种字段值结构都不一样，详情请[查看记录字段值类型](/zh/api-doc/record/interface)。
* **fieldKeyType（可选）**
  * 描述：指定字段键的类型
  * 类型：字符串
  * 可选值：
    * "name"：使用字段名作为键
    * "id"：使用字段 ID 作为键
    * "dbFieldName"：使用字段 dbFieldName 作为键
  * 示例：`"name"` 或 `"id"` 或 `"dbFieldName"`
  * 说明：如果不指定，默认使用字段名作为键。
  * 用法：
    * 当设置为 "name" 时：
      ```json theme={null}
      {
        "fields": {
          "Name": "John Doe",
          "Age": 30
        }
      }
      ```
    * 当设置为 "id" 时：
      ```json theme={null}
      {
        "fields": {
          "fldABCDEFGHIJKLMN": "John Doe",
          "fldOPQRSTUVWXYZ12": 30
        }
      }
      ```
* **typecast（可选）**
  * 描述：是否自动转换字段值类型，默认会严格校验，需要保证输入的值符合当前字段的数据类型，如果开启则会尝试自动进行转换。
  * 类型：布尔值
  * 可选值：true 或 false
  * 示例：`true`
  * 说明：如果设置为 true，系统会尝试将输入值转换为正确的字段值类型。
  * 用法示例：
    * 关联字段：可以直接使用主键文本进行关联
      ```json theme={null}
      {
        "User table": "张三"
      }
      ```
    * 日期字段：可以使用非标准格式的日期字符串
      ```json theme={null}
      {
        "Date": "2023-05-15"
      }
      ```
    * 用户字段：可以直接使用用户名
      ```json theme={null}
      {
        "Assigned To": "John Doe"
      }
      ```
* **order（可选）**
  * 描述：指定新记录的在指定视图所在的位置
  * 类型：对象
  * 包含以下属性：
    * viewId
      * 描述：视图的 ID [（如何获取）](/zh/api-doc/get-id#viewid)
      * 类型：字符串
      * 示例：`"viwABCDEFGHIJKLMN"`
    * anchorId
      * 描述：锚点记录的 ID[（如何获取）](/zh/api-doc/get-id#recordid)
      * 类型：字符串
      * 示例：`"rec123456789ABCDE"`
    * position
      * 描述：相对于锚点记录的位置
      * 类型：字符串
      * 可选值：
        * "before"：在锚点记录之前
        * "after"：在锚点记录之后
      * 示例：`"after"`
  * 完整示例：
    ```json theme={null}
    {
      "viewId": "viwABCDEFGHIJKLMN",
      "anchorId": "rec123456789ABCDE",
      "position": "after"
    }
    ```
  * 说明：使用 order 可以精确控制新记录在特定视图中的位置。

### 响应

#### 成功响应

* 状态码: 201 Created
* 响应体: 返回创建的记录数据。

**示例响应体**

```json theme={null}
{
  "records": [
    {
      "id": "record789",
      "fields": {
        "single line text": "text value 1"
      }
    },
    {
      "id": "record567",
      "fields": {
        "single line text": "text value 2"
      }
    }
  ]
}
```

### 错误响应

* 状态码: 400 Bad Request: 请求体格式错误或缺少必需字段。
* 状态码: 404 Not Found: 指定的表 tableId 不存在。

### 示例代码

<CodeGroup>
  ```bash CURL theme={null}
  curl -X POST 'https://app.teable.cn/api/table/__tableId__/record' \
    -H 'Authorization: Bearer __token__' \
    -H 'Content-Type: application/json' \
    -d '{
      "records": [
        {
          "fields": {
            "Name": "John Doe",
            "Age": 30
          }
        }
      ]
    }'
  ```

  ```js JS SDK theme={null}
  import { configApi, createRecords } from '@teable/openapi';

  configApi({
    endpoint: 'https://app.teable.cn',
    token,
  });

  const response = await createRecords('__tableId__', {
    records: [
      {
        fields: {
          Name: 'John Doe',
          Age: 30
        }
      }
    ]
  });

  console.log(response.data);
  ```

  ```ts TypeScript theme={null}
  const response = await fetch('https://app.teable.cn/api/table/__tableId__/record', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer __token__',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      records: [
        {
          fields: {
            Name: 'John Doe',
            Age: 30
          }
        }
      ]
    })
  });

  console.log(await response.json());
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://app.teable.cn/api/table/__tableId__/record',
      headers={
          'Authorization': 'Bearer __token__',
          'Content-Type': 'application/json'
      },
      json={
          'records': [
              {
                  'fields': {
                      'Name': 'John Doe',
                      'Age': 30
                  }
              }
          ]
      }
  )

  print(response.json())
  ```
</CodeGroup>
