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

# Create Query

> Creates a new query under the given project. Supports plain and materialized query types. Optionally accepts a path to place the query in a folder hierarchy. A MATERIALIZED query is materialized immediately on create and its result is queryable as `"peaka"."mtquery"."<name>"`; an optional `schedule` keeps it refreshed. To materialize an existing query, send `inputQueryRefId` with the source query's id instead of `inputQuery`: a snapshot of the source SQL is taken at create time and stored as the new query's own `inputQuery` (the source query is left untouched, and later changes to it do not propagate).

<span style={{display: "flex", gap: "10px", flexDirection: "row", alignItems: "center"}}>
  <img src="https://cdn.peaka.com/badges/partner-api-key-badge.png" />

  <img src="https://cdn.peaka.com/badges/project-api-key-badge.png" />
</span>

<Note>
  Looking to materialize an existing query? See [How to Materialize a Query with the Peaka API](/how-to-guides/how-to-materialize-a-query-with-the-peaka-api) for the full workflow — creating a `MATERIALIZED` query from `inputQueryRefId`, scheduling refreshes, and querying the result.
</Note>


## OpenAPI

````yaml post /data/projects/{projectId}/queries
openapi: 3.0.1
info:
  title: Peaka Gateway API
  description: Peaka Gateway API Documentation
  version: '1.0'
servers:
  - url: https://partner.peaka.studio/api/v1
    description: Default Server URL (US Zone)
  - url: https://partner.eu.peaka.studio/api/v1
    description: EU Zone
security:
  - bearerAuth: []
paths:
  /data/projects/{projectId}/queries:
    post:
      tags:
        - Data -- Queries
      summary: Create Query
      description: >-
        Creates a new query under the given project. Supports plain and
        materialized query types. Optionally accepts a path to place the query
        in a folder hierarchy. A MATERIALIZED query is materialized immediately
        on create and its result is queryable as `"peaka"."mtquery"."<name>"`;
        an optional `schedule` keeps it refreshed. To materialize an existing
        query, send `inputQueryRefId` with the source query's id instead of
        `inputQuery`: a snapshot of the source SQL is taken at create time and
        stored as the new query's own `inputQuery` (the source query is left
        untouched, and later changes to it do not propagate).
      operationId: createQuery
      parameters:
        - name: projectId
          in: path
          description: ID of the Project
          required: true
          schema:
            type: string
          example: mtKDhe1U
        - name: checkSchemaExists
          in: query
          required: false
          schema:
            type: boolean
            default: true
      requestBody:
        description: Query Request
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QueryRequest'
            examples:
              Create Query:
                description: Create Query
                value:
                  displayName: sampleQuery
                  inputQuery: SELECT * from "mycatalog".payment.customers
              Create Query in Folder:
                description: Create Query in Folder
                value:
                  displayName: sampleQuery
                  inputQuery: SELECT * from "mycatalog".payment.customers
                  path: /reports/monthly
              Create Materialized Query:
                description: Create Materialized Query
                value:
                  displayName: sampleQuery
                  inputQuery: SELECT * from "mycatalog".payment.customers
                  queryType: MATERIALIZED
              Create Materialized Query (Interval Schedule):
                description: Create Materialized Query (Interval Schedule)
                value:
                  displayName: sampleQuery
                  inputQuery: SELECT * from "mycatalog".payment.customers
                  queryType: MATERIALIZED
                  schedule:
                    type: interval
                    repeatDuration: PT6H
              Create Materialized Query (Cron Schedule):
                description: Create Materialized Query (Cron Schedule)
                value:
                  displayName: sampleQuery
                  inputQuery: SELECT * from "mycatalog".payment.customers
                  queryType: MATERIALIZED
                  schedule:
                    type: cron
                    cronExpression: 0 0 * * *
                    timezone: UTC
              Create Materialized Query from Existing Query:
                description: Create Materialized Query from Existing Query
                value:
                  displayName: sampleQuery
                  queryType: MATERIALIZED
                  inputQueryRefId: '709922802836177297'
              Create Materialized Query from Existing Query (Twice-Daily Schedule):
                description: >-
                  Create Materialized Query from Existing Query (Twice-Daily
                  Schedule)
                value:
                  displayName: sampleQuery
                  queryType: MATERIALIZED
                  inputQueryRefId: '709922802836177297'
                  schedule:
                    type: interval
                    repeatDuration: PT12H
        required: true
      responses:
        '200':
          description: Query created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Query'
components:
  schemas:
    QueryRequest:
      type: object
      properties:
        displayName:
          type: string
          description: The display name of the query.
          example: sampleQuery
        inputQuery:
          type: string
          description: The SQL query. Required unless inputQueryRefId is provided.
          nullable: true
        queryType:
          type: string
          description: The type of the query. Defaults to PLAIN.
          enum:
            - PLAIN
            - MATERIALIZED
          default: PLAIN
        schedule:
          $ref: '#/components/schemas/MaterializedQuerySchedule'
        inputQueryRefId:
          type: string
          description: >-
            The id of an existing query to materialize. Valid only if the query
            type is MATERIALIZED. When set, the materialized query is built from
            the referenced query's SQL and inputQuery is not required. The
            reference is resolved when the request is processed: a snapshot of
            the referenced query's SQL is stored as this query's inputQuery. The
            reference id itself is not persisted and is not returned in
            responses; later changes to the referenced query do not propagate.
            Send the reference again on an update to re-resolve from the source.
          nullable: true
          example: '709922802836177297'
        path:
          type: string
          description: >-
            The folder path to place this query in. Intermediate folders are
            created automatically. If omitted, the query is placed at the root
            level.
          nullable: true
          example: /reports/monthly
    Query:
      type: object
      properties:
        id:
          type: string
          description: The ID of the query.
        displayName:
          type: string
          description: The display name of the query.
        name:
          type: string
          description: >-
            The name of the query used in SQL, e.g. SELECT * FROM
            "peaka"."query"."<name>". For a MATERIALIZED query the materialized
            result is queryable as "peaka"."mtquery"."<name>".
        inputQuery:
          type: string
          description: >-
            The SQL query. For a materialized query created from an existing
            query (via inputQueryRefId), this holds the snapshot of the source
            query's SQL resolved when the query was created or last updated with
            the reference.
        queryType:
          type: string
          description: The type of the query.
          enum:
            - PLAIN
            - MATERIALIZED
        schedule:
          $ref: '#/components/schemas/MaterializedQuerySchedule'
        path:
          type: string
          description: >-
            The folder path of the query (e.g. "/analytics/dashboards"). Null if
            not in any folder.
          nullable: true
        folderId:
          type: string
          description: >-
            The ID of the folder containing this query. Null if not in any
            folder.
          nullable: true
      description: A saved query belonging to a project.
    MaterializedQuerySchedule:
      type: object
      properties:
        type:
          type: string
          description: Type of schedule.
          enum:
            - interval
            - cron
            - none
        cronExpression:
          type: string
          description: >-
            UNIX cron expression (minute hour day-of-month month day-of-week),
            e.g. '0 0 * * *' for every day at midnight. Set only when the
            schedule type is 'cron'.
          nullable: true
          example: 0 0 * * *
        repeatDuration:
          type: string
          description: >-
            ISO-8601 duration between refreshes, e.g. 'PT6H' (every 6 hours),
            'PT12H' (twice daily), 'P1D' (daily). Set only when the schedule
            type is 'interval'.
          nullable: true
          example: PT12H
        timezone:
          type: string
          description: >-
            IANA timezone used to evaluate the cron expression, e.g. 'UTC' or
            'Europe/Istanbul'. Set only when the schedule type is 'cron'.
          nullable: true
          example: UTC
      description: >-
        Schedule configuration of a materialized query. Provide EITHER an
        interval schedule (`type=interval` with `repeatDuration`) OR a cron
        schedule (`type=cron` with `cronExpression` and optional `timezone`).
        Use `type=none` for no schedule. The fields that do not apply to the
        chosen type are omitted.
      nullable: true
      example:
        type: cron
        cronExpression: 0 0 * * *
        timezone: UTC
  securitySchemes:
    bearerAuth:
      type: http
      description: >-
        Use the Authorization header with the value 'Bearer <apiKey>' to
        authenticate. Partner API Keys have full access; Project API Keys are
        limited to their project scope. Learn more:
        https://docs.peaka.com/api-reference/authentication
      scheme: bearer
      bearerFormat: Api Key

````