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

# Handler for SQL CREATE MATERIALIZED VIEW statements

> POST /apps/:app_id/views/sql
Body: { "sql": "CREATE MATERIALIZED VIEW app::view AS SELECT ... FROM app::collection WHERE ..." }
Headers: X-App-Key (required with Admin permission)

The SQL must use the "app::collection" format for table references.
This handler translates the SQL to JSON and calls the existing create_materialized_view handler.



## OpenAPI

````yaml /openapi.yaml post /apps/{app_id}/views/sql
openapi: 3.1.0
info:
  title: OnDB API
  description: Monetize anything
  license:
    name: ''
  version: 4.0.0
servers: []
security: []
tags:
  - name: Health
    description: Health check and system status
  - name: Applications
    description: Application management
  - name: Billing
    description: Billing information and usage
  - name: Pricing
    description: Pricing information and quotes
  - name: X402 Payment Protocol
    description: Payment verification and settlement
  - name: Data Storage
    description: Store data to Celestia blockchain
  - name: Data Query
    description: Query and retrieve data
  - name: Collections
    description: Collection management
  - name: Indexes
    description: Index management and relations
  - name: Retention
    description: Data retention policies
  - name: Materialized Views
    description: Materialized view management
  - name: Predefined Queries
    description: Create and execute predefined queries
  - name: View Query
    description: Query materialized view data
  - name: Wallet
    description: App wallet management
  - name: Tasks
    description: Task tracking and status
  - name: Transactions
    description: Transaction status
  - name: Authorization
    description: Authorization grant management
  - name: API Keys
    description: API key management
  - name: Encryption
    description: Encryption settings and configuration
  - name: Payout
    description: Payout settings and history
  - name: API Collections
    description: API collection management
paths:
  /apps/{app_id}/views/sql:
    post:
      tags:
        - Materialized Views
      summary: Handler for SQL CREATE MATERIALIZED VIEW statements
      description: >-
        POST /apps/:app_id/views/sql

        Body: { "sql": "CREATE MATERIALIZED VIEW app::view AS SELECT ... FROM
        app::collection WHERE ..." }

        Headers: X-App-Key (required with Admin permission)


        The SQL must use the "app::collection" format for table references.

        This handler translates the SQL to JSON and calls the existing
        create_materialized_view handler.
      operationId: handle_sql_create_view
      parameters:
        - name: app_id
          in: path
          description: Application ID
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SqlCreateViewRequest'
        required: true
      responses:
        '200':
          description: View created successfully
          content:
            application/json:
              schema: {}
        '400':
          description: Invalid SQL
        '401':
          description: Unauthorized
        '500':
          description: Internal server error
      security:
        - api_key: []
components:
  schemas:
    SqlCreateViewRequest:
      type: object
      description: SQL CREATE MATERIALIZED VIEW Request
      required:
        - sql
      properties:
        refresh_mode:
          type:
            - string
            - 'null'
          description: >-
            Optional refresh mode: "live" (auto-refresh on write) or "lazy"
            (manual refresh)

            Defaults to "lazy"
        sql:
          type: string
          description: >-
            SQL CREATE MATERIALIZED VIEW statement

            Format: CREATE MATERIALIZED VIEW app::view_name AS SELECT ... FROM
            app::collection WHERE ... GROUP BY ...

            Example: "CREATE MATERIALIZED VIEW myapp::daily_summary AS SELECT
            market, SUM(volume) as total FROM myapp::trades GROUP BY market"

````