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

# Create Model

> Create a new model



## OpenAPI

````yaml https://api.hightouch.io/api/swagger.json post /models
openapi: 3.0.0
info:
  title: Hightouch API
  version: 1.0.0
  description: Hightouch Public Rest API to access syncs, models, sources and destinations
  contact:
    name: Hightouch
    url: https://hightouch.com
servers:
  - url: https://api.hightouch.com/api/v1
security: []
paths:
  /models:
    post:
      summary: Create Model
      description: Create a new model
      operationId: CreateModel
      parameters:
        - in: query
          name: skipColumnQuery
          required: false
          schema:
            default: false
            type: boolean
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ModelCreate'
            examples:
              Example 1:
                value:
                  name: testModel3
                  slug: testModel3
                  description: Users with their last names
                  primaryKey: id
                  sourceId: 1
                  queryType: raw_sql
                  isSchema: false
                  raw:
                    sql: select id, last_name from users
              Example 2:
                value:
                  name: West coast fans
                  slug: west-coast-fans
                  description: Fans located in CA, OR, or WA
                  primaryKey: id
                  sourceId: 1
                  queryType: visual
                  isSchema: false
                  visual:
                    parentId: 2
                    filter:
                      conditions: []
                  spaceId: 3f2b1c8e-6d4a-4f0e-9b7c-2a1d5e8f0c3b
                  rowLevelAccessRuleIds:
                    - 7c0e7f1d-2a3b-4c5d-8e9f-0a1b2c3d4e5f
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                anyOf:
                  - $ref: '#/components/schemas/Model'
                  - $ref: '#/components/schemas/ValidateErrorJSON'
                  - $ref: '#/components/schemas/InternalServerError'
              examples:
                Example 1:
                  value:
                    id: 3
                    name: testModel3
                    slug: testModel3
                    description: Users with their last names
                    workspaceId: 1
                    primaryKey: id
                    createdAt: '2022-02-16T21:37:58.510Z'
                    updatedAt: '2022-02-16T21:37:58.510Z'
                    createdBy:
                      id: 1
                      name: Jane Doe
                      email: jane.doe@example.com
                    updatedBy:
                      id: 1
                      name: Jane Doe
                      email: jane.doe@example.com
                    sourceId: 1
                    queryType: raw_sql
                    tags: {}
                    isSchema: false
                    isGeneralModel: false
                    raw:
                      sql: select id, last_name from users
                    syncs: []
                    lastRunSize: null
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '409':
          description: Conflict
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidateErrorJSON'
        '422':
          description: Validation Failed
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidateErrorJSON'
        '500':
          description: Something went wrong
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InternalServerError'
      security:
        - bearerAuth: []
components:
  schemas:
    ModelCreate:
      description: The input for creating a Model
      properties:
        name:
          type: string
          description: The name of the model
        slug:
          type: string
          description: The slug of the model
        queryType:
          type: string
          description: >-
            The type of the query. Available options: custom, raw_sql, tabel,
            dbt and visual.
        sourceId:
          type: number
          format: double
          description: The id of the source that model is connected to
        isSchema:
          type: boolean
          description: |-
            If is_schema is true, the model is just used to build other models.
            Either as part of visual querying, or as the root of a visual query.
        visual:
          properties:
            secondaryLabel:
              type: string
            primaryLabel:
              type: string
            filter: {}
            parentId:
              type: number
              format: double
              description: Parent id of the schema that visual query is based on
          required:
            - filter
            - parentId
          type: object
          description: Visual query, used by audience
        custom:
          properties:
            query: {}
          required:
            - query
          type: object
          description: >-
            Custom query for sources that doesn't support sql. For example,
            Airtable.
        table:
          properties:
            name:
              type: string
          required:
            - name
          type: object
          description: Table-based query that fetches on a table instead of SQL
        raw:
          properties:
            sql:
              type: string
          required:
            - sql
          type: object
          description: Standard raw SQL query
        folderId:
          type: string
        spaceId:
          type: string
          description: >-
            The Space this audience belongs to. Applies only to workspaces with
            Spaces enabled;

            contact Hightouch to enable Spaces and to obtain Space ids.

            Only valid when queryType is "visual". Omitted for models that are
            not in a Space.

            An API key scoped to a Space must set it on create. Read-only after
            creation.
        rowLevelAccessRuleIds:
          items:
            type: string
          type: array
          description: >-
            Row-level access rules attached to this audience. Applies only to
            workspaces with

            Spaces enabled. Only valid when queryType is "visual"; always
            present on an audience.

            When creating an audience in a Space, include one rule from each
            required row-level

            access group on the parent model. Rule ids come from the Spaces
            governance API.

            Read-only after creation.
        primaryKey:
          type: string
          description: >-
            The primary key will be null if the query doesn't get directly
            synced (e.g. a relationship table for visual querying).

            Optional here but actually conditionally required: every model
            except a

            general model must supply one.
        isGeneralModel:
          type: boolean
          description: >-
            Create a general model: a model that exposes warehouse data to
            Hightouch AI

            agents instead of being synced. Takes no primaryKey and no folderId,
            and

            cannot be a schema model, and only supports certain query types.
        description:
          type: string
          description: >-
            The description of the model. On a general model this is the context
            AI

            agents read to decide when to use it.
        dbt:
          properties:
            modelId:
              type: number
              format: double
              description: Model id that refers to a dbt model
          required:
            - modelId
          type: object
        dbt_cloud:
          properties:
            modelId:
              type: number
              format: double
              description: Model id that refers to a dbt Cloud model
          required:
            - modelId
          type: object
      required:
        - name
        - slug
        - queryType
        - sourceId
        - isSchema
      type: object
      additionalProperties: false
    Model:
      description: >-
        The SQL query that pulls data from your source to send to your
        destination.

        We send your SQL query directly to your source so any SQL that is valid
        for your source (including functions) is valid in Hightouch.
      properties:
        id:
          type: number
          format: double
          description: The id of the model
        name:
          type: string
          description: The name of the model
        slug:
          type: string
          description: The slug of the model
        description:
          type: string
          nullable: true
          description: The description of the model. Null when no description has been set.
        workspaceId:
          type: number
          format: double
          description: The id of the workspace where the model belongs to
        primaryKey:
          type: string
          description: >-
            The primary key will be null if the query doesn't get directly
            synced (e.g. a relationship table for visual querying)
        createdAt:
          type: string
          format: date-time
          description: The timestamp when model was created
        updatedAt:
          type: string
          format: date-time
          description: The timestamp when model was lastly updated
        createdBy:
          allOf:
            - $ref: '#/components/schemas/UserReference'
          nullable: true
          description: The user who created the model, or null if no user was recorded
        updatedBy:
          allOf:
            - $ref: '#/components/schemas/UserReference'
          nullable: true
          description: The user who last updated the model, or null if no user was recorded
        sourceId:
          type: number
          format: double
          description: The id of the source that model is connected to
        queryType:
          type: string
          description: >-
            The type of the query. Available options: custom, raw_sql, tabel,
            dbt and visual.
        tags:
          properties: {}
          additionalProperties:
            type: string
          type: object
          description: The tags of the model
        isSchema:
          type: boolean
          description: |-
            If is_schema is true, the model is just used to build other models.
            Either as part of visual querying, or as the root of a visual query.
        isGeneralModel:
          type: boolean
          description: >-
            If is_general_model is true, the model is not synced. It exposes
            warehouse

            data to Hightouch AI agents and is managed from the schema section.
        syncs:
          items:
            type: number
            format: double
          type: array
          description: The list of id of syncs that uses this model
        visual:
          properties:
            secondaryLabel:
              type: string
            primaryLabel:
              type: string
            filter: {}
            parentId:
              type: number
              format: double
              description: Parent id of the schema that visual query is based on
          required:
            - filter
            - parentId
          type: object
          description: Visual query, used by audience
        custom:
          properties:
            query: {}
          required:
            - query
          type: object
          description: >-
            Custom query for sources that doesn't support sql. For example,
            Airtable.
        table:
          properties:
            name:
              type: string
          required:
            - name
          type: object
          description: Table-based query that fetches on a table instead of SQL
        dbt:
          properties:
            dbtUniqueId:
              type: string
              description: >-
                Unique ID of the model assigned by dbt (usually some combination
                of the schema and table name)
            compiledSql:
              type: string
              description: Compiled SQL in the dbt model
            rawSql:
              type: string
              description: Raw SQL in the dbt model
            database:
              type: string
              description: Name of the database containing the generated table
            schema:
              type: string
              description: Name of the schema containing the generated table
            name:
              type: string
              description: Name of the table generated by the dbt model
            modelId:
              type: number
              format: double
              description: Model id that refer to a dbt model
          required:
            - dbtUniqueId
            - compiledSql
            - rawSql
            - database
            - schema
            - name
            - modelId
          type: object
          description: Query that is based on a dbt model
        dbt_cloud:
          properties:
            dbtUniqueId:
              type: string
              description: >-
                Unique ID of the model assigned by dbt (usually some combination
                of the schema and table name)
            compiledSql:
              type: string
              description: Compiled SQL in the dbt model
            rawSql:
              type: string
            name:
              type: string
              description: Name of the table generated by the dbt model
            modelId:
              type: number
              format: double
              description: Model id that refer to a dbt model
          required:
            - dbtUniqueId
            - compiledSql
            - rawSql
            - name
            - modelId
          type: object
        raw:
          properties:
            sql:
              type: string
          required:
            - sql
          type: object
          description: Standard raw SQL query
        folderId:
          type: string
        lastRunSize:
          type: number
          format: double
          nullable: true
          description: >-
            The number of rows returned by the most recent successful sync run
            of this model.

            Null if the model has never been synced successfully.
        spaceId:
          type: string
          description: >-
            The Space this audience belongs to. Applies only to workspaces with
            Spaces enabled;

            contact Hightouch to enable Spaces and to obtain Space ids.

            Only valid when queryType is "visual". Omitted for models that are
            not in a Space.

            An API key scoped to a Space must set it on create. Read-only after
            creation.
        rowLevelAccessRuleIds:
          items:
            type: string
          type: array
          description: >-
            Row-level access rules attached to this audience. Applies only to
            workspaces with

            Spaces enabled. Only valid when queryType is "visual"; always
            present on an audience.

            When creating an audience in a Space, include one rule from each
            required row-level

            access group on the parent model. Rule ids come from the Spaces
            governance API.

            Read-only after creation.
      required:
        - id
        - name
        - slug
        - description
        - workspaceId
        - primaryKey
        - createdAt
        - updatedAt
        - createdBy
        - updatedBy
        - sourceId
        - queryType
        - tags
        - isSchema
        - isGeneralModel
        - syncs
        - lastRunSize
      type: object
      additionalProperties: false
    ValidateErrorJSON:
      properties:
        message:
          type: string
          enum:
            - Validation failed
          nullable: false
        details:
          properties: {}
          additionalProperties: {}
          type: object
      required:
        - message
        - details
      type: object
      additionalProperties: false
    InternalServerError:
      type: string
      enum:
        - Internal Server Error
      nullable: false
    UserReference:
      description: >-
        The Hightouch user who created or last updated a resource. Changes made
        with

        an API key are attributed to the user who created the key.
      properties:
        id:
          type: number
          format: double
          description: The user's id
        name:
          type: string
          description: The user's name
        email:
          type: string
          description: The user's email address
      required:
        - id
        - name
        - email
      type: object
      additionalProperties: false
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: Bearer

````