openapi: 3.1.0
info:
  title: Entayo CRM API
  version: "1.0"
  description: |
    The REST API behind Entayo CRM. Everything you can do on the CRM's screens
    with your contacts, companies, lists, activity, tasks, pipelines and
    campaigns, you can do here with an **API key**.

    - **Base URL:** `https://<region>.zocton.com/entayo-crm/api` — your data
      centre (`apac`, `eu` or `na`). Pick it in the server box of any
      endpoint. Never use your workspace address (`<you>.zocton.com`) for API
      calls.
    - **Authentication:** `Authorization: Bearer entayo_…` — see
      [Authentication](/docs/authentication).
    - **Project:** add `X-Entayo-Project: <project id>` to work in a project
      other than Default — see [Projects](/docs/projects).
    - **Bodies** are JSON (`Content-Type: application/json`) unless an
      endpoint says otherwise. Ids are positive integers.
servers:
  - url: https://{region}.zocton.com/entayo-crm/api
    description: "Your data centre: apac = Asia Pacific, eu = Europe, na = North America"
    variables:
      region:
        default: apac
        enum: [apac, eu, na]
security:
  - apiKey: []
tags:
  - name: Account
    description: Who the key belongs to, and its projects.
  - name: People & Companies
    description: |
      Master data — every person and company in the project. `kind` is
      `people` or `companies`. A row has two ids: `id` (the **master id**, used
      by lists and campaigns) and `entity_id` (the **record id**, used by every
      `/entities/{id}` endpoint, activity and tasks).
  - name: Records
    description: One record by its record id (`entity_id`) — read, change, delete, move through the pipeline.
  - name: Bulk record actions
    description: Change many records in one call.
  - name: Record types & fields
    description: The record types of the project (People, Companies, custom types, and lists) and their fields.
  - name: Lists
    description: People and company lists (and segments — a list with a `parent_id`).
  - name: Activity
    description: The activity log — emails, calls, meetings, notes … on a record.
  - name: Activity types
  - name: Tasks
  - name: Pipeline stages
    description: The kanban stages of a record type.
  - name: Campaigns
    description: Email campaigns sent to people.
  - name: Files
  - name: Team
    description: Assigning contacts to people on your Zocton team.
  - name: Settings
  - name: Utilities
  - name: API keys
paths:
  # ---------------------------------------------------------------- Account
  /auth/me:
    get:
      tags: [Account]
      summary: Who am I
      description: The person the API key belongs to.
      operationId: getMe
      responses:
        "200":
          description: The key's owner.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: integer }
                  username: { type: string }
                  email: { type: [string, "null"] }
                  name: { type: [string, "null"] }
                  role: { type: string, enum: [admin, user] }
                  team: { type: ["object", "null"] }
                  impersonation: { type: ["object", "null"], description: Always null for an API key. }
              example: { id: 42, username: ada, email: ada@example.com, name: Ada Lovelace, role: admin, team: null, impersonation: null }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /projects:
    get:
      tags: [Account]
      summary: List projects
      description: |
        Every project of the account, in the Zocton Dashboard's order. Use a
        project's `id` in the `X-Entayo-Project` header.
      operationId: listProjects
      responses:
        "200":
          description: The projects and the active one.
          content:
            application/json:
              schema:
                type: object
                properties:
                  projects:
                    type: array
                    items: { $ref: "#/components/schemas/Project" }
                  active_id: { type: integer }
              example:
                projects:
                  - { id: 1, name: Default, is_default: true, color: null, logo_url: null, emoji: null }
                  - { id: 7, name: Germany, is_default: false, color: "#c15f3c", logo_url: null, emoji: "🇩🇪" }
                active_id: 1
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Account]
      summary: Create a project
      description: Projects are normally added on the Zocton Dashboard. This creates one in the CRM only.
      operationId: createProject
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string, description: "Required, has a letter, no special characters, not a duplicate." }
            example: { name: Germany }
      responses:
        "201": { description: The new project. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /projects/{id}:
    patch:
      tags: [Account]
      summary: Rename a project
      operationId: renameProject
      parameters: [{ $ref: "#/components/parameters/Id" }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [name], properties: { name: { type: string } } }
            example: { name: Germany (DACH) }
      responses:
        "200": { description: The renamed project. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # ---------------------------------------------------- People & Companies
  /master/{kind}:
    get:
      tags: [People & Companies]
      summary: Search people or companies
      description: |
        Paged rows of every person or company in the project, with their columns.
        Filter with `search`, `list_id`, `f_<column>=<value>` (any column, exact
        match, e.g. `f_country=Germany`) or the encoded `filter` (see
        [Filters](/docs/filters)).
      operationId: listMaster
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
        - { name: search, in: query, schema: { type: string }, description: Free text over the row. }
        - { name: list_id, in: query, schema: { type: integer }, description: Only members of this list. }
        - { name: membership, in: query, schema: { type: string, enum: [any, in_list, not_in_list], default: any } }
        - { name: sort, in: query, schema: { type: string, default: created_at }, description: "A column slug, or `created_at`." }
        - $ref: "#/components/parameters/Order"
        - { name: page, in: query, schema: { type: integer, minimum: 1, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 1000, default: 200 } }
        - $ref: "#/components/parameters/Filter"
        - $ref: "#/components/parameters/Assigned"
      responses:
        "200":
          description: One page of rows.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: "#/components/schemas/MasterRow" } }
                  columns: { type: array, items: { $ref: "#/components/schemas/Column" } }
                  kind: { type: string, enum: [person, company] }
                  total: { type: integer }
                  page: { type: integer }
                  limit: { type: integer }
                  total_pages: { type: integer }
              example:
                data:
                  - { id: 311, entity_id: 9120, first_name: Ada, last_name: Lovelace, email: ada@example.com, company_name: Analytical Engines, country: United Kingdom, created_at: "2026-10-30T09:12:44.000Z" }
                columns:
                  - { slug: first_name, name: First Name, field_type: text }
                kind: person
                total: 1
                page: 1
                limit: 200
                total_pages: 1
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [People & Companies]
      summary: Create a person or company
      description: |
        `values` maps column slugs (from [List columns](#tag/people--companies/GET/master/{kind}/fields))
        to values. At least one value is required. The answer carries both ids.
      operationId: createMaster
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [values]
              properties:
                values: { type: object, additionalProperties: true, description: "column slug → value" }
            example: { values: { first_name: Ada, last_name: Lovelace, email: ada@example.com, city: London } }
      responses:
        "201":
          description: Created.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/MasterRow" }
              example: { id: 312, entity_id: 9121, first_name: Ada, last_name: Lovelace, email: ada@example.com, success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/fields:
    get:
      tags: [People & Companies]
      summary: List columns
      description: Every column of people or companies — the slugs to use in `values`, `field_values`, `f_…` and filters.
      operationId: listMasterFields
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      responses:
        "200":
          description: The columns.
          content:
            application/json:
              schema:
                type: object
                properties:
                  kind: { type: string }
                  columns: { type: array, items: { $ref: "#/components/schemas/Column" } }
              example:
                kind: person
                columns:
                  - { slug: first_name, name: First Name, field_type: text }
                  - { slug: email, name: Email, field_type: email }
                  - { slug: country, name: Country, field_type: text }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [People & Companies]
      summary: Add custom columns
      operationId: createMasterFields
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [fields]
              properties:
                fields:
                  type: array
                  items:
                    type: object
                    required: [name]
                    properties:
                      name: { type: string }
                      field_type: { $ref: "#/components/schemas/FieldType" }
            example: { fields: [{ name: Lead source, field_type: text }] }
      responses:
        "200": { description: "`{ kind, created }` — the columns made." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/ids:
    get:
      tags: [People & Companies]
      summary: All matching master ids
      description: The master ids of every row matching the same query as Search (no paging) — for "select all" then a bulk action.
      operationId: listMasterIds
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
        - { name: search, in: query, schema: { type: string } }
        - { name: list_id, in: query, schema: { type: integer } }
        - $ref: "#/components/parameters/Filter"
        - $ref: "#/components/parameters/Assigned"
      responses:
        "200":
          description: The ids.
          content:
            application/json:
              example: { ids: [311, 312, 318], total: 3 }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/lookup:
    post:
      tags: [People & Companies]
      summary: Look up people by master id
      description: Name, email and ids of the given people (`kind` must be `people`).
      operationId: lookupPeople
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [ids], properties: { ids: { type: array, items: { type: integer } } } }
            example: { ids: [311, 312] }
      responses:
        "200": { description: "`{ contacts, requested, truncated }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/delete:
    post:
      tags: [People & Companies]
      summary: Delete people or companies
      description: |
        Deletes the rows **and** their records, activity and tasks. Cannot be
        undone. Give `master_ids` or `entity_ids`, or `all: true` for every row.
      operationId: deleteMaster
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                master_ids: { type: array, items: { type: integer } }
                entity_ids: { type: array, items: { type: integer } }
                all: { type: boolean }
            example: { master_ids: [311, 312] }
      responses:
        "200": { description: "`{ deleted }` — how many were deleted." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/import:
    post:
      tags: [People & Companies]
      summary: Import rows
      description: |
        Import a chunk of rows (the CSV import's engine). Each row is an object
        of source column → value; `column_mapping` maps source columns to
        column slugs. Existing people/companies are matched by their unique
        key (email, domain …) and updated or skipped by `overwrite_existing`.
        Send large files in chunks of a few hundred rows.
      operationId: importMaster
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [rows, column_mapping]
              properties:
                rows: { type: array, items: { type: object, additionalProperties: true } }
                column_mapping: { type: object, additionalProperties: { type: string }, description: "source column → column slug" }
                overwrite_existing: { type: boolean, default: false }
                add_to_list_id: { type: integer, description: Put every imported row in this list. }
                source: { type: string, description: A label for where the rows came from. }
            example:
              rows: [{ "First Name": Ada, "Email": ada@example.com }]
              column_mapping: { "First Name": first_name, "Email": email }
              add_to_list_id: 55
      responses:
        "200": { description: "`{ inserted, updated, skipped, kept, untouched, … }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/check-duplicates:
    post:
      tags: [People & Companies]
      summary: Check rows for duplicates
      description: Before an import — which rows already exist, and which would change.
      operationId: checkDuplicates
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [kind, rows]
              properties:
                kind: { type: string, enum: [person, company] }
                rows: { type: array, items: { type: object, additionalProperties: true }, description: "Rows keyed by column slug." }
            example: { kind: person, rows: [{ email: ada@example.com, first_name: Ada }] }
      responses:
        "200": { description: "`{ kind, checked, matched, conflicted, unchanged, unkeyed, … }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /master/{kind}/merge:
    post:
      tags: [People & Companies]
      summary: Merge people
      description: Merge 2–20 people into one (`kind` must be `people`). The same engine as the Merge button.
      operationId: mergePeople
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                keep_master_id: { type: integer, description: The person to keep. }
                merge_master_ids: { type: array, items: { type: integer }, description: The people merged into it (1–19). }
                keep_entity_id: { type: integer, description: Or by record id. }
                merge_entity_ids: { type: array, items: { type: integer } }
                preview: { type: boolean, description: "true → only show the candidates (send master_ids or entity_ids); nothing is merged." }
                master_ids: { type: array, items: { type: integer } }
                entity_ids: { type: array, items: { type: integer } }
            example: { keep_master_id: 311, merge_master_ids: [318, 402] }
      responses:
        "200": { description: "The merge result (or, with preview, the candidates)." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # ---------------------------------------------------------------- Records
  /entities:
    get:
      tags: [Records]
      summary: List records of a type
      description: |
        Records of one record type (or of one list — a list is a record type
        with `is_list: true`). Give `entity_type_id` or `entity_type_slug`.
      operationId: listEntities
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: entity_type_id, in: query, schema: { type: integer } }
        - { name: entity_type_slug, in: query, schema: { type: string }, example: person }
        - { name: search, in: query, schema: { type: string } }
        - { name: kanban_stage_id, in: query, schema: { type: integer } }
        - { name: sort, in: query, schema: { type: string, default: created_at }, description: "`created_at`, `updated_at`, `follow_up_date`, `last_activity_date` or a field slug." }
        - $ref: "#/components/parameters/Order"
        - { name: page, in: query, schema: { type: integer, minimum: 1, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 1000, default: 50 }, description: Keep to 1000 or less. }
        - $ref: "#/components/parameters/Filter"
        - $ref: "#/components/parameters/Assigned"
      responses:
        "200":
          description: One page of records.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: "#/components/schemas/Entity" } }
                  total: { type: integer }
                  page: { type: integer }
                  limit: { type: integer }
                  total_pages: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Records]
      summary: Create a record
      description: |
        Create a record of any type. For people and companies, Create a person
        or company is simpler and gives both ids.
      operationId: createEntity
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_type_id, field_values]
              properties:
                entity_type_id: { type: integer }
                field_values: { type: object, additionalProperties: true, description: "field slug → value" }
                kanban_stage_id: { type: integer, description: Starting stage; the first stage if left out. }
            example: { entity_type_id: 3, field_values: { deal_name: Renewal 2027, amount: 12000 } }
      responses:
        "201":
          description: Created.
          content:
            application/json:
              example: { id: 9122, success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/{id}:
    parameters: [{ $ref: "#/components/parameters/RecordId" }]
    get:
      tags: [Records]
      summary: Get a record
      description: Every field, its stage, follow-up, relationships and lists.
      operationId: getEntity
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200":
          description: The record.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Entity" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Records]
      summary: Update a record
      description: |
        Only what you send changes. `follow_up_date` takes a date or a phrase
        like `in 5 days 5 PM` or `wed 7.30 pm`; `null` clears it.
      operationId: updateEntity
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                field_values: { type: object, additionalProperties: true, description: "field slug → new value" }
                kanban_stage_id: { type: integer }
                follow_up_date: { type: [string, "null"] }
                follow_up_tz: { type: string, description: "IANA time zone the follow-up phrase is read in, e.g. Europe/Berlin." }
                notes: { type: string }
            example: { field_values: { title: CTO }, follow_up_date: in 3 days 10 AM, follow_up_tz: Europe/London }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Records]
      summary: Delete a record
      description: |
        Deletes the record with its activity and tasks — cannot be undone. With
        `list_id` it is only taken out of that list.
      operationId: deleteEntity
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: list_id, in: query, schema: { type: integer }, description: Only remove it from this list. }
      responses:
        "200":
          description: Deleted (or removed from the list).
          content:
            application/json:
              example: { success: true }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/{id}/status:
    put:
      tags: [Records]
      summary: Move to a pipeline stage
      operationId: setEntityStage
      parameters: [{ $ref: "#/components/parameters/RecordId" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [kanban_stage_id], properties: { kanban_stage_id: { type: integer } } }
            example: { kanban_stage_id: 14 }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/{id}/location:
    put:
      tags: [Records]
      summary: Set a record's map location
      operationId: setEntityLocation
      parameters: [{ $ref: "#/components/parameters/RecordId" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [lat, lng]
              properties:
                lat: { type: number }
                lng: { type: number }
                city: { type: string }
                country: { type: string }
            example: { lat: 51.5072, lng: -0.1276, city: London, country: United Kingdom }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/needs-attention:
    get:
      tags: [Records]
      summary: Records that need attention
      description: Overdue follow-ups and records gone quiet — the dashboard's "needs attention" list.
      operationId: needsAttention
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: limit, in: query, schema: { type: integer } }
      responses:
        "200": { description: The records. }
        "401": { $ref: "#/components/responses/Unauthorized" }

  # ---------------------------------------------------- Bulk record actions
  /entities/bulk-stage:
    post:
      tags: [Bulk record actions]
      summary: Move many records to a stage
      operationId: bulkStage
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_ids, stage_id]
              properties:
                entity_ids: { type: array, items: { type: integer }, minItems: 1 }
                stage_id: { type: integer }
            example: { entity_ids: [9120, 9121], stage_id: 14 }
      responses:
        "200": { description: "`{ updated }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/bulk-update-field:
    post:
      tags: [Bulk record actions]
      summary: Set one field on many records
      operationId: bulkUpdateField
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_ids, field_definition_id]
              properties:
                entity_ids: { type: array, items: { type: integer }, minItems: 1 }
                field_definition_id: { type: integer, description: "The field's id (Get a record type → fields)." }
                value: { description: The new value. }
            example: { entity_ids: [9120, 9121], field_definition_id: 88, value: Partner }
      responses:
        "200": { description: "`{ updated }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/bulk-delete:
    post:
      tags: [Bulk record actions]
      summary: Delete many records
      description: Deletes the records — or, with `list_id`, only takes them out of that list.
      operationId: bulkDelete
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_ids]
              properties:
                entity_ids: { type: array, items: { type: integer }, minItems: 1 }
                list_id: { type: integer }
            example: { entity_ids: [9120, 9121] }
      responses:
        "200": { description: "`{ deleted }` or `{ removed_from_list }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/bulk-move:
    post:
      tags: [Bulk record actions]
      summary: Move or copy records to another type or list
      operationId: bulkMove
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_ids, target_entity_type_slug]
              properties:
                entity_ids: { type: array, items: { type: integer }, minItems: 1 }
                target_entity_type_slug: { type: string }
                source_list_id: { type: integer, description: Also take them out of this list. }
            example: { entity_ids: [9120], target_entity_type_slug: warm-leads }
      responses:
        "200": { description: "`{ moved, … }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entities/bulk-assign:
    post:
      tags: [Bulk record actions, Team]
      summary: Assign many contacts to team members
      operationId: bulkAssign
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                kind: { type: string, enum: [people, companies] }
                entity_ids: { type: array, items: { type: integer } }
                master_ids: { type: array, items: { type: integer } }
                add: { type: array, items: { type: string }, description: Team member ids to add (from List team people). }
                remove: { type: array, items: { type: string } }
      responses:
        "200": { description: "`{ updated }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }

  # ------------------------------------------------- Record types & fields
  /entity-types:
    get:
      tags: [Record types & fields]
      summary: List record types
      description: People, Companies, custom types and lists, with their ids and slugs.
      operationId: listEntityTypes
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200":
          description: The record types.
          content:
            application/json:
              schema: { type: array, items: { $ref: "#/components/schemas/EntityType" } }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Record types & fields]
      summary: Create a record type
      operationId: createEntityType
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties: { name: { type: string }, icon: { type: string }, color: { type: string } }
            example: { name: Deals, icon: briefcase, color: "#5c8d76" }
      responses:
        "201": { description: The new record type. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /entity-types/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Record types & fields]
      summary: Get a record type
      description: With its fields, pipeline stages and follow-up settings.
      operationId: getEntityType
      responses:
        "200": { description: The record type. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Record types & fields]
      summary: Update a record type
      description: |
        Rename or recolour a record type or list, and/or set its follow-up interval.
        `follow_up_days` (0–365 whole days) is the automatic follow-up for that list's contacts only; 0 = no
        automatic follow-up. Saving it re-works the automatic follow-up of every contact of the list at once
        (last activity logged by a person + the days, or cleared for 0); follow-ups set by hand are never touched.
        A contact in several lists uses the shortest interval above 0.
      operationId: updateEntityType
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                icon: { type: string }
                color: { type: string }
                follow_up_days: { type: integer, minimum: 0, maximum: 365, description: "0 = no automatic follow-up." }
            example: { follow_up_days: 0 }
      responses:
        "200": { description: "The record type. With `follow_up_days`, also `follow_up_days` and `follow_up_updated` (contacts whose follow-up changed)." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Record types & fields]
      summary: Delete a record type
      description: Deletes the type and every record of it. Cannot be undone.
      operationId: deleteEntityType
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entity-types/{id}/fields:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Record types & fields]
      summary: List fields of a record type
      operationId: listFields
      responses:
        "200": { description: "The fields, in order." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Record types & fields]
      summary: Add a field
      operationId: createField
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string }
                field_type: { $ref: "#/components/schemas/FieldType" }
                is_required: { type: boolean }
                default_value: { type: string }
                options_json: { type: array, items: { type: string }, description: Choices for select / multi_select. }
                show_in_table: { type: boolean }
                is_pinned: { type: boolean }
            example: { name: Amount, field_type: currency, show_in_table: true }
      responses:
        "201": { description: The new field. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Record types & fields]
      summary: Update fields (and their order)
      operationId: updateFields
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [fields]
              properties: { fields: { type: array, items: { type: object, additionalProperties: true } } }
      responses:
        "200": { description: The fields. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /entity-types/{id}/field-values:
    get:
      tags: [Record types & fields]
      summary: Distinct values of a field
      description: The values a field holds across the type (for filter pick-lists), optionally with counts.
      operationId: fieldValues
      parameters:
        - $ref: "#/components/parameters/Id"
        - $ref: "#/components/parameters/Project"
        - { name: slug, in: query, required: true, schema: { type: string } }
        - { name: q, in: query, schema: { type: string }, description: Only values containing this. }
        - { name: counts, in: query, schema: { type: string, enum: ["1"] } }
        - { name: limit, in: query, schema: { type: integer } }
        - $ref: "#/components/parameters/Filter"
      responses:
        "200": { description: The values. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # ------------------------------------------------------------------ Lists
  /lists:
    get:
      tags: [Lists]
      summary: List lists
      operationId: listLists
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: kind, in: query, schema: { type: string, enum: [person, company] } }
      responses:
        "200": { description: "The lists with `member_count`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Lists]
      summary: Create a list
      description: |
        Optionally filled straight away with `master_ids` (up to 50,000). With
        `parent_id` it is a segment of that list and takes its kind.
      operationId: createList
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string, maxLength: 255 }
                kind: { type: string, enum: [person, company] }
                master_ids: { type: array, items: { type: integer }, maxItems: 50000 }
                description: { type: string }
                color: { type: string }
                icon: { type: string }
                parent_id: { type: integer }
            example: { name: Webinar Nov 2026, kind: person, master_ids: [311, 312] }
      responses:
        "201": { description: "The list with `member_count`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "409": { $ref: "#/components/responses/Conflict" }
  /lists/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Lists]
      summary: Get a list
      operationId: getList
      responses:
        "200": { description: The list. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Lists]
      summary: Update a list
      operationId: updateList
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties: { name: { type: string }, description: { type: string }, color: { type: string }, icon: { type: string } }
      responses:
        "200": { description: The list. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
    delete:
      tags: [Lists]
      summary: Delete a list
      description: The list goes; the people and companies in it stay.
      operationId: deleteList
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /lists/{id}/members:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Lists]
      summary: List members
      operationId: listMembers
      responses:
        "200":
          description: The master ids in the list.
          content:
            application/json:
              example: { master_ids: [311, 312] }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Lists]
      summary: Add to a list
      operationId: addMembers
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [master_ids], properties: { master_ids: { type: array, items: { type: integer } } } }
            example: { master_ids: [318] }
      responses:
        "200":
          description: Added. Campaigns of the list pick them up.
          content:
            application/json:
              example: { added: 1, requested: 1, campaigns: [], success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Lists]
      summary: Remove from a list
      operationId: removeMembers
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                master_ids: { type: array, items: { type: integer } }
                entity_ids: { type: array, items: { type: integer } }
            example: { master_ids: [318] }
      responses:
        "200":
          description: Removed.
          content:
            application/json:
              example: { removed: 1, campaigns: [], success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /lists/{id}/move:
    post:
      tags: [Lists]
      summary: Move a list to another project
      operationId: moveList
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [project_id], properties: { project_id: { type: integer } } }
            example: { project_id: 7 }
      responses:
        "200": { description: Moved. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /lists/{id}/share:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Lists, Team]
      summary: Who a list is shared with
      operationId: getListShare
      responses:
        "200": { description: "`{ people, segment }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
    put:
      tags: [Lists, Team]
      summary: Share a list with a team member
      operationId: shareList
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [sub, level]
              properties:
                sub: { type: string, description: The team member's id (List team people). }
                level: { type: string, enum: [none, view, edit, manage] }
      responses:
        "200": { description: "`{ person }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }

  # --------------------------------------------------------------- Activity
  /entities/{id}/activities:
    parameters: [{ $ref: "#/components/parameters/RecordId" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Activity]
      summary: A record's activity
      description: Newest first.
      operationId: listActivities
      parameters:
        - { name: type, in: query, schema: { type: string }, description: "An activity type slug, e.g. `email`." }
        - { name: direction, in: query, schema: { $ref: "#/components/schemas/Direction" } }
        - { name: include_related, in: query, schema: { type: string, enum: ["1"] }, description: Also the activity of related records (a company's people). }
        - { name: page, in: query, schema: { type: integer, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, default: 50 } }
      responses:
        "200":
          description: One page of activity.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: "#/components/schemas/Activity" } }
                  total: { type: integer }
                  page: { type: integer }
                  limit: { type: integer }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Activity]
      summary: Log an activity
      description: Moves the record's follow-up date on by its type's default interval.
      operationId: logActivity
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [activity_type_id]
              properties:
                activity_type_id: { type: integer, description: From List activity types. }
                direction: { $ref: "#/components/schemas/Direction" }
                subject: { type: string }
                body: { type: string }
                metadata_json: { type: object, additionalProperties: true }
                logged_by: { type: string, enum: [manual, ai], default: manual }
                created_at: { type: string, format: date-time, description: When it happened; now if left out. }
            example: { activity_type_id: 7, direction: outbound, subject: Intro call, body: Talked about the Q1 rollout. }
      responses:
        "201":
          description: Logged.
          content:
            application/json:
              example: { id: 50211, success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /activities/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Activity]
      summary: Get an activity
      operationId: getActivity
      responses:
        "200": { description: The activity. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Activity]
      summary: Update an activity
      operationId: updateActivity
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                activity_type_id: { type: integer }
                direction: { $ref: "#/components/schemas/Direction" }
                subject: { type: string }
                body: { type: string }
                metadata_json: { type: object, additionalProperties: true }
                created_at: { type: string, format: date-time }
      responses:
        "200": { description: The activity. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Activity]
      summary: Delete an activity
      operationId: deleteActivity
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /activities/recent:
    get:
      tags: [Activity]
      summary: Recent activity across the project
      operationId: recentActivity
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: entity_type, in: query, schema: { type: string }, description: Record type slug. }
        - { name: activity_type, in: query, schema: { type: string }, description: Activity type slug. }
        - { name: direction, in: query, schema: { $ref: "#/components/schemas/Direction" } }
        - { name: logged_by, in: query, schema: { type: string, enum: [manual, ai] } }
        - { name: date_from, in: query, schema: { type: string, format: date } }
        - { name: date_to, in: query, schema: { type: string, format: date } }
        - { name: search, in: query, schema: { type: string } }
        - { name: page, in: query, schema: { type: integer, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, default: 50 } }
      responses:
        "200": { description: "`{ data, total, page, limit, total_pages }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /activities/calendar:
    get:
      tags: [Activity]
      summary: Activity and follow-ups between two dates
      operationId: activityCalendar
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: entity_type_slug, in: query, required: true, schema: { type: string } }
        - { name: from, in: query, required: true, schema: { type: string, format: date } }
        - { name: to, in: query, required: true, schema: { type: string, format: date } }
      responses:
        "200": { description: "`{ activities, entity_dates }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # --------------------------------------------------------- Activity types
  /activity-types:
    get:
      tags: [Activity types]
      summary: List activity types
      description: Email, WhatsApp, LinkedIn Message, Meeting, Phone Call, Notes … and your own.
      operationId: listActivityTypes
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200":
          description: The types.
          content:
            application/json:
              example: [{ id: 7, name: Phone Call, slug: phone-call, icon: phone, color: "#2563eb", sort_order: 7 }]
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Activity types]
      summary: Create an activity type
      operationId: createActivityType
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties: { name: { type: string }, icon: { type: string }, color: { type: string } }
      responses:
        "201": { description: The new type. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /activity-types/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    put:
      tags: [Activity types]
      summary: Update an activity type
      operationId: updateActivityType
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties: { name: { type: string }, icon: { type: string }, color: { type: string }, sort_order: { type: integer } }
      responses:
        "200": { description: The type. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Activity types]
      summary: Delete an activity type
      operationId: deleteActivityType
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # ------------------------------------------------------------------ Tasks
  /tasks:
    get:
      tags: [Tasks]
      summary: List tasks
      operationId: listTasks
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: status, in: query, schema: { $ref: "#/components/schemas/TaskStatus" } }
        - { name: priority, in: query, schema: { $ref: "#/components/schemas/Priority" } }
        - { name: type, in: query, schema: { type: string }, description: Task type. }
        - { name: created_by, in: query, schema: { type: string, enum: [manual, ai] } }
        - { name: entity_id, in: query, schema: { type: integer }, description: Only one record's tasks. }
        - { name: entity_type_slug, in: query, schema: { type: string } }
        - { name: search, in: query, schema: { type: string } }
        - { name: sort, in: query, schema: { type: string, enum: [due_date, priority, created_at], default: due_date } }
        - $ref: "#/components/parameters/Order"
        - { name: page, in: query, schema: { type: integer, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, default: 50 } }
      responses:
        "200":
          description: One page of tasks.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: "#/components/schemas/Task" } }
                  total: { type: integer }
                  page: { type: integer }
                  limit: { type: integer }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Tasks]
      summary: Create a task
      operationId: createTask
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [title]
              properties:
                title: { type: string }
                description: { type: string }
                entity_id: { type: integer, description: The record the task is about. }
                task_type: { type: string }
                priority: { $ref: "#/components/schemas/Priority" }
                due_date: { type: string, format: date-time }
                created_by: { type: string, enum: [manual, ai], default: manual }
                assigned_to: { type: string }
            example: { title: Send the proposal, entity_id: 9120, priority: high, due_date: "2026-11-05T09:00:00Z" }
      responses:
        "201":
          description: Created.
          content:
            application/json:
              example: { id: 801, success: true }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Tasks]
      summary: Set the status of many tasks
      operationId: setTasksStatus
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [ids, status]
              properties:
                ids: { type: array, items: { type: integer } }
                status: { $ref: "#/components/schemas/TaskStatus" }
            example: { ids: [801, 802], status: completed }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /tasks/bulk:
    post:
      tags: [Tasks]
      summary: Complete, delete or re-status many tasks
      operationId: bulkTasks
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [ids, action]
              properties:
                ids: { type: array, items: { type: integer } }
                action: { type: string, enum: [complete, delete, status] }
                status: { $ref: "#/components/schemas/TaskStatus" }
            example: { ids: [801, 802], action: complete }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /tasks/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Tasks]
      summary: Get a task
      operationId: getTask
      responses:
        "200":
          description: The task.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Task" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    put:
      tags: [Tasks]
      summary: Update a task
      description: Only what you send changes.
      operationId: updateTask
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                title: { type: string }
                description: { type: string }
                task_type: { type: string }
                status: { $ref: "#/components/schemas/TaskStatus" }
                priority: { $ref: "#/components/schemas/Priority" }
                due_date: { type: [string, "null"], format: date-time }
                assigned_to: { type: [string, "null"] }
                entity_id: { type: [integer, "null"] }
            example: { status: in_progress, due_date: "2026-11-06T09:00:00Z" }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Tasks]
      summary: Delete a task
      operationId: deleteTask
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /tasks/{id}/complete:
    put:
      tags: [Tasks]
      summary: Toggle a task done / not done
      operationId: toggleTask
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }

  # -------------------------------------------------------- Pipeline stages
  /kanban/stages:
    get:
      tags: [Pipeline stages]
      summary: List stages of a record type
      operationId: listStages
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: entity_type_id, in: query, schema: { type: integer } }
        - { name: entity_type_slug, in: query, schema: { type: string }, example: person }
      responses:
        "200":
          description: The stages in order, with `entity_count`.
          content:
            application/json:
              example: [{ id: 14, name: Contacted, color: "#f59e0b", sort_order: 2, entity_count: 37 }]
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Pipeline stages]
      summary: Add a stage
      operationId: createStage
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entity_type_id, name]
              properties: { entity_type_id: { type: integer }, name: { type: string }, color: { type: string } }
            example: { entity_type_id: 1, name: Demo booked, color: "#5c8d76" }
      responses:
        "201": { description: The new stage. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /kanban/stages/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    put:
      tags: [Pipeline stages]
      summary: Update a stage
      operationId: updateStage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties: { name: { type: string }, color: { type: string }, sort_order: { type: integer } }
      responses:
        "200": { description: The stage. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Pipeline stages]
      summary: Delete a stage
      operationId: deleteStage
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /kanban/stages/reorder:
    put:
      tags: [Pipeline stages]
      summary: Reorder stages
      operationId: reorderStages
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [stages]
              properties:
                stages:
                  type: array
                  items: { type: object, required: [id, sort_order], properties: { id: { type: integer }, sort_order: { type: integer } } }
            example: { stages: [{ id: 14, sort_order: 1 }, { id: 15, sort_order: 2 }] }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # -------------------------------------------------------------- Campaigns
  /campaigns:
    get:
      tags: [Campaigns]
      summary: List campaigns
      operationId: listCampaigns
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ campaigns }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Campaigns]
      summary: Create a campaign
      description: A draft, with the people of a list or the given master ids (up to 50,000).
      operationId: createCampaign
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                list_id: { type: integer }
                master_ids: { type: array, items: { type: integer } }
                sender_id: { type: string, description: A sender from List senders. }
            example: { name: November newsletter, list_id: 55 }
      responses:
        "201":
          description: The draft and who made it in.
          content:
            application/json:
              example: { id: 21, audience: { added: 2, noEmail: 0, duplicate: 0, suppressed: 0 } }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /campaigns/{id}:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Campaigns]
      summary: Get a campaign
      operationId: getCampaign
      responses:
        "200": { description: "`{ campaign, attachments, … }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    patch:
      tags: [Campaigns]
      summary: Edit a campaign
      operationId: updateCampaign
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                subject: { type: string }
                preheader: { type: string }
                html: { type: string, description: "The email body. `{{first_name}}`-style merge fields are filled per person." }
                sender_id: { type: string }
                include_signature: { type: boolean }
                reply_to_mode: { type: string }
                reply_to: { type: string }
            example: { subject: "Hi {{first_name}}, our November news", html: "<p>Hello {{first_name}},</p>" }
      responses:
        "200": { description: "`{ campaign, attachments, errors }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    delete:
      tags: [Campaigns]
      summary: Delete a campaign
      description: A sent campaign cannot be deleted (409).
      operationId: deleteCampaign
      responses:
        "200": { description: "`{ ok: true }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "409": { $ref: "#/components/responses/Conflict" }
  /campaigns/{id}/recipients:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Campaigns]
      summary: List recipients
      operationId: listRecipients
      parameters:
        - { name: limit, in: query, schema: { type: integer } }
        - { name: offset, in: query, schema: { type: integer } }
      responses:
        "200": { description: "`{ recipients, … }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Campaigns]
      summary: Add recipients
      operationId: addRecipients
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [master_ids], properties: { master_ids: { type: array, items: { type: integer } } } }
      responses:
        "200": { description: "`{ audience, stats }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
    delete:
      tags: [Campaigns]
      summary: Remove recipients
      operationId: removeRecipients
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [recipient_ids], properties: { recipient_ids: { type: array, items: { type: integer } } } }
      responses:
        "200": { description: "`{ removed, stats }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /campaigns/{id}/preview:
    post:
      tags: [Campaigns]
      summary: Preview the email for one recipient
      operationId: previewCampaign
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                recipient_id: { type: integer }
                index: { type: integer, description: Or the n-th recipient. }
                subject: { type: string }
                preheader: { type: string }
                html: { type: string }
      responses:
        "200": { description: "`{ subject, preheader, html, text, recipient }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /campaigns/{id}/test-send:
    post:
      tags: [Campaigns]
      summary: Send a test email
      operationId: testSendCampaign
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [to]
              properties:
                to: { type: string, format: email }
                subject: { type: string }
                html: { type: string }
                sender_id: { type: string }
            example: { to: me@example.com }
      responses:
        "200": { description: "`{ ok: true }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /campaigns/{id}/launch:
    post:
      tags: [Campaigns]
      summary: Launch a campaign
      description: Sends it now, or spread over a time window. This sends real email.
      operationId: launchCampaign
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                mode: { type: string, enum: [now, at_time, over_duration, batch], default: now }
                start_at: { type: string, format: date-time }
                duration_hours: { type: number }
                window: { type: object, additionalProperties: true }
                batch: { type: object, additionalProperties: true }
            example: { mode: now }
      responses:
        "200": { description: "`{ queued, drained, campaign, stats }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /campaigns/{id}/duplicate:
    post:
      tags: [Campaigns]
      summary: Duplicate a campaign
      operationId: duplicateCampaign
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      responses:
        "201": { description: "`{ id, name, audience }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /campaigns/{id}/reset:
    post:
      tags: [Campaigns]
      summary: Reset a campaign to draft
      operationId: resetCampaign
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ ok, restored }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "409": { $ref: "#/components/responses/Conflict" }
  /campaigns/{id}/activity:
    get:
      tags: [Campaigns]
      summary: A campaign's history
      operationId: campaignActivity
      parameters:
        - $ref: "#/components/parameters/Id"
        - $ref: "#/components/parameters/Project"
        - { name: limit, in: query, schema: { type: integer } }
      responses:
        "200": { description: "`{ entries }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /marketing/senders:
    get:
      tags: [Campaigns]
      summary: List senders
      description: The sending addresses set up on the Zocton Dashboard.
      operationId: listSenders
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ senders, available, manage_href }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /marketing/suppressions:
    get:
      tags: [Campaigns]
      summary: List suppressed addresses
      description: Addresses that are never emailed (unsubscribed, bounced, do-not-mail).
      operationId: listSuppressions
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ suppressions }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }

  # ------------------------------------------------------------------ Files
  /entities/{id}/files:
    parameters: [{ $ref: "#/components/parameters/RecordId" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Files]
      summary: A record's files
      operationId: listFiles
      responses:
        "200": { description: The files. }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [Files]
      summary: Upload a file to a record
      operationId: uploadFile
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties: { file: { type: string, format: binary } }
      responses:
        "201": { description: The stored file. }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /uploads/{entityId}/{filename}:
    get:
      tags: [Files]
      summary: Download a file
      operationId: downloadFile
      parameters:
        - { name: entityId, in: path, required: true, schema: { type: integer } }
        - { name: filename, in: path, required: true, schema: { type: string } }
        - $ref: "#/components/parameters/Project"
      responses:
        "200":
          description: The file.
          content:
            application/octet-stream:
              schema: { type: string, format: binary }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /files/{fileId}:
    delete:
      tags: [Files]
      summary: Delete a file
      operationId: deleteFile
      parameters:
        - { name: fileId, in: path, required: true, schema: { type: integer } }
        - $ref: "#/components/parameters/Project"
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }

  # ------------------------------------------------------------------- Team
  /team/people:
    get:
      tags: [Team]
      summary: List team people
      description: The people on your Zocton team who can open this project — their ids are what assignments and sharing take.
      operationId: listTeamPeople
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ people, me, canAssign, canShare }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /entities/{id}/assignees:
    parameters: [{ $ref: "#/components/parameters/RecordId" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Team]
      summary: Who a contact is assigned to
      operationId: getAssignees
      responses:
        "200": { description: "`{ assignees }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
    put:
      tags: [Team]
      summary: Assign a contact
      description: Replaces the contact's assignees.
      operationId: setAssignees
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [assignees], properties: { assignees: { type: array, items: { type: string } } } }
      responses:
        "200": { description: "`{ assignees }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }

  # --------------------------------------------------------------- Settings
  /settings:
    get:
      tags: [Settings]
      summary: Read settings
      operationId: getSettings
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: category, in: query, schema: { type: string }, description: "e.g. `general`, `view_config`, `columns`." }
      responses:
        "200": { description: The settings. }
        "401": { $ref: "#/components/responses/Unauthorized" }
    put:
      tags: [Settings]
      summary: Save a setting
      operationId: putSetting
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [category, key]
              properties: { category: { type: string }, key: { type: string }, value: {} }
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /dashboard:
    get:
      tags: [Settings]
      summary: Dashboard numbers
      description: The counts the CRM's dashboard shows for this project.
      operationId: getDashboard
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: The counts. }
        "401": { $ref: "#/components/responses/Unauthorized" }

  # -------------------------------------------------------------- Utilities
  /geocode:
    get:
      tags: [Utilities]
      summary: Geocode a place
      operationId: geocode
      parameters:
        - { name: query, in: query, required: true, schema: { type: string }, example: "Pune, India" }
        - { name: cache_only, in: query, schema: { type: string, enum: ["1"] } }
      responses:
        "200":
          description: The place.
          content:
            application/json:
              example: { lat: 18.5204, lng: 73.8567, display_name: "Pune, Maharashtra, India" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /geocode/bulk:
    post:
      tags: [Utilities]
      summary: Look up many places (cache only)
      operationId: geocodeBulk
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, required: [queries], properties: { queries: { type: array, items: { type: string } } } }
      responses:
        "200": { description: "`{ results, requested, found }`." }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
  /health:
    get:
      tags: [Utilities]
      summary: Health and version
      description: No key needed.
      operationId: health
      security: []
      responses:
        "200":
          description: Up.
          content:
            application/json:
              example: { status: ok, app: entayo-crm, version: "0.136.0" }

  # --------------------------------------------------------------- API keys
  /api-keys:
    get:
      tags: [API keys]
      summary: List API keys
      description: Your keys — name, prefix, last used. Never the secret.
      operationId: listApiKeys
      responses:
        "200":
          description: The keys.
          content:
            application/json:
              example: [{ id: 3, name: Zapier, key_prefix: entayo_ab12, created_by: ada, last_used_at: "2026-11-01T10:00:00.000Z", revoked: 0, created_at: "2026-10-01T08:00:00.000Z" }]
        "401": { $ref: "#/components/responses/Unauthorized" }
  /api-keys/{id}:
    delete:
      tags: [API keys]
      summary: Revoke an API key
      description: The key stops working at once. Cannot be undone.
      operationId: revokeApiKey
      parameters: [{ $ref: "#/components/parameters/Id" }]
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }

components:
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      bearerFormat: "entayo_…"
      description: "An API key from Entayo CRM → Settings → API keys, sent as `Authorization: Bearer entayo_…`."
  parameters:
    Project:
      name: X-Entayo-Project
      in: header
      required: false
      schema: { type: integer, minimum: 1 }
      description: The project to work in (List projects → `id`). Left out → Default. Not yours → 404 `project_not_found`.
    Kind:
      name: kind
      in: path
      required: true
      schema: { type: string, enum: [people, companies] }
    Id:
      name: id
      in: path
      required: true
      schema: { type: integer, minimum: 1 }
    RecordId:
      name: id
      in: path
      required: true
      description: The record id (`entity_id`).
      schema: { type: integer, minimum: 1 }
    Order:
      name: order
      in: query
      schema: { type: string, enum: [asc, desc], default: desc }
    Filter:
      name: filter
      in: query
      schema: { type: string }
      description: An encoded filter — see [Filters](/docs/filters).
    Assigned:
      name: assigned
      in: query
      schema: { type: string }
      description: Only contacts assigned to this team member id (`me` for yourself, in a team).
  responses:
    Success:
      description: Done.
      content:
        application/json:
          schema: { type: object, properties: { success: { type: boolean } } }
          example: { success: true }
    BadRequest:
      description: Something in the request is missing or wrong — `error` says what.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: entity_ids must be a non-empty array }
    Unauthorized:
      description: No API key, or a key that is wrong or revoked.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: Not authenticated }
    Forbidden:
      description: Not allowed for this account.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    NotFound:
      description: Not found in this project (or the `X-Entayo-Project` is not yours).
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: Entity not found }
    Conflict:
      description: Clashes with what is there (a name already taken, a campaign already sent).
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error: { type: string }
        code: { type: string }
    Project:
      type: object
      properties:
        id: { type: integer }
        name: { type: string }
        is_default: { type: boolean }
        color: { type: [string, "null"] }
        logo_url: { type: [string, "null"] }
        emoji: { type: [string, "null"] }
    Column:
      type: object
      properties:
        slug: { type: string }
        name: { type: string }
        field_type: { $ref: "#/components/schemas/FieldType" }
    FieldType:
      type: string
      enum: [text, number, email, url, phone, date, datetime, select, multi_select, boolean, textarea, currency, location]
    MasterRow:
      type: object
      description: "A person or company: its columns as keys (first_name, email, company_name, country …)."
      additionalProperties: true
      properties:
        id: { type: integer, description: Master id. }
        entity_id: { type: [integer, "null"], description: Record id. }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    Entity:
      type: object
      additionalProperties: true
      properties:
        id: { type: integer, description: Record id. }
        entity_type_id: { type: integer }
        field_values: { type: object, additionalProperties: true, description: "field slug → value" }
        status:
          type: [object, "null"]
          properties: { stage_id: { type: integer }, stage_name: { type: string }, stage_color: { type: string } }
        follow_up: { type: [object, "null"] }
        last_activity_date: { type: [string, "null"], format: date-time }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    EntityType:
      type: object
      additionalProperties: true
      properties:
        id: { type: integer }
        name: { type: string }
        slug: { type: string }
        icon: { type: [string, "null"] }
        color: { type: [string, "null"] }
        is_list: { type: [integer, boolean] }
        master_kind: { type: [string, "null"], enum: [person, company, null] }
    Direction:
      type: string
      enum: [inbound, outbound, internal]
      default: outbound
    Activity:
      type: object
      additionalProperties: true
      properties:
        id: { type: integer }
        entity_id: { type: integer }
        activity_type_id: { type: integer }
        direction: { $ref: "#/components/schemas/Direction" }
        subject: { type: [string, "null"] }
        body: { type: [string, "null"] }
        metadata_json: {}
        logged_by: { type: string }
        created_at: { type: string, format: date-time }
    TaskStatus:
      type: string
      enum: [pending, in_progress, completed, cancelled]
    Priority:
      type: string
      enum: [low, medium, high, urgent]
    Task:
      type: object
      additionalProperties: true
      properties:
        id: { type: integer }
        entity_id: { type: [integer, "null"] }
        title: { type: string }
        description: { type: [string, "null"] }
        task_type: { type: [string, "null"] }
        status: { $ref: "#/components/schemas/TaskStatus" }
        priority: { $ref: "#/components/schemas/Priority" }
        due_date: { type: [string, "null"], format: date-time }
        created_by: { type: string }
        completed_at: { type: [string, "null"], format: date-time }
        created_at: { type: string, format: date-time }
