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

# Create snippet

> Creates a reusable code snippet in the workspace. Snippets are async JavaScript functions executed in a sandboxed V8 isolate with access to cnap.request() for API calls.



## OpenAPI

````yaml /openapi.json post /v1/snippets
openapi: 3.1.0
info:
  title: CNAP API
  version: 1.0.0
  description: >-
    Public API for managing CNAP workspaces, clusters, templates, products, and
    deployments.


    Authenticate with a Personal Access Token via the `Authorization: Bearer
    cnap_pat_...` header.


    Workspace-scoped endpoints require the `X-Workspace-Id` header.
servers:
  - url: https://api.cnap.tech
    description: Production
security: []
paths:
  /v1/snippets:
    post:
      tags:
        - Snippets
      summary: Create snippet
      description: >-
        Creates a reusable code snippet in the workspace. Snippets are async
        JavaScript functions executed in a sandboxed V8 isolate with access to
        cnap.request() for API calls.
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  minLength: 1
                  maxLength: 100
                  example: Cluster Resource Audit
                description:
                  type: string
                  maxLength: 500
                  description: Optional human-readable description
                code:
                  type: string
                  minLength: 1
                  maxLength: 50000
                  description: >-
                    Async JavaScript function body. Executed in a sandboxed V8
                    isolate with access to cnap.request().
                display_type:
                  type: string
                  enum:
                    - table
                    - stat
                    - json
                    - logs
                  description: >-
                    Controls how the snippet result is rendered in dashboard
                    widgets
              required:
                - name
                - code
                - display_type
      responses:
        '201':
          description: Snippet created
          content:
            application/json:
              schema:
                type: object
                properties:
                  snippet_id:
                    type: string
                required:
                  - snippet_id
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '403':
          description: Missing or invalid workspace
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '422':
          description: Validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - BearerAuth: []
components:
  schemas:
    Error:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: string
              example: not_found
            message:
              type: string
              example: Resource not found
            param:
              type: string
              description: The request field that caused the error
              example: name
            suggestion:
              type: string
              example: Run `cnap clusters list` to see available clusters
            details: {}
          required:
            - code
            - message
      required:
        - error
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: >-
        Personal Access Token (cnap_pat_...) or OAuth2 JWT. Create tokens at
        https://cnap.tech/account/tokens

````