{
  "openapi": "3.1.0",
  "info": {
    "title": "agentCRM API",
    "version": "0.3.0",
    "description": "A CRM whose data plane is a hosted Postgres database. Agents either POST SQL directly or use the typed REST surface for routine CRUD on built-in and custom tables.",
    "x-llms-txt": "https://www.tryagentcrm.com/llms.txt"
  },
  "servers": [
    {
      "url": "https://www.tryagentcrm.com/v1"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/{table}": {
      "parameters": [
        {
          "name": "table",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Table name (e.g. contacts, companies, deals, emails, or a custom table)."
        }
      ],
      "get": {
        "summary": "List rows in a table",
        "description": "Filter via query params: ?col=v (eq), ?col=v1,v2 (any), ?col_after=ISO, ?col_before=ISO, ?col_contains=substr, ?col_starts=prefix, ?col_present=true|false, ?col_has=v (array containment), ?custom.key=v (jsonb). Soft-delete: default excludes; ?include_deleted=true or ?only_deleted=true override. Pagination is keyset on (updated_at desc, id desc); pass ?cursor= back from _meta.next_cursor.",
        "responses": {
          "200": {
            "$ref": "#/components/responses/ListOk"
          },
          "400": {
            "description": "Invalid filter / unknown column"
          },
          "404": {
            "description": "Table not in this workspace"
          }
        }
      },
      "post": {
        "summary": "Create a row",
        "description": "Body is a JSON object of column values. id/created_at/updated_at/deleted_at are server-set; client values silently dropped. On UNIQUE conflict returns 409 with the existing row when we can find it (e.g. contacts.email, companies.domain).",
        "requestBody": {
          "required": true
        },
        "responses": {
          "201": {
            "$ref": "#/components/responses/RowOk"
          },
          "400": {
            "description": "Validation error"
          },
          "403": {
            "description": "Read-only key"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/{table}/{id}": {
      "parameters": [
        {
          "name": "table",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Fetch a single row",
        "description": "Built-ins expand relations: contacts include company; deals include contact + company; companies include contact_count and deal_count. Soft-deleted rows are returned (so they can be restored).",
        "responses": {
          "200": {
            "$ref": "#/components/responses/RowOk"
          },
          "404": {
            "description": "Row not found"
          }
        }
      },
      "patch": {
        "summary": "Partial update",
        "description": "Body is a JSON object of column values to change. jsonb columns are merged (existing || body), not replaced. updated_at is set automatically.",
        "requestBody": {
          "required": true
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/RowOk"
          },
          "400": {
            "description": "Validation error"
          },
          "403": {
            "description": "Read-only key"
          },
          "404": {
            "description": "Row not found"
          }
        }
      },
      "delete": {
        "summary": "Delete a row",
        "description": "If the table has a deleted_at column (built-ins do), this is a soft delete by default. ?hard=true performs a real DELETE (the activity entry captures the row's last state).",
        "responses": {
          "200": {
            "$ref": "#/components/responses/RowOk"
          },
          "403": {
            "description": "Read-only key"
          },
          "404": {
            "description": "Row not found"
          },
          "409": {
            "description": "Already soft-deleted"
          }
        }
      }
    },
    "/{table}/{id}/restore": {
      "parameters": [
        {
          "name": "table",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Un-soft-delete a row",
        "responses": {
          "200": {
            "$ref": "#/components/responses/RowOk"
          },
          "400": {
            "description": "Table has no deleted_at column"
          },
          "404": {
            "description": "Row not found"
          },
          "409": {
            "description": "Row is not soft-deleted"
          }
        }
      }
    },
    "/{table}/search": {
      "parameters": [
        {
          "name": "table",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "q",
          "in": "query",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Fuzzy search a table",
        "description": "Built-ins blend trgm similarity across natural columns (contacts: name + email; companies: name + domain; deals: name; emails: subject + snippet).",
        "responses": {
          "200": {
            "$ref": "#/components/responses/ListOk"
          },
          "400": {
            "description": "Search blend not defined for this table"
          }
        }
      }
    },
    "/tables": {
      "get": {
        "summary": "List all tables in this workspace",
        "responses": {
          "200": {
            "description": "Tables with column counts."
          }
        }
      }
    },
    "/schema": {
      "get": {
        "summary": "Full schema graph (tables, columns, FKs, indexes)",
        "description": "Fetch once per session to compose joins via /sql with confidence.",
        "responses": {
          "200": {
            "description": "Tables array with columns/foreign_keys/indexes."
          }
        }
      }
    },
    "/activity": {
      "get": {
        "summary": "Activity feed",
        "description": "Stream of created/updated/deleted/restored/conflict rows from triggers on built-ins (and any custom table that opts in). Filters: ?since=ISO, ?until=ISO, ?subject_type=contact|company|deal|email|list|list_member|<custom>, ?subject_id=<id>, ?actor=api_key:<id>, ?verb=...",
        "responses": {
          "200": {
            "description": "Activity rows + next_cursor."
          }
        }
      }
    },
    "/lists/{id}/rows": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "Resolve a saved list to rows",
        "description": "Composes the list's stored predicate, joins, and column projection into one query and returns the rows. Supports ?col=v filters (target columns only) on top of the stored predicate, plus ?limit=, ?offset=, ?include_deleted=, ?only_deleted=.",
        "responses": {
          "200": {
            "description": "List rows; each is an object keyed by configured column key, with _id always present."
          },
          "404": {
            "description": "List not found"
          },
          "409": {
            "description": "Target table referenced by the list no longer exists"
          }
        }
      }
    },
    "/lists/{id}/members": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "List raw membership of a static list",
        "responses": {
          "200": {
            "description": "list_members rows."
          }
        }
      },
      "post": {
        "summary": "Add a member to a static list",
        "description": "Body: { member_id: string, custom?: object }. 409 on duplicate. Rejected for dynamic lists.",
        "requestBody": {
          "required": true
        },
        "responses": {
          "201": {
            "$ref": "#/components/responses/RowOk"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/lists/{id}/members/{member_id}": {
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        },
        {
          "name": "member_id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "delete": {
        "summary": "Remove a member from a static list",
        "responses": {
          "200": {
            "$ref": "#/components/responses/RowOk"
          },
          "404": {
            "description": "Member not in list"
          }
        }
      }
    },
    "/keys/rotate": {
      "post": {
        "summary": "Rotate live keys for a workspace",
        "description": "Clerk session required. Mints a new live key and revokes all existing non-revoked live keys for the workspace.",
        "security": [],
        "parameters": [
          {
            "name": "ws",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "New live key minted."
          }
        }
      }
    },
    "/connection": {
      "post": {
        "summary": "Mint a direct Postgres connection URL",
        "description": "Clerk session required; claimed workspaces only. Resets the LOGIN PASSWORD on the tenant role and returns a fresh connection URL.",
        "security": [],
        "parameters": [
          {
            "name": "ws",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "Fresh connection URL."
          }
        }
      }
    },
    "/members": {
      "get": {
        "summary": "List workspace members",
        "description": "Clerk session required. Returns each member's user id, email, name, role, and join date.",
        "security": [],
        "parameters": [
          {
            "name": "ws",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Members + your role."
          },
          "403": {
            "description": "Not a member of this workspace."
          }
        }
      },
      "delete": {
        "summary": "Remove a member (owner only)",
        "description": "Removes the member and revokes all of their live keys for this workspace. Refuses to remove the last remaining owner.",
        "security": [],
        "parameters": [
          {
            "name": "ws",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "user_id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Member removed; their live keys revoked."
          },
          "403": {
            "description": "Forbidden — owners only."
          },
          "404": {
            "description": "User is not in this workspace."
          },
          "409": {
            "description": "Last-owner protection — promote someone first."
          }
        }
      }
    },
    "/members/invite": {
      "post": {
        "summary": "Invite a user to the workspace (owner only)",
        "description": "Mints a single-use 7-day token. Returns accept_url for the invitee. Email + role go on the invite; on accept the user's email must match.",
        "security": [],
        "requestBody": {
          "required": true
        },
        "responses": {
          "201": {
            "description": "Invite created; share accept_url with the invitee."
          },
          "400": {
            "description": "Invalid email or role."
          },
          "403": {
            "description": "Caller is not an owner."
          }
        }
      }
    },
    "/members/accept/{token}": {
      "parameters": [
        {
          "name": "token",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "post": {
        "summary": "Accept an invite (signed-in user)",
        "description": "Creates membership and mints a user-tied live key. Token must be unused, unexpired, unrevoked, and the Clerk user's email must match the invited email.",
        "security": [],
        "responses": {
          "201": {
            "description": "Membership created + live key minted."
          },
          "401": {
            "description": "Not signed in."
          },
          "403": {
            "description": "Email mismatch."
          },
          "404": {
            "description": "Token not found."
          },
          "409": {
            "description": "Invite already accepted or user already a member."
          },
          "410": {
            "description": "Invite expired or revoked."
          }
        }
      }
    },
    "/workspaces": {
      "post": {
        "summary": "Create a workspace",
        "description": "Anonymous (no Clerk session): returns a dev key with a 7-day TTL and a claim_url. Authenticated (Clerk session cookie present): claims a workspace immediately, returns a live key, and inserts the caller as the owner.",
        "security": [],
        "requestBody": {
          "required": false
        },
        "responses": {
          "201": {
            "$ref": "#/components/responses/CreateWorkspace"
          },
          "429": {
            "description": "Rate limited"
          },
          "503": {
            "description": "Warm pool empty; retry shortly"
          }
        }
      }
    },
    "/sql": {
      "post": {
        "summary": "Execute SQL on the workspace's Postgres schema",
        "description": "Runs the supplied SQL inside a transaction as the workspace's per-tenant role. DDL and DML both allowed. Demoted dev keys (post-claim) execute as read-only.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SqlRequest"
              },
              "examples": {
                "insert": {
                  "summary": "Insert a contact",
                  "value": {
                    "sql": "INSERT INTO contacts (email, first_name) VALUES ($1, $2) RETURNING id",
                    "params": [
                      "sarah@acme.com",
                      "Sarah"
                    ]
                  }
                },
                "select": {
                  "summary": "Select with JOIN",
                  "value": {
                    "sql": "SELECT c.email, co.name AS company FROM contacts c LEFT JOIN companies co ON co.id=c.company_id WHERE c.first_name = $1",
                    "params": [
                      "Sarah"
                    ]
                  }
                },
                "ddl": {
                  "summary": "Create a custom table",
                  "value": {
                    "sql": "CREATE TABLE projects (id text PRIMARY KEY DEFAULT 'p_' || encode(gen_random_bytes(8),'hex'), name text NOT NULL, status text, due_date date, custom jsonb DEFAULT '{}')"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/SqlOk"
          },
          "400": {
            "$ref": "#/components/responses/SqlError"
          },
          "401": {
            "description": "Missing or invalid bearer token"
          },
          "413": {
            "description": "Body too large (max 256KB)"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/claim/{ws_id}": {
      "parameters": [
        {
          "name": "ws_id",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string",
            "pattern": "^ws_[a-z0-9_]+$"
          }
        }
      ],
      "post": {
        "summary": "Claim a workspace",
        "description": "Requires a Clerk session. Marks the workspace as claimed, inserts caller as owner, mints a live key, and demotes any existing dev keys to read-only for 7 days, then revokes them at 14 days.",
        "security": [],
        "responses": {
          "201": {
            "$ref": "#/components/responses/ClaimOk"
          },
          "401": {
            "description": "Not signed in"
          },
          "404": {
            "description": "Workspace not found"
          },
          "409": {
            "description": "Already claimed (or claimed by you)"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "agentCRM api key (acrm_dev_… | acrm_live_…)"
      }
    },
    "schemas": {
      "SqlRequest": {
        "type": "object",
        "required": [
          "sql"
        ],
        "properties": {
          "sql": {
            "type": "string",
            "description": "Postgres SQL. Use $1, $2, … for bind parameters."
          },
          "params": {
            "type": "array",
            "items": {},
            "description": "Bind values matched to $1, $2, … in the SQL."
          }
        }
      }
    },
    "responses": {
      "ListOk": {
        "description": "List of rows.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "data": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": true
                  }
                },
                "_meta": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        }
      },
      "RowOk": {
        "description": "Single row.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "data": {
                  "type": "object",
                  "additionalProperties": true
                },
                "_meta": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        }
      },
      "Conflict": {
        "description": "UNIQUE collision. `existing` is the row that already holds the value when we can resolve it.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "enum": [
                    "conflict"
                  ]
                },
                "message": {
                  "type": "string"
                },
                "existing": {
                  "type": [
                    "object",
                    "null"
                  ],
                  "additionalProperties": true
                }
              }
            }
          }
        }
      },
      "CreateWorkspace": {
        "description": "A new workspace. Anonymous returns dev key + claim_url; authed returns live key.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "data": {
                  "type": "object",
                  "properties": {
                    "workspace_id": {
                      "type": "string"
                    },
                    "api_key": {
                      "type": "string"
                    },
                    "api_base": {
                      "type": "string"
                    },
                    "claim_url": {
                      "type": "string",
                      "description": "Anonymous only."
                    },
                    "ttl_expires_at": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Anonymous only."
                    }
                  }
                },
                "_meta": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        }
      },
      "SqlOk": {
        "description": "Successful query.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "data": {
                  "type": "object",
                  "properties": {
                    "columns": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "rows": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "additionalProperties": true
                      }
                    },
                    "rowCount": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    },
                    "command": {
                      "type": "string",
                      "description": "SELECT | INSERT | UPDATE | DELETE | CREATE | …"
                    }
                  }
                },
                "_meta": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        }
      },
      "SqlError": {
        "description": "Postgres SQL error. Read code/hint/position to self-correct.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "enum": [
                    "sql_error"
                  ]
                },
                "message": {
                  "type": "string"
                },
                "code": {
                  "type": "string",
                  "description": "Postgres SQLSTATE"
                },
                "hint": {
                  "type": "string"
                },
                "position": {
                  "type": "string"
                },
                "docs_url": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "ClaimOk": {
        "description": "Workspace claimed; live key minted, dev key demoted.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "data": {
                  "type": "object",
                  "properties": {
                    "workspace_id": {
                      "type": "string"
                    },
                    "api_key": {
                      "type": "string",
                      "description": "acrm_live_*"
                    },
                    "api_base": {
                      "type": "string"
                    }
                  }
                },
                "_meta": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          }
        }
      }
    }
  }
}