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

# Create function

> Create a new Zavu Function. The function starts in `draft` status. A dedicated API key is auto-provisioned and injected as the `ZAVU_API_KEY` secret so the function can call back into the Zavu API without manual setup.

Provide `sourceCode` to seed the draft. Call `POST /v1/functions/{functionId}/deploy` afterwards to publish.



## OpenAPI

````yaml /openapi.json post /v1/functions
openapi: 3.0.3
info:
  title: Zavu Unified Messaging Layer API
  version: 0.2.0
  description: >
    Unified multi-channel messaging API for Zavu.


    Supported channels:

    - **SMS**: Simple text messages

    - **WhatsApp**: Rich messaging with media, buttons, lists, CTA URL buttons,
    and templates

    - **Telegram**: Bot messaging with text, media, and interactive elements

    - **Email**: Transactional emails via Amazon SES


    Design goals:

    - Simple `send()` entrypoint for developers

    - Project-level authentication via Bearer token

    - Support for all WhatsApp message types (text, image, video, audio,
    document, sticker, location, contact, buttons, list, cta_url, reaction,
    template)

    - If a non-text message type is sent, WhatsApp channel is used automatically

    - 24-hour WhatsApp conversation window enforcement

    - Universal `to` field accepts phone numbers (E.164), email addresses, or
    numeric chat IDs (Telegram/Instagram)
servers:
  - url: https://api.zavu.dev
security:
  - bearerAuth: []
paths:
  /v1/functions:
    post:
      tags:
        - Functions
      summary: Create function
      description: >-
        Create a new Zavu Function. The function starts in `draft` status. A
        dedicated API key is auto-provisioned and injected as the `ZAVU_API_KEY`
        secret so the function can call back into the Zavu API without manual
        setup.


        Provide `sourceCode` to seed the draft. Call `POST
        /v1/functions/{functionId}/deploy` afterwards to publish.
      operationId: createFunction
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FunctionCreateRequest'
            example:
              slug: order-bot
              name: Order Bot
              description: Replies to order status questions on WhatsApp.
              sourceCode: |
                import { defineFunction } from '@zavu/functions';

                export default defineFunction(async (event, ctx) => {
                  ctx.log('received', event.type);
                });
              dependencies:
                openai: ^4.20.0
      responses:
        '201':
          description: Function created.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ZavuFunctionResponse'
        '400':
          description: Invalid request (e.g. duplicate slug, invalid dependencies).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
      security:
        - bearerAuth: []
components:
  schemas:
    FunctionCreateRequest:
      type: object
      required:
        - slug
        - name
      properties:
        slug:
          type: string
          description: >-
            URL-safe identifier (lowercase, digits, hyphens). Must be unique per
            project.
          minLength: 1
          maxLength: 50
          example: order-bot
        name:
          type: string
          maxLength: 80
          example: Order Bot
        description:
          type: string
          maxLength: 280
        runtime:
          $ref: '#/components/schemas/ZavuFunctionRuntime'
        timeoutSec:
          type: integer
          minimum: 1
          maximum: 30
          default: 10
        memoryMb:
          type: integer
          enum:
            - 128
            - 256
            - 512
            - 1024
          default: 256
        httpEnabled:
          type: boolean
          default: false
          description: Whether to expose a public HTTPS URL for this function.
        sourceCode:
          type: string
          description: TypeScript source code for the function entry point (max ~900KB).
          maxLength: 900000
        dependencies:
          type: object
          additionalProperties:
            type: string
          description: npm dependencies. Keys are package names, values are semver ranges.
          example:
            openai: ^4.20.0
    ZavuFunctionResponse:
      type: object
      required:
        - function
      properties:
        function:
          $ref: '#/components/schemas/ZavuFunction'
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: string
          example: invalid_request
        message:
          type: string
          example: Phone number is invalid
        details:
          type: object
          additionalProperties: true
    ZavuFunctionRuntime:
      type: string
      description: Runtime the function is deployed on.
      enum:
        - nodejs24
    ZavuFunction:
      type: object
      description: >-
        A Zavu Function — user-supplied TypeScript that runs in Zavu Cloud and
        reacts to messaging events or HTTP requests.
      required:
        - id
        - slug
        - name
        - runtime
        - status
        - timeoutSec
        - memoryMb
        - httpEnabled
        - dependencies
        - createdAt
        - updatedAt
      properties:
        id:
          type: string
          example: fn_abc123
        slug:
          type: string
          description: URL-safe identifier, unique per project.
          example: order-bot
        name:
          type: string
          example: Order Bot
        description:
          type: string
          nullable: true
        runtime:
          $ref: '#/components/schemas/ZavuFunctionRuntime'
        status:
          $ref: '#/components/schemas/ZavuFunctionStatus'
        timeoutSec:
          type: integer
          description: Per-invocation timeout in seconds.
          example: 10
        memoryMb:
          type: integer
          description: Memory allocation in MB.
          example: 256
        httpEnabled:
          type: boolean
          description: Whether the function can be invoked over HTTPS via its public URL.
        publicUrl:
          type: string
          format: uri
          nullable: true
          description: HTTPS endpoint when httpEnabled is true.
        dependencies:
          type: object
          additionalProperties:
            type: string
          description: >-
            npm dependencies installed in the function bundle. Keys are package
            names, values are semver ranges.
          example:
            openai: ^4.20.0
        activeDeploymentId:
          type: string
          nullable: true
          description: ID of the deployment currently serving traffic.
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time
    ZavuFunctionStatus:
      type: string
      description: Lifecycle status of a Zavu Function.
      enum:
        - draft
        - bundling
        - deploying
        - active
        - failed
        - disabled
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

````