# 保存自定义事件

> 为用户画像记录一个自定义事件：即应用中发生的某些操作，例如完成用户引导。若要通过该事件启动邮件序列，请将其名称添加到[目的](/docs/mail-purposes#event-triggers-from-your-app)的进入规则中。若要通过该事件提前终止序列，请将其名称添加到目的的退出规则中。
>
> 请在发送事件之前添加名称。目的规则仅适用于添加名称后 Adapty Mail 接收到的事件。

## OpenAPI

```yaml
/api-specs/adapty-mail-api.yaml post /api/v1/profile/custom-event/save/
openapi: 3.1.0
info:
  title: Adapty Mail API
  version: 1.0.0
  description: |
    通过 Adapty Mail API，您可以[将用户画像、购买记录、订阅变更及自定义事件发送](/docs/mail-send-data-via-api)
    至 Adapty Mail，直接从您的服务器完成数据上报。

    使用本 API 可以：

    - 在 Adapty Mail 中尚无用户基础时添加订阅者。
    - 复用来自其他应用的订阅者基础。
    - 以您的后端作为数据源，通过服务器到服务器的方式向 Adapty Mail 提供数据。
    - 根据应用中的事件（例如完成用户引导）启动或停止邮件序列。

    每个流程的[目的](/docs/mail-purposes)决定了哪些事件会触发它。在每个项目默认包含的八个
    目的中，**从未购买**默认仅需要一个包含电子邮件的用户画像。其他标准营销目的则需要您上报购买记录和订阅变更。
servers:
  - url: https://api-mail.adapty.io
    description: 生产服务器
paths:
  /api/v1/profile/custom-event/save/:
    post:
      summary: 保存自定义事件
      description: |
        为用户画像记录一个自定义事件：即应用中发生的某些操作，例如完成用户引导。若要通过该事件启动邮件序列，请将其名称添加到[目的](/docs/mail-purposes#event-triggers-from-your-app)的进入规则中。若要通过该事件提前终止序列，请将其名称添加到目的的退出规则中。

        请在发送事件之前添加名称。目的规则仅适用于添加名称后 Adapty Mail 接收到的事件。
      operationId: saveCustomEvent
      security:
        - apikeyAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CustomEventDTO"
            examples:
              basic:
                summary: A user completed onboarding
                value:
                  event_type: onboarding_completed
                  event_id: evt_def456
                  event_datetime: "2026-06-12T18:45:00Z"
                  external_profile_id: user_12345
                  payload:
                    goal: Sleep better
                    steps_completed: "6"
      responses:
        "200":
          description: |
            自定义事件已接收。Adapty Mail 在响应后对事件应用目的规则，因此响应中不会显示序列是否已启动。响应体为空。
        "400":
          description: 验证失败——必填字段缺失或无效。`field_name` 指示具体字段。
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Errors"
              examples:
                default:
                  value:
                    errors:
                      - message: Field required
                        error_code: base_error
                        status_code: 400
                        field_name: event_type
        "403":
          description: 缺少或无效的私密 API 密钥。
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Errors"
              examples:
                default:
                  value:
                    errors:
                      - message: Secret key doesn't exist
                        error_code: secret_key_does_not_exist_error
                        status_code: 403
                        field_name: null
components:
  schemas:
    CustomEventDTO:
      type: object
      required:
        - event_type
        - event_id
        - event_datetime
        - external_profile_id
      properties:
        event_type:
          type: string
          pattern: ^[a-z][a-z0-9_]*$
          maxLength: 64
          description: |
            事件名称，例如 `onboarding_completed`。最多 64 个字符：小写字母、数字和下划线，且必须以字母开头。请使用与目的入口规则或退出规则中 **Custom** 下相同的名称。

            自定义事件的名称不能与 Adapty Mail 内部事件名称重复：`created`、`updated`、`email_confirmed`、`suppressed`、`deleted`，以及所有交易事件的 `event_type`，例如 `subscription_started`。
        event_id:
          type: string
          minLength: 1
          maxLength: 128
          description: |
            您为此事件设置的唯一标识符，最多 128 个字符。Adapty Mail 会忽略已存在的 `event_id`，因此您可以安全地重试请求。
        event_datetime:
          type: string
          format: date-time
          description: 事件发生的时间，采用 ISO 8601 格式，UTC 时区。
        external_profile_id:
          type: string
          description: |
            与您保存用户画像时使用的 `external_profile_id` 相同的稳定标识符。

            您可以在用户画像存在之前发送自定义事件。当您稍后保存具有相同标识符的用户画像时，Adapty Mail 会将该事件关联到该用户画像。
            届时，该用户画像可凭此事件满足某个目的的条件。
        customer_user_id:
          type: string
          description: 用户在您自有系统中的标识符。
        email:
          type: string
          format: email
          description: 用户的电子邮件地址。
        source:
          type: string
        payload:
          type: object
          maxProperties: 50
          propertyNames:
            pattern: ^[a-z][a-z0-9_]*$
          additionalProperties:
            type: string
            maxLength: 1000
          description: |
            用于在邮件中展示的数据。最多包含 50 个字段，每个值为最多 1,000 个字符的字符串。字段名称遵循与 `event_type` 相同的字符规则。请使用目的在 **Payload fields** 下列出的名称。

            每个字段通过其[自定义事件占位符](/docs/mail-email-data#custom-event-placeholders)出现在邮件中，例如 `{{ custom_event.goal }}`。
    Errors:
      type: object
      description: 标准错误响应。每次失败都会返回一个包含此结构的 4XX 状态。
      properties:
        errors:
          type: array
          items:
            type: object
            properties:
              message:
                type: string
                description: 人类可读的错误描述。
              error_code:
                type: string
                description: 机器可读的错误标识符。
              status_code:
                type: integer
                description: 此错误的 HTTP 状态码。
              field_name:
                type: string
                description: 导致错误的请求字段，如果错误与字段无关则为 `null`。
  securitySchemes:
    apikeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: |
        使用您的 Adapty Mail 密钥对每个请求进行身份验证，将其作为 **Authorization** 请求头发送，
        值的格式为 `Bearer {your_secret_api_key}`，例如 `Bearer secret_live_...`。

        在 Adapty Mail 的**设置**中找到此密钥。该密钥与项目绑定——它标识数据所属的项目，
        因此接口无需传入项目 ID。
```
