> ## Documentation Index
> Fetch the complete documentation index at: https://docs.snackbase.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Collection

> Create a new collection with custom schema.

Creates an Alembic migration and applies it to create the physical table.
Only superadmins can create collections.



## OpenAPI

````yaml post /api/v1/collections
openapi: 3.1.0
info:
  title: SnackBase
  description: Open-source Backend-as-a-Service (BaaS)
  version: 0.1.0
servers: []
security: []
paths:
  /api/v1/collections:
    post:
      tags:
        - collections
      summary: Create Collection
      description: >-
        Create a new collection with custom schema.


        Creates an Alembic migration and applies it to create the physical
        table.

        Only superadmins can create collections.
      operationId: create_collection_api_v1_collections_post
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateCollectionRequest'
      responses:
        '201':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CollectionResponse'
        '400':
          description: Validation error
        '403':
          description: Superadmin access required
        '409':
          description: Collection already exists
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    CreateCollectionRequest:
      properties:
        name:
          type: string
          maxLength: 64
          minLength: 3
          title: Name
          description: Collection name (3-64 chars, alphanumeric + underscores)
        schema:
          items:
            $ref: '#/components/schemas/FieldDefinition'
          type: array
          minItems: 1
          title: Schema
          description: List of field definitions (at least one required)
        list_rule:
          anyOf:
            - type: string
            - type: 'null'
          title: List Rule
          description: Filter expression for listing records
        view_rule:
          anyOf:
            - type: string
            - type: 'null'
          title: View Rule
          description: Filter expression for viewing records
        create_rule:
          anyOf:
            - type: string
            - type: 'null'
          title: Create Rule
          description: Validation expression for creating records
        update_rule:
          anyOf:
            - type: string
            - type: 'null'
          title: Update Rule
          description: Filter/validation expression for updates
        delete_rule:
          anyOf:
            - type: string
            - type: 'null'
          title: Delete Rule
          description: Filter expression for deletions
        list_fields:
          type: string
          title: List Fields
          description: Fields visible in list operations
          default: '*'
        view_fields:
          type: string
          title: View Fields
          description: Fields visible in view operations
          default: '*'
        create_fields:
          type: string
          title: Create Fields
          description: Fields allowed in create requests
          default: '*'
        update_fields:
          type: string
          title: Update Fields
          description: Fields allowed in update requests
          default: '*'
      type: object
      required:
        - name
        - schema
      title: CreateCollectionRequest
      description: Request body for creating a new collection.
    CollectionResponse:
      properties:
        id:
          type: string
          title: Id
          description: Collection ID (UUID)
        name:
          type: string
          title: Name
          description: Collection name
        table_name:
          type: string
          title: Table Name
          description: Physical table name in database
        schema:
          items:
            $ref: '#/components/schemas/SchemaFieldResponse'
          type: array
          title: Schema
          description: Collection schema
        created_at:
          type: string
          format: date-time
          title: Created At
          description: When the collection was created
        updated_at:
          type: string
          format: date-time
          title: Updated At
          description: When the collection was last updated
      type: object
      required:
        - id
        - name
        - table_name
        - schema
        - created_at
        - updated_at
      title: CollectionResponse
      description: Response for a created collection.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    FieldDefinition:
      properties:
        name:
          type: string
          maxLength: 64
          minLength: 1
          title: Name
          description: Field name (alphanumeric + underscores, starts with letter)
        type:
          type: string
          title: Type
          description: >-
            Field type: text, number, boolean, datetime, email, url, json,
            reference, file, date
        required:
          type: boolean
          title: Required
          description: Whether the field is required
          default: false
        default:
          title: Default
          description: Default value for the field
        unique:
          type: boolean
          title: Unique
          description: Whether the field value must be unique
          default: false
        options:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Options
          description: Additional field options
        collection:
          anyOf:
            - type: string
            - type: 'null'
          title: Collection
          description: Target collection name (required for reference type)
        on_delete:
          anyOf:
            - type: string
            - type: 'null'
          title: On Delete
          description: >-
            On delete action: cascade, set_null, restrict (required for
            reference type)
        pii:
          type: boolean
          title: Pii
          description: Whether this field contains PII data
          default: false
        mask_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Mask Type
          description: 'Mask type for PII fields: email, ssn, phone, name, full, custom'
        expression:
          anyOf:
            - type: string
            - type: 'null'
          title: Expression
          description: >-
            SQL expression for computed fields (e.g., 'concat(first_name,
            last_name)')
        return_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Return Type
          description: 'Return type for computed fields: text, number, boolean, datetime'
      type: object
      required:
        - name
        - type
      title: FieldDefinition
      description: Definition of a single field in a collection schema.
    SchemaFieldResponse:
      properties:
        name:
          type: string
          title: Name
        type:
          type: string
          title: Type
        required:
          type: boolean
          title: Required
          default: false
        default:
          title: Default
        unique:
          type: boolean
          title: Unique
          default: false
        options:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Options
        collection:
          anyOf:
            - type: string
            - type: 'null'
          title: Collection
        on_delete:
          anyOf:
            - type: string
            - type: 'null'
          title: On Delete
        pii:
          type: boolean
          title: Pii
          default: false
        mask_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Mask Type
        expression:
          anyOf:
            - type: string
            - type: 'null'
          title: Expression
        return_type:
          anyOf:
            - type: string
            - type: 'null'
          title: Return Type
      type: object
      required:
        - name
        - type
      title: SchemaFieldResponse
      description: Schema field in collection response.
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````