# Upsert Row

`POST /api/v1/tables/{tableId}/rows/upsert`

## Description
Insert a new row or update an existing one based on a unique column value. The table must have at least one column with a unique constraint. If a row with a matching unique value exists, it is updated; otherwise, a new row is inserted. When multiple unique columns exist, specify `conflictTarget` to indicate which column to match on.

## Parameters

- **tableId** (path) (required) — `string`: The unique identifier of the table.

## Request Body

```json
{
  "type": "object",
  "required": [
    "workspaceId",
    "data"
  ],
  "properties": {
    "workspaceId": {
      "type": "string",
      "description": "The workspace ID."
    },
    "data": {
      "type": "object",
      "additionalProperties": true,
      "description": "Row data. Must include a value for the conflict target column."
    },
    "conflictTarget": {
      "type": "string",
      "description": "Name of the unique column to match on. Required when the table has multiple unique columns. If the table has exactly one unique column, this can be omitted."
    }
  }
}
```

## Responses

### 200 — Row upserted successfully.

```json
{
  "type": "object",
  "properties": {
    "success": {
      "type": "boolean",
      "description": "Indicates whether the request was successful."
    },
    "data": {
      "type": "object",
      "properties": {
        "row": {
          "type": "object",
          "description": "A single row in a table.",
          "properties": {
            "id": {
              "type": "string",
              "description": "Unique row identifier.",
              "example": "row_xyz789"
            },
            "data": {
              "type": "object",
              "additionalProperties": true,
              "description": "Row data as key-value pairs matching the table schema."
            },
            "position": {
              "type": "integer",
              "description": "Row's position/order in the table."
            },
            "createdAt": {
              "type": "string",
              "format": "date-time",
              "description": "ISO 8601 timestamp when the row was created."
            },
            "updatedAt": {
              "type": "string",
              "format": "date-time",
              "description": "ISO 8601 timestamp when the row was last modified."
            }
          }
        },
        "operation": {
          "type": "string",
          "enum": [
            "insert",
            "update"
          ],
          "description": "Whether the row was inserted or updated."
        },
        "message": {
          "type": "string",
          "description": "Confirmation message."
        }
      },
      "description": "Response payload."
    }
  }
}
```

### 400 — Invalid request parameters. Check the details array for specific validation errors.

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "string",
      "description": "Human-readable error message describing the validation failure."
    },
    "details": {
      "type": "array",
      "description": "List of specific validation errors with field-level details.",
      "items": {
        "type": "object"
      }
    }
  }
}
```

### 401 — Invalid or missing API key. Ensure the X-API-Key header is set with a valid key.

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "string",
      "description": "Human-readable error message."
    }
  }
}
```

### 403 — Access denied. You do not have permission to access this resource. For audit log endpoints, this requires an Enterprise subscription and organization admin/owner role.

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "string",
      "description": "Human-readable error message."
    }
  }
}
```

### 404 — The requested resource was not found. Verify the ID is correct and belongs to your workspace.

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "string",
      "description": "Human-readable error message."
    }
  }
}
```

### 429 — Rate limit exceeded. Wait for the duration specified in the Retry-After header before retrying.

```json
{
  "type": "object",
  "properties": {
    "error": {
      "type": "string",
      "description": "Human-readable error message with rate limit details."
    }
  }
}
```
