> ## 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.

# Invoke function

> Execute a deployed serverless function with a custom JSON payload. The request body is forwarded directly to your function code.

Request payload structure is completely flexible - send any valid JSON that your function expects. Your function receives the payload as the request body and can parse it however needed.

Successful response behavior depends on `invoke_response_mode`:
- New functions default to `passthrough`, so Kapso forwards the function response body, success status code, and `Content-Type` directly
- Legacy wrapped functions can keep `wrapped`, which preserves the legacy API shape and returns successful JSON results under `data`

The function must be in 'deployed' status to be invoked. If the function is in 'draft' or 'error' status, the request will fail with 422 validation error.

Authentication:
- Private functions require `X-API-Key`
- Public Cloudflare functions (`public_endpoint=true`) can be invoked without an API key
- Private and unknown functions both return `404` from this route

Execution tracking:
- All invocations are tracked with timing and request/response data
- Failed invocations return an `invocation_id` for debugging
- Check function invocation history via GET /functions/{id} (extended view)

Error handling:
- Function execution errors are captured and returned with details
- Errors are truncated to 512 characters for storage
- Original error context is preserved in invocation record




## OpenAPI

````yaml /api/platform/v1/openapi-workflows.yaml post /functions/{function_id}/invoke
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/{function_id}/invoke:
    parameters:
      - name: function_id
        in: path
        required: true
        schema:
          type: string
          format: uuid
        description: Function identifier
    post:
      tags:
        - Functions
      summary: Invoke function
      description: >
        Execute a deployed serverless function with a custom JSON payload. The
        request body is forwarded directly to your function code.


        Request payload structure is completely flexible - send any valid JSON
        that your function expects. Your function receives the payload as the
        request body and can parse it however needed.


        Successful response behavior depends on `invoke_response_mode`:

        - New functions default to `passthrough`, so Kapso forwards the function
        response body, success status code, and `Content-Type` directly

        - Legacy wrapped functions can keep `wrapped`, which preserves the
        legacy API shape and returns successful JSON results under `data`


        The function must be in 'deployed' status to be invoked. If the function
        is in 'draft' or 'error' status, the request will fail with 422
        validation error.


        Authentication:

        - Private functions require `X-API-Key`

        - Public Cloudflare functions (`public_endpoint=true`) can be invoked
        without an API key

        - Private and unknown functions both return `404` from this route


        Execution tracking:

        - All invocations are tracked with timing and request/response data

        - Failed invocations return an `invocation_id` for debugging

        - Check function invocation history via GET /functions/{id} (extended
        view)


        Error handling:

        - Function execution errors are captured and returned with details

        - Errors are truncated to 512 characters for storage

        - Original error context is preserved in invocation record
      operationId: invokeFunction
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FunctionInvokeRequest'
            examples:
              shipping_calculation:
                summary: Calculate shipping cost
                value:
                  weight: 5.5
                  distance: 120
                  service: express
              data_transformation:
                summary: Transform user data
                value:
                  user_id: usr_123
                  fields:
                    - email
                    - name
                    - phone
                  format: csv
      responses:
        '200':
          description: >
            Function executed successfully. `wrapped` functions return
            successful JSON under `data`.

            `passthrough` functions may also return other 2xx status codes and
            non-JSON content types depending on the function response.
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/FunctionInvokeResponse'
                  - $ref: '#/components/schemas/FunctionInvokeWrappedResponse'
              examples:
                shipping_result:
                  summary: Passthrough JSON result
                  value:
                    cost: 24.75
                    currency: USD
                    estimated_days: 2
                wrapped_shipping_result:
                  summary: Wrapped JSON result
                  value:
                    data:
                      cost: 24.75
                      currency: USD
                      estimated_days: 2
            text/plain:
              schema:
                type: string
              example: accepted
        '400':
          description: Invalid JSON in request body
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: Invalid JSON in request
        '404':
          description: >-
            Function not found, or private function is not accessible with the
            provided API key
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: Function not found
        '422':
          description: Function is not deployed or validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: Function is not deployed
        '500':
          description: Function execution failed
          content:
            application/json:
              schema:
                type: object
                required:
                  - error
                properties:
                  error:
                    type: string
                    description: Error message
                  detail:
                    type: string
                    description: >-
                      Detailed error information from function execution (if
                      available)
                  invocation_id:
                    type: string
                    format: uuid
                    description: Invocation record ID for debugging
              example:
                error: Function execution failed
                detail: 'ReferenceError: calculateShipping is not defined'
                invocation_id: 550e8400-e29b-41d4-a716-446655440000
      security:
        - ApiKeyAuth: []
        - {}
components:
  schemas:
    FunctionInvokeRequest:
      type: object
      description: >
        Payload to send to the function. Structure is completely flexible - send
        any valid JSON that your function expects. The payload will be forwarded
        to your function as the request body.
      additionalProperties: true
      example:
        weight: 5.5
        distance: 120
        service: express
    FunctionInvokeResponse:
      description: >
        Successful invoke output for functions using
        `invoke_response_mode=passthrough`.

        Kapso forwards the upstream response body directly and preserves the
        upstream success status code and `Content-Type`.
      oneOf:
        - type: object
          additionalProperties: true
        - type: array
          items: {}
        - type: string
        - type: number
        - type: boolean
      example:
        cost: 24.75
        currency: USD
    FunctionInvokeWrappedResponse:
      type: object
      required:
        - data
      properties:
        data:
          description: >
            Successful invoke output for functions using
            `invoke_response_mode=wrapped`.

            Kapso preserves the legacy API shape by nesting successful JSON
            responses under `data`.
          oneOf:
            - type: object
              additionalProperties: true
            - type: array
              items: {}
            - type: string
            - type: number
            - type: boolean
      example:
        data:
          cost: 24.75
          currency: USD
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: Human-readable error message
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key

````