# Create Table

`POST /api/v1/tables`

## Description
Create a new table in a workspace. Define the table schema with typed columns, optional constraints (required, unique), and a name.

## Request Body

```json
{
  "type": "object",
  "required": [
    "workspaceId",
    "name",
    "schema"
  ],
  "properties": {
    "workspaceId": {
      "type": "string",
      "description": "The workspace to create the table in."
    },
    "name": {
      "type": "string",
      "description": "Table name. Must start with a letter or underscore, alphanumeric and underscores only.",
      "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
    },
    "description": {
      "type": "string",
      "description": "Optional description of the table."
    },
    "schema": {
      "type": "object",
      "required": [
        "columns"
      ],
      "properties": {
        "columns": {
          "type": "array",
          "items": {
            "type": "object",
            "description": "Definition of a table column including its type and constraints.",
            "required": [
              "name",
              "type"
            ],
            "properties": {
              "name": {
                "type": "string",
                "description": "Column name. Must start with a letter or underscore.",
                "example": "email",
                "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"
              },
              "type": {
                "type": "string",
                "enum": [
                  "string",
                  "number",
                  "boolean",
                  "date",
                  "json"
                ],
                "description": "Data type of the column."
              },
              "required": {
                "type": "boolean",
                "description": "Whether the column requires a value on insert.",
                "default": false
              },
              "unique": {
                "type": "boolean",
                "description": "Whether values in this column must be unique across all rows.",
                "default": false
              }
            }
          },
          "minItems": 1,
          "description": "Column definitions for the table."
        }
      },
      "description": "Table schema definition containing column definitions."
    }
  }
}
```

## Responses

### 200 — Table created successfully.

```json
{
  "type": "object",
  "properties": {
    "success": {
      "type": "boolean",
      "description": "Indicates whether the request was successful."
    },
    "data": {
      "type": "object",
      "properties": {
        "table": {
          "type": "object",
          "description": "A user-defined table with a typed schema.",
          "properties": {
            "id": {
              "type": "string",
              "description": "Unique table identifier.",
              "example": "tbl_abc123"
            },
            "name": {
              "type": "string",
              "description": "Table name.",
              "example": "contacts"
            },
            "description": {
              "type": "string",
              "description": "Optional description of the table.",
              "example": "Customer contact records"
            },
            "schema": {
              "type": "object",
              "description": "Table schema definition.",
              "properties": {
                "columns": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/ColumnDefinition"
                  },
                  "description": "Array of column definitions for the table."
                }
              }
            },
            "rowCount": {
              "type": "integer",
              "description": "Current number of rows in the table."
            },
            "maxRows": {
              "type": "integer",
              "description": "Maximum rows allowed by the current billing plan."
            },
            "createdAt": {
              "type": "string",
              "format": "date-time",
              "description": "ISO 8601 timestamp when the table was created."
            },
            "updatedAt": {
              "type": "string",
              "format": "date-time",
              "description": "ISO 8601 timestamp when the table was last modified."
            }
          }
        },
        "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."
    }
  }
}
```

### 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."
    }
  }
}
```
