# 更新客户资料与标签

Operation / SDK: `updateCustomer` · `update_customer` · `UpdateCustomer`

以保留的 requestId 部分更新资料、属性和标签；重放返回原结果。

更新于 2026-11-06

[网页版本](https://www.longplayai.com/zh-hans/docs/api/update-customer)

<a id="request"></a>

## 请求

- **凭据**: Server Key，权限 `customers:write`。只在后端调用。
- **SDK**: JavaScript `createLongplayServer().updateCustomer` · Python `update_customer` · Go `UpdateCustomer`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/customers/update" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "lookup": {
    "type": "id",
    "value": "8b258f8b-4228-40fa-8d24-5b16428e686d"
  },
  "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
  "patch": {
    "name": "Alex",
    "attributes": {
      "source": "website"
    }
  }
}'
```

**TypeScript: updateCustomer.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function updateCustomer() {
  return longplay.updateCustomer({
    lookup: {
      type: "id",
      value: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    },
    requestId: "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
    patch: {
      name: "Alex",
      attributes: {
        source: "website",
      },
    },
  });
}
```

**请求体: request.json**

```json
{
  "lookup": {
    "type": "id",
    "value": "8b258f8b-4228-40fa-8d24-5b16428e686d"
  },
  "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
  "patch": {
    "name": "Alex",
    "attributes": {
      "source": "website"
    }
  }
}
```

**响应示例: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "customer": {
    "id": "8b258f8b-4228-40fa-8d24-5b16428e686d",
    "externalUserId": "user_42",
    "name": "Alex Chen",
    "email": "alex@example.com",
    "attributes": {},
    "createdAt": "2026-10-03T12:00:00.000Z"
  },
  "changed": true,
  "replayed": true
}
```

<a id="parameters"></a>

## 请求体参数

**application/json**

- `lookup` (object | object, required): 用 type 指定 id 或 externalUserId，再用 value 提供对应值。
- `lookup.type` ("id" | "externalUserId", required): `value` 的匹配方式：`id` 按 Customer 的 UUID 查找，`externalUserId` 按你系统中的用户 ID 查找。
- `lookup.value` (uuid | string, required): `type` 为 `id` 时传 Customer 的 UUID；为 `externalUserId` 时传你系统中的用户 ID（1–255 个字符，首尾空白会被去除）。
- `requestId` (uuid, required): 你的业务请求 UUID；同一次操作重试复用，新的操作生成新值。
- `patch` (object, required): 只提交需要修改的资料字段。attributes 保存 JSON 属性；removeAttributes 删除键，tagChanges 增减标签。
- `patch.name` (string, optional): 展示名称，不作为唯一标识。 maxLength: 80
- `patch.email` (email | "", optional): 客户联系邮箱；不用于自动合并客户身份。
- `patch.phone` (string | null, optional): Customer 的 E.164 联系电话；null 清空号码，填写号码不表示客户同意接收消息。
- `patch.attributes` (object, optional): 最多 128 个键，每键 1–128 字符，总 JSON ≤64 KiB；值可为嵌套 JSON，单值最多 20 层和 10,000 个节点。禁止保留字段；null 是属性值。标签保存在 attributes.tags；更新 Customer 时用 removeAttributes 删除键、用 tagChanges 修改标签。
- `patch.removeAttributes` (array, optional): 显式删除属性键；最多 128 个不含 tags 的键，键名须以字母开头并只含字母、数字和下划线。 maxItems: 128
- `patch.tagChanges` (object, optional): 使用 add 与 remove 数组增减标签；不能通过 patch.attributes 直接覆盖 tags。
- `patch.tagChanges.add` (array, required): 要添加的项。 maxItems: 20
- `patch.tagChanges.remove` (array, required): 要移除的项。 maxItems: 20

<a id="response"></a>

## 响应字段

成功响应都包含 `requestId`（本次请求 ID，排查时提供）和 `scope`（App 与环境）。

**HTTP 200 · application/json**

- `requestId` (uuid, required): 服务端为这次 HTTP 请求生成的 UUID，排查时提供；它不是你的业务 requestId。
- `scope` (object, required): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `scope.projectId` (string, required): 凭据绑定的 App ID。 minLength: 1
- `scope.environment` (enum, required): 独立数据范围：sandbox 或 live。 Values: `"sandbox"`, `"live"`.
- `customer` (object, required): 客户资料对象。
- `customer.id` (uuid, required): Customer 的 ID。
- `customer.externalUserId` (string | null, required): 你服务端登录系统的稳定用户 ID；不是邮箱。按邮箱预建、尚未认领的客户为 null。
- `customer.name` (string, required): 展示名称，不作为唯一标识。 maxLength: 80
- `customer.email` (email | "", required): 客户联系邮箱；不用于自动合并客户身份。
- `customer.phone` (string | null, optional): Customer 的 E.164 联系电话；null 清空号码，填写号码不表示客户同意接收消息。
- `customer.attributes` (object, required): 最多 128 个键，每键 1–128 字符，总 JSON ≤64 KiB；值可为嵌套 JSON，单值最多 20 层和 10,000 个节点。禁止保留字段；null 是属性值。标签保存在 attributes.tags；更新 Customer 时用 removeAttributes 删除键、用 tagChanges 修改标签。
- `customer.createdAt` (date-time, required): 记录创建时的 ISO 8601 UTC 时间。
- `changed` (boolean, required): 这次更新是否修改了数据。
- `replayed` (boolean, required): 是否为相同 `requestId` 的重放结果。

<a id="errors"></a>

## 错误

失败时返回错误对象，按 `error.code` 与 `error.recovery` 处理。下表是这个接口的凭据、作用域和幂等字段决定的错误；视请求内容还可能返回 `NOT_FOUND`（引用的资源不存在）、`NOT_CONFIGURED`（所需的集成未连接）或 `PAYLOAD_TOO_LARGE`。有些拒绝（如余额不足）不是错误，而是成功响应里的 `reason`。

**可能的错误**

| code | HTTP | 何时 | recovery |
| --- | --- | --- | --- |
| `INVALID_REQUEST` | 400 | 请求不符合本页的参数。缺少或无法解析协议头时分别返回 `UNSUPPORTED_PROTOCOL_VERSION` 或 `INVALID_PROTOCOL_VERSION`。 | `fix_request` |
| `UNAUTHENTICATED` | 401 | 凭据缺失、无效、过期或已撤销。 | `authenticate` |
| `FORBIDDEN` | 403 | 凭据没有 `customers:write` 权限。 | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` 或 `X-Indiana-Environment` 与凭据所属的 App 和环境不一致。 | `check_scope` |
| `CONFLICT` | 409 | 同一个 `requestId` 用了不同的输入（`reason: IDEMPOTENCY_CONFLICT`）。 | `resolve_conflict` |
| `RATE_LIMITED` | 429 | 请求过多；按 `Retry-After` 等待后用同一请求重试。 | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | 服务暂时不可用；用同一请求重试。 | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | 结果未知；先读取原操作，或用原 ID 和原输入重放。 | `recover_operation` |

- [错误码与恢复](https://www.longplayai.com/zh-hans/docs/api/errors.md)
- [请求约定](https://www.longplayai.com/zh-hans/docs/api.md)
- [全部接口](https://www.longplayai.com/zh-hans/docs/api/operations.md#identity)
