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

# Create function

> Create a new serverless function in draft status. The function will be saved but not deployed to the runtime platform.

After creating a function:
1. Review and test the code locally
2. Deploy using POST /functions/{id}/deploy
3. Invoke using POST /functions/{id}/invoke

Choose function_type based on your deployment requirements:
- `cloudflare_worker`: Fast global edge deployment with standard JavaScript
- `supabase_function`: Deno runtime with built-in Supabase client

The function slug will be auto-generated from the name if not provided (lowercase with hyphens).




## OpenAPI

````yaml /api/platform/v1/openapi-workflows.yaml post /functions
openapi: 3.1.0
info:
  title: Kapso Platform API – Advanced Resources
  version: 0.1.0
  description: >
    Build WhatsApp automation workflows and serverless functions. Create
    multi-step conversation flows, inspect executions, and deploy custom logic.
servers:
  - url: https://api.kapso.ai/platform/v1
    description: Production
security:
  - ApiKeyAuth: []
tags:
  - name: Workflows
    description: Create and manage conversation workflows and executions
  - name: Workflow Triggers
    description: Configure workflow triggers to automate execution
  - name: Functions
    description: Manage serverless functions, deployments, and secrets
  - name: WhatsApp Conversations
    description: Query WhatsApp conversation data and related resources
paths:
  /functions:
    post:
      tags:
        - Functions
      summary: Create function
      description: >
        Create a new serverless function in draft status. The function will be
        saved but not deployed to the runtime platform.


        After creating a function:

        1. Review and test the code locally

        2. Deploy using POST /functions/{id}/deploy

        3. Invoke using POST /functions/{id}/invoke


        Choose function_type based on your deployment requirements:

        - `cloudflare_worker`: Fast global edge deployment with standard
        JavaScript

        - `supabase_function`: Deno runtime with built-in Supabase client


        The function slug will be auto-generated from the name if not provided
        (lowercase with hyphens).
      operationId: createFunction
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FunctionCreateRequest'
      responses:
        '201':
          description: Function created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FunctionResponse'
        '401':
          $ref: '#/components/responses/UnauthorizedError'
        '422':
          $ref: '#/components/responses/ValidationError'
components:
  schemas:
    FunctionCreateRequest:
      type: object
      required:
        - function
      properties:
        function:
          type: object
          required:
            - name
            - code
            - function_type
          properties:
            name:
              type: string
              description: Function display name (user-friendly identifier)
              example: Calculate Shipping Cost
            slug:
              type: string
              description: >
                URL-safe function identifier (optional - auto-generated from
                name if not provided). Must be lowercase alphanumeric with
                hyphens only.
              pattern: ^[a-z0-9-]+$
              example: calculate-shipping-cost
            description:
              type: string
              description: Optional function description
              example: >-
                Calculates shipping cost based on weight, distance, and service
                level
            code:
              type: string
              description: >
                JavaScript function code (max 1MB). Must be valid JavaScript for
                the target runtime. For Cloudflare Workers, use standard
                JavaScript. For Supabase Functions, use Deno-compatible code.
              example: |
                export default async function(request) {
                  const { weight, distance, service } = await request.json();
                  const baseCost = weight * 0.5;
                  const distanceCost = distance * 0.1;
                  const serviceFee = service === 'express' ? 10 : 0;
                  return new Response(JSON.stringify({
                    cost: baseCost + distanceCost + serviceFee
                  }));
                }
            function_type:
              type: string
              enum:
                - cloudflare_worker
                - supabase_function
              description: >
                Target serverless platform. Choose based on your deployment
                requirements:

                - `cloudflare_worker`: Fast global edge deployment, standard
                JavaScript

                - `supabase_function`: Deno runtime with built-in Supabase
                client
              example: cloudflare_worker
            public_endpoint:
              type: boolean
              default: false
              description: >
                Allow invoke requests without an API key. Only supported for
                `cloudflare_worker` functions.
            runtime_config:
              type: object
              description: >-
                Platform-specific runtime configuration (environment variables,
                resource limits, etc.)
              additionalProperties: true
              example:
                timeout: 30
                memory: 128
      description: >
        Request to create a new serverless function. The function will be saved
        in draft status. Use POST /functions/{id}/deploy to deploy it to the
        runtime platform.
    FunctionResponse:
      type: object
      required:
        - data
      properties:
        data:
          $ref: '#/components/schemas/Function'
      description: Single function response
    Function:
      type: object
      required:
        - id
        - name
        - slug
        - status
        - function_type
        - created_at
        - updated_at
      properties:
        id:
          type: string
          format: uuid
          description: Unique function identifier
        name:
          type: string
          description: Function display name (user-friendly identifier)
        slug:
          type: string
          description: >
            URL-safe function identifier (lowercase alphanumeric with hyphens).
            Used in endpoint URLs. Must match pattern: /^[a-z0-9-]+$/
          pattern: ^[a-z0-9-]+$
          example: calculate-shipping-cost
        lock_version:
          type: integer
          description: >
            Optimistic locking version. Increments on each update. Include this
            value on update to detect concurrent modifications — stale values
            return `409 Conflict`.
          example: 0
        description:
          type:
            - string
            - 'null'
          description: Optional function description explaining purpose and usage
        code:
          type: string
          description: >
            JavaScript function code. Maximum size: 1 megabyte. This is the
            actual code that will execute when the function is invoked. Must be
            valid JavaScript that can run in the target environment (Cloudflare
            Workers or Supabase Edge Functions).
        version:
          type:
            - integer
            - 'null'
          description: >
            Function version number. Increments with each deployment. Use this
            to track which version is currently deployed in production.
        status:
          type: string
          enum:
            - draft
            - deployed
            - error
          description: |
            Deployment status:
            - `draft`: Function code saved but not yet deployed to runtime
            - `deployed`: Successfully deployed and available for invocation
            - `error`: Deployment failed (check error logs for details)
        last_deployed_at:
          type:
            - string
            - 'null'
          format: date-time
          description: >-
            Timestamp of most recent successful deployment, null if never
            deployed
        function_type:
          type: string
          enum:
            - cloudflare_worker
            - supabase_function
          description: >
            Serverless runtime platform:

            - `cloudflare_worker`: Deploys to Cloudflare Workers (global edge
            network)

            - `supabase_function`: Deploys to Supabase Edge Functions (Deno
            runtime)
        invoke_response_mode:
          type: string
          enum:
            - wrapped
            - passthrough
          default: passthrough
          description: >
            Controls how successful invoke responses are returned:

            - `wrapped`: Legacy behavior. Successful JSON responses are nested
            under `data`.

            - `passthrough`: Kapso forwards the function response body, status
            code, and `Content-Type` directly.


            Existing functions may still be `wrapped`. Newly created functions
            default to `passthrough`.
        public_endpoint:
          type: boolean
          default: false
          description: >
            Whether the invoke endpoint can be called without an API key. Only
            supported for `cloudflare_worker` functions.

            When `true`, Kapso serves the function through the Platform API
            invoke route and anonymous requests are allowed.
        runtime_config:
          type:
            - object
            - 'null'
          description: >
            Platform-specific runtime configuration. Structure varies by
            function_type. Use this to configure environment variables, resource
            limits, or platform-specific features.
          additionalProperties: true
        endpoint_url:
          type:
            - string
            - 'null'
          format: uri
          description: >
            Invocation URL for this function. Computed based on `function_type`.

            - `cloudflare_worker`:
            `https://api.kapso.ai/platform/v1/functions/{function_id}/invoke`

            - `supabase_function`: Direct Supabase Edge Function URL


            For private Cloudflare functions, include `X-API-Key`. For public
            Cloudflare functions (`public_endpoint=true`), the API key is
            optional.
          example: https://api.kapso.ai/platform/v1/functions/{function_id}/invoke
        project_id:
          type: string
          format: uuid
          description: Project this function belongs to
        created_by_id:
          type:
            - string
            - 'null'
          format: uuid
          description: ID of user who created this function
        created_at:
          type: string
          format: date-time
          description: Function creation timestamp
        updated_at:
          type: string
          format: date-time
          description: Last modification timestamp
      description: >
        Serverless function configuration. Functions are custom JavaScript code
        that runs on-demand in response to API invocations. Deploy functions to
        either Cloudflare Workers (global edge network) or Supabase Edge
        Functions (Deno runtime).
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: Human-readable error message
  responses:
    UnauthorizedError:
      description: Missing or invalid API key
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    ValidationError:
      description: Request validation failed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key

````