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 <key>` — see
      [Authentication](/developers/entayo-crm/authentication). Two kinds of
      key work:
      - a **Zocton API key** (`zk_…`), made on the Zocton Dashboard →
        **API Keys** with access to Entayo CRM. One key for every Zocton app.
        A read-only key may only use `GET`; anything else is 403
        `insufficient_scope`. A key made for chosen projects must name one
        with `X-Entayo-Project` on every call (400 `project_required`
        without it, 403 `project_not_allowed` for another project). A Zocton
        key never reaches **API keys** (403 `forbidden_for_api_key`).
      - an **Entayo CRM API key** (`entayo_…`), made in Entayo CRM →
        Settings → **API Keys**. Full access to your CRM.
    - **Project:** add `X-Entayo-Project: <project id>` to work in a project
      other than Default — see [Projects](/developers/entayo-crm/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" }
  /key-projects:
    get:
      tags: [Account]
      summary: The projects this key may use
      description: |
        Only the projects the calling API key may use, each with its level —
        `write` (read and change) or `read`. A key made for chosen projects
        calls this WITHOUT `X-Entayo-Project` (every other route needs it for
        such a key) and sees only its own projects; an every-project key sees
        all of them. Use a project's `id` in the `X-Entayo-Project` header.
        API keys only: any other credential gets 400 `api_key_only`.
      operationId: listKeyProjects
      responses:
        "200":
          description: The key's projects, and whether the key is limited to chosen projects.
          content:
            application/json:
              schema:
                type: object
                properties:
                  projects:
                    type: array
                    items:
                      type: object
                      properties:
                        id: { type: integer }
                        name: { type: string }
                        is_default: { type: boolean }
                        access: { type: string, enum: [read, write] }
                  limited: { type: boolean }
              example:
                projects:
                  - { id: 7, name: Germany, is_default: false, access: write }
                limited: true
        "400": { $ref: "#/components/responses/BadRequest" }
        "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](/developers/entayo-crm/filters)).

        **Structured filters** (CRM 0.154.0): `filters` is a JSON array of
        `{field, op, value}` on any column slug (custom columns, `created_at` and
        `updated_at` too), all of which must hold; `any_of` is a JSON array of
        groups (each an array of conditions that all hold), of which any one must
        hold. Ops: `eq`, `neq`, `in`, `not_in`, `contains`, `not_contains`,
        `contains_any`, `not_contains_any`, `contains_all`, `starts_with`,
        `ends_with`, `gt`, `gte`, `lt`, `lte`, `between` (`[low, high]`),
        `is_empty`, `not_empty`. Text compares case-insensitively; dates are
        `YYYY-MM-DD`. An unknown field, or an op the column's type has no meaning
        for, is a 400 naming it — never ignored.

        `fields=email,title` answers slim rows: `id`, `entity_id` and those
        columns only, without the per-row lists, stage and flags.
      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"
        - { name: filters, in: query, schema: { type: string }, description: 'JSON array of {field, op, value}, ANDed, e.g. [{"field":"email_status","op":"eq","value":"Verified"}]' }
        - { name: any_of, in: query, schema: { type: string }, description: 'JSON array of groups of conditions; a row matches when any group holds' }
        - { name: in_list_ids, in: query, schema: { type: string }, description: "Comma-separated list ids: only rows in at least one." }
        - { name: exclude_list_ids, in: query, schema: { type: string }, description: "Comma-separated list ids: skip rows in any of them." }
        - { name: fields, in: query, schema: { type: string }, description: "Comma-separated column slugs: slim rows." }
        - { name: strict, in: query, schema: { type: string, enum: ["1"] }, description: "An unknown `sort` is a 400 instead of the newest-first default." }
      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}/facets:
    get:
      tags: [People & Companies]
      summary: Count people or companies, per value of a column
      description: |
        How many rows match, with the same filters as [Search](#tag/people--companies/GET/master/{kind})
        (`search`, `list_id`, `filter`, `filters`, `any_of`, `in_list_ids`, `exclude_list_ids`).
        With `group_by`: the count per value of that column, most common first; `value: null` is
        "no value". `split=1` counts each comma-separated part (departments, keywords). CRM 0.154.0.
      operationId: countMaster
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
        - { name: group_by, in: query, schema: { type: string }, description: A column slug. }
        - { name: top, in: query, schema: { type: integer, minimum: 1, maximum: 500, default: 50 } }
        - { name: split, in: query, schema: { type: string, enum: ["1"] } }
        - { name: filters, in: query, schema: { type: string } }
        - { name: any_of, in: query, schema: { type: string } }
        - { name: in_list_ids, in: query, schema: { type: string } }
        - { name: exclude_list_ids, in: query, schema: { type: string } }
        - { name: search, in: query, schema: { type: string } }
        - { name: list_id, in: query, schema: { type: integer } }
      responses:
        "200":
          description: The counts.
          content:
            application/json:
              schema:
                type: object
                properties:
                  kind: { type: string, enum: [person, company] }
                  total: { type: integer }
                  group_by: { type: [string, "null"] }
                  values: { type: array, items: { type: object, properties: { value: {}, count: { type: integer } } } }
                  distinct_values: { type: integer }
              example: { kind: person, total: 67996, group_by: industry, values: [{ value: information technology & services, count: 9120 }, { value: null, count: 311 }], distinct_values: 140 }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /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:
    get:
      tags: [People & Companies]
      summary: Search people, slim
      description: |
        Search people and get back only `id` (master id), `name`, `email`,
        `company` and `title` — the shape a contact picker needs. Takes the
        same `search`, `list_id`, `filter`, `sort`, `order` and `page` as
        List people; `limit` defaults to 50 (at most 200). `kind` must be
        `people` (`companies` → 400).
      operationId: searchPeopleSlim
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
        - { name: search, in: query, schema: { type: string } }
        - { name: list_id, in: query, schema: { type: integer }, description: Only members of this list. }
        - $ref: "#/components/parameters/Filter"
        - { name: page, in: query, schema: { type: integer, minimum: 1, default: 1 } }
        - { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 200, default: 50 } }
      responses:
        "200":
          description: The matching people, slim.
          content:
            application/json:
              example:
                contacts: [{ id: 312, name: Ada Lovelace, email: ada@example.com, company: Analytical Engines, title: Founder }]
                total: 1
                page: 1
                totalPages: 1
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [People & Companies]
      summary: Look up people by master id
      description: |
        Name, email and ids of the given people (`kind` must be `people`).
        Up to 500 ids are answered; `truncated` says when more were sent.
      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}/location:
    get:
      tags: [People & Companies]
      summary: Count missing locations
      description: How many people (or companies) in the project have no timezone, country or state.
      operationId: countMissingLocation
      parameters:
        - $ref: "#/components/parameters/Project"
        - $ref: "#/components/parameters/Kind"
      responses:
        "200":
          description: The total and how many lack each value.
          content:
            application/json:
              schema:
                type: object
                properties:
                  total: { type: integer }
                  timezone: { type: integer }
                  country: { type: integer }
                  state: { type: integer }
              example: { total: 1200, timezone: 340, country: 85, state: 410 }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      tags: [People & Companies]
      summary: Set timezone, country or state in bulk
      description: |
        Sets a timezone, country and/or state on many rows. Name the rows with
        exactly one of `master_ids`, `entity_ids`, `list_id` or `all: true` —
        nothing named is a 400, never "every row". With `only_missing: true`
        only blank values are filled. The timezone must be a real IANA zone
        (`Europe/Berlin`).
      operationId: updateLocations
      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 } }
                list_id: { type: integer }
                all: { type: boolean, description: "`true` to change every row of the project." }
                timezone: { type: string }
                country: { type: string }
                state: { type: string }
                only_missing: { type: boolean, default: false }
            example: { list_id: 55, country: Germany, timezone: Europe/Berlin, only_missing: true }
      responses:
        "200":
          description: How many rows changed.
          content:
            application/json:
              example: { updated: 48 }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /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. }
                row_lists:
                  type: [array, "null"]
                  items: { type: [array, "null"], items: { type: integer } }
                  description: |
                    One entry per row (`null` or `[]` = none): more lists that
                    row goes into, e.g. the WhatsApp group lists from
                    `POST /whatsapp-lists`. Every id must be a list of this project.
                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}/copy:
    post:
      tags: [Lists]
      summary: Copy a list and its records into a project
      description: |
        Each member becomes a record in the target project with every column value (custom columns are
        added there), or reuses the record the target already has with that email (else the same dedupe
        key) — its values untouched. The new list holds them all. `mode: move` then deletes the source
        list (never its records). Activity, tasks, stages, follow-ups and files are not copied. At most
        50,000 members. The target is held to the same access as any write made there. CRM 0.154.0.
      operationId: copyList
      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, description: The target project. }
                name: { type: string, description: "The new list's name (default: the source list's)." }
                mode: { type: string, enum: [copy, move], default: copy }
            example: { project_id: 37, name: TwoDots — CTOs India, mode: copy }
      responses:
        "201":
          description: Copied.
          content:
            application/json:
              example: { success: true, new_list_id: 912, name: TwoDots — CTOs India, target_project_id: 37, copied: 180, reused: 20, skipped: 0, members: 200, source_list_removed: false }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { description: The key or person may not write in the target project. }
        "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" }
  /whatsapp-lists:
    get:
      tags: [Lists]
      summary: List WhatsApp lists
      description: |
        The project's WhatsApp lists that still exist — one per WhatsApp group
        brought in by an import, plus the individual-chats list. `group_key`
        is how the CRM recognises the group next time.
      operationId: listWhatsappLists
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200":
          description: The lists.
          content:
            application/json:
              example:
                lists: [{ group_key: "name:book club", list_id: 81, name: "WhatsApp — Book Club", member_count: 24 }]
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Lists]
      summary: Make or find WhatsApp lists
      description: |
        Before importing WhatsApp contacts: one list per group, and one for
        people in no group. `mode: existing` reuses the list the CRM already
        has for that group (made if there is none); `mode: new` always makes a
        new one. Lists are named `WhatsApp — <group>`. Send the ids you get
        back in `row_lists` on Import rows. Adds only: nothing is removed,
        renamed or deleted.
      operationId: prepareWhatsappLists
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [groups]
              properties:
                groups:
                  type: array
                  items:
                    type: object
                    required: [name]
                    properties:
                      name: { type: string }
                      mode: { type: string, enum: [existing, new], default: existing }
                individual:
                  type: [object, "null"]
                  description: The list for contacts in no group; `null` for none.
                  properties:
                    mode: { type: string, enum: [existing, new], default: existing }
            example: { groups: [{ name: Book Club, mode: existing }], individual: { mode: existing } }
      responses:
        "200":
          description: Group key → list id, the individual list, and how many lists were made.
          content:
            application/json:
              example: { group_lists: { "name:book club": 81 }, individual_list_id: 82, created: 1 }
        "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/{id}/email:
    get:
      tags: [Activity]
      summary: Open a TurboWorkflow email
      description: |
        The email behind an activity that TurboWorkflow logged (a campaign or
        workflow send): subject, HTML, to, from, when, status. It is fetched
        from TurboWorkflow when you ask and never stored in the CRM.
        Any other activity → 400. The project no longer connected to the
        TurboWorkflow project that sent it → 409. TurboWorkflow no longer has
        it → 404. TurboWorkflow not reachable → 502 or 503; try again later.
      operationId: getActivityEmail
      parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
      responses:
        "200":
          description: The email.
          content:
            application/json:
              schema:
                type: object
                properties:
                  subject: { type: string }
                  html: { type: string }
                  to: { type: [string, "null"] }
                  from: { type: [string, "null"] }
                  sent_at: { type: [string, "null"] }
                  status: { type: [string, "null"] }
                  campaign_name: { type: [string, "null"] }
                  workflow_name: { type: [string, "null"] }
                  truncated: { type: boolean, description: "`true` when a very long body was cut short." }
              example: { subject: Our November news, html: "<p>Hello Ada,</p>", to: ada@example.com, from: "Team <team@example.com>", sent_at: "2026-11-20T09:00:00Z", status: delivered, campaign_name: November news, workflow_name: null, truncated: false }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/Conflict" }
        "502": { description: TurboWorkflow gave no usable answer. }
        "503": { description: TurboWorkflow is not reachable right now. }
  /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. Merge fields are filled per person: `{{contact.first_name}}` (or `{{first_name}}`), with a fallback after a pipe — `{{contact.first_name | there}}`." }
                sender_id: { type: string }
                include_signature: { type: boolean }
                reply_to_mode: { type: string }
                reply_to: { type: string }
                attachments:
                  type: array
                  description: The whole attachment list of the email (replaces the old one). Upload each file first with Upload a campaign attachment.
                  items:
                    type: object
                    required: [uploadId, filename, size, contentType]
                    properties:
                      uploadId: { type: string }
                      filename: { type: string }
                      size: { type: integer }
                      contentType: { 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" }
  /campaigns/{id}/ingest:
    parameters: [{ $ref: "#/components/parameters/Id" }, { $ref: "#/components/parameters/Project" }]
    get:
      tags: [Campaigns]
      summary: Get the campaign's email API URL
      description: |
        The secret address that lets another system (a build server, a CMS, a
        script) set this campaign's email without a key: `POST` JSON
        `{ subject, html }` to `url`, or a multipart `file` to
        `attachmentUrl`. Only a draft takes it. The URL is the whole
        credential — keep it like a password. Made the first time you ask.
      operationId: getCampaignApiUrl
      responses:
        "200":
          description: The two addresses.
          content:
            application/json:
              schema:
                type: object
                properties:
                  url: { type: string }
                  attachmentUrl: { type: string }
              example:
                url: "https://apac.zocton.com/entayo-crm/api/ce/3kV…"
                attachmentUrl: "https://apac.zocton.com/entayo-crm/api/ce/3kV…/attachment"
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "503": { description: The address cannot be built right now. Try again later. }
    post:
      tags: [Campaigns]
      summary: Regenerate the campaign's email API URL
      description: A new secret address. The old one stops working at once — update anything that still uses it.
      operationId: regenerateCampaignApiUrl
      responses:
        "200": { description: "The new `{ url, attachmentUrl }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "503": { description: The address cannot be built right now. Try again later. }
  /marketing/attachments:
    post:
      tags: [Campaigns]
      summary: Upload a campaign attachment
      description: |
        Stores one file for a campaign email and answers what to put in the
        campaign's `attachments` (Edit a campaign). Not attached to anything
        until a draft is saved with it. Executable file types are refused
        (400); a file larger than an email can carry is 413.
      operationId: uploadCampaignAttachment
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file: { type: string, format: binary }
      responses:
        "201":
          description: The stored file.
          content:
            application/json:
              example: { uploadId: "u_9f2c…", filename: price-list.pdf, size: 182044, contentType: application/pdf }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "413": { description: The file is too large for an email. }
  /marketing/images:
    post:
      tags: [Campaigns]
      summary: Upload an inline image
      description: |
        Stores an image for an email body and answers its public address, to
        use in `<img src>`. PNG, JPG, GIF or WebP only (no SVG). A very large
        image is 413.
      operationId: uploadCampaignImage
      parameters: [{ $ref: "#/components/parameters/Project" }]
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file: { type: string, format: binary }
      responses:
        "201":
          description: The image's public address.
          content:
            application/json:
              example: { url: "https://…/campaign-images/5e1c….png" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "413": { description: The image is too large. }
        "503": { description: "Image storage is not available here — use an image address instead, or attach the file." }
  /marketing/senders:
    get:
      tags: [Campaigns]
      summary: List senders
      description: The sending addresses set up on the Zocton Dashboard. Never a relay password.
      operationId: listSenders
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "200": { description: "`{ senders, available, manage_href }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
    post:
      tags: [Campaigns]
      summary: Add a sender (moved to the Zocton Dashboard)
      description: Senders are added and changed only on the Zocton Dashboard → Senders. Always 410, with where to go.
      deprecated: true
      operationId: createSender
      parameters: [{ $ref: "#/components/parameters/Project" }]
      responses:
        "401": { $ref: "#/components/responses/Unauthorized" }
        "410": { $ref: "#/components/responses/Gone" }
  /marketing/senders/{id}:
    get:
      tags: [Campaigns]
      summary: Get a sender
      description: |
        One sender from the Zocton Dashboard. Editing, pausing, archiving,
        testing and deleting a sender happen only there — `PATCH`, `PUT`,
        `POST` and `DELETE` here answer 410.
      operationId: getSender
      parameters:
        - $ref: "#/components/parameters/Project"
        - { name: id, in: path, required: true, schema: { type: string }, description: The sender's id (List senders). }
      responses:
        "200": { description: "`{ sender }`." }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
  /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 Entayo CRM API keys (`entayo_…`) — name, prefix, last used. Never
        the secret. Call it with an `entayo_…` key: a Zocton key (`zk_…`) gets
        403 `forbidden_for_api_key` — Zocton keys are listed and revoked on the
        Zocton Dashboard.
      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" }
        "403": { $ref: "#/components/responses/Forbidden" }
  /api-keys/{id}:
    delete:
      tags: [API keys]
      summary: Revoke an API key
      description: |
        An Entayo CRM API key (`entayo_…`) stops working at once. Cannot be
        undone. Not for a Zocton key (`zk_…` → 403 `forbidden_for_api_key`).
      operationId: revokeApiKey
      parameters: [{ $ref: "#/components/parameters/Id" }]
      responses:
        "200": { $ref: "#/components/responses/Success" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }

components:
  securitySchemes:
    apiKey:
      type: http
      scheme: bearer
      bearerFormat: "zk_… or entayo_…"
      description: "A Zocton API key (`zk_…`, Zocton Dashboard → API Keys, with access to Entayo CRM) or an Entayo CRM API key (`entayo_…`, Entayo CRM → Settings → API Keys), sent as `Authorization: Bearer <key>`."
  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](/developers/entayo-crm/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, or for this key. `code` says which:
        `insufficient_scope` (a read-only Zocton key on a change),
        `project_not_allowed` (a Zocton key made for other projects),
        `forbidden_for_api_key` (a route a Zocton key never reaches).
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: This key is read-only for the CRM, code: insufficient_scope }
    Gone:
      description: Not done here any more — `manage_href` says where (the Zocton Dashboard).
      content:
        application/json:
          schema:
            type: object
            properties:
              error: { type: string }
              manage_href: { type: string }
          example: { error: Senders are managed in the Zocton Dashboard., manage_href: /dashboard/senders }
    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 }
