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

# Update Function or Macro

> Changes the fields you send. New code, a new `type`, `usedAs` or `name` is compiled before it is saved. A `HELPER`'s `params` follow its code, and its code must still define `{% macro <name>(...) %}` with the (new) name: rename it in the code too. The change applies to every agent and guardrail using it.



## OpenAPI

````yaml /api-reference/openapi.json patch /v2/voice-agents/functions-macros/{functionMacroId}
openapi: 3.0.1
info:
  title: Hamsa API
  description: >-
    The Hamsa API: voice agents and their calls, phone numbers, batch calls and
    knowledge bases; speech to text, text to speech, voices and AI content.
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://api.tryhamsa.com
security: []
paths:
  /v2/voice-agents/functions-macros/{functionMacroId}:
    patch:
      summary: Update a function or macro
      description: >-
        Changes the fields you send. New code, a new `type`, `usedAs` or `name`
        is compiled before it is saved. A `HELPER`'s `params` follow its code,
        and its code must still define `{% macro <name>(...) %}` with the (new)
        name: rename it in the code too. The change applies to every agent and
        guardrail using it.
      parameters:
        - in: path
          name: functionMacroId
          required: true
          description: ID of the function or macro.
          schema:
            type: string
            format: uuid
            example: 9c1d2e3f-4a5b-4c6d-8e7f-0a1b2c3d4e5f
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  minLength: 1
                  maxLength: 255
                  description: >-
                    Name (1-255 characters), unique in the project. A `HELPER`
                    macro's name must be an identifier: letters, digits and
                    underscores, not starting with a digit
                    (`FunctionMacroHelperNameInvalid`), and its `code` must
                    define `{% macro <name>(...) %}` with this name. For any
                    macro written as definitions, the one named like the record
                    runs (else the first).
                notes:
                  type: string
                  maxLength: 2000
                  nullable: true
                  description: Your own notes. Not used on calls.
                type:
                  type: string
                  enum:
                    - JAVASCRIPT_FUNCTION
                    - JINJA_MACRO
                  description: >-
                    `JAVASCRIPT_FUNCTION`: the body of `function (text, vars) {
                    ... }`. `JINJA_MACRO`: a Jinja template with `text`, `vars`
                    and `args` in scope, or a `{% macro <name>(...) %}`
                    definition.
                usedAs:
                  type: string
                  enum:
                    - CHECK
                    - ACTION
                    - HELPER
                  description: >-
                    `CHECK`: answers "does this text match?" (a guardrail's
                    `lookFor`). `ACTION`: answers "what changes?" (a guardrail's
                    `behaviour`): new text, or variables to set. `HELPER`: a
                    macro prompts call by its name, as `{{ <name>(...) }}`
                    (`JINJA_MACRO` only).
                code:
                  type: string
                  minLength: 1
                  maxLength: 20000
                  description: >-
                    The code (1-20,000 characters), compiled when you save it.


                    **Function**: the body of `function (text, vars) { ... }`.
                    `text` is what was said, as said; `vars` are the call's
                    variables, as text unless a value is JSON. A `CHECK` returns
                    `true` to match. An `ACTION` returns a string (the new text)
                    or an object (the variables to set). JavaScript built-ins
                    only (no `console`, `fetch` or timers); 50 ms and 16 MiB per
                    run.


                    **Macro**: a template with `text`, `vars` and `args`, or a
                    `{% macro <name>(...) %}` definition (required for `HELPER`,
                    named like the record). A `CHECK` matches unless it renders
                    empty, `false`, `0`, `none` or `no`. An `ACTION` sets
                    variables when it renders a JSON object; anything else is
                    the new text. 50 ms and 64 KiB of output.
                params:
                  type: array
                  nullable: true
                  maxItems: 20
                  items:
                    type: string
                    maxLength: 64
                    pattern: ^[A-Za-z_][A-Za-z0-9_]*$
                  description: >-
                    `HELPER` macros only, and optional: the parameters that the
                    `{% macro <name>(...) %}` in `code` declares, in order.
                    Hamsa reads them from the code; if you send them they must
                    match it (`FunctionMacroParamsMismatch`). Leave out or
                    `null` for `CHECK` and `ACTION`.
              description: >-
                Send at least one field. Fields you leave out keep their value.
                Changing `type` or `usedAs` is refused while a guardrail uses it
                in a slot the change would no longer fit.
            examples:
              code:
                summary: New code
                value:
                  code: return /\d{12,19}/.test(text.replace(/[\s-]/g, ''));
              notes:
                summary: Clear the notes
                value:
                  notes: null
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  message:
                    type: string
                    example: success
                  data:
                    $ref: '#/components/schemas/FunctionMacro'
        '400':
          description: >-
            Invalid body: for example `HELPER` with a `JAVASCRIPT_FUNCTION`
            (`FunctionMacroHelperMacrosOnly`), `params` on a `CHECK` or `ACTION`
            (`FunctionMacroParamsHelperOnly`), a `HELPER` whose `code` does not
            define `{% macro <name>(...) %}` with its name
            (`FunctionMacroHelperDefinitionRequired`), a definition with more
            than 20 parameters, a parameter over 64 characters or a repeated one
            (`FunctionMacroParamsMaxLength`, `FunctionMacroParamInvalid`,
            `FunctionMacroParamsDuplicate`), `params` that differ from the
            definition's (`FunctionMacroParamsMismatch`), or code that does not
            compile (`FunctionMacroCodeSyntaxError`, with the reason and line in
            `params.reason` and `params.line`), or no field to update
            (`FunctionMacroUpdateNeedsAField`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '401':
          description: 'Unauthorized: missing or invalid API key.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '403':
          description: >-
            Your API key or role does not have the `functions_macros:update`
            permission.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '404':
          description: >-
            Function or macro not found in this project
            (`FunctionMacroNotFound`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '409':
          description: >-
            Another function or macro has this name
            (`FunctionMacroNameAlreadyExists`), or a guardrail uses it and the
            new `type` or `usedAs` would no longer fit
            (`FunctionMacroUsedInGuardrails`, naming them): change the guardrail
            first.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '500':
          description: Server Side Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '503':
          description: >-
            The code could not be checked right now
            (`ScriptExecutorUnavailable`). Try again later.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
      security:
        - ApiKeyAuth: []
components:
  schemas:
    FunctionMacro:
      type: object
      description: 'A project script: a guardrail check or action, or a prompt helper macro.'
      properties:
        id:
          type: string
          format: uuid
          example: 9c1d2e3f-4a5b-4c6d-8e7f-0a1b2c3d4e5f
        projectId:
          type: string
          format: uuid
          example: fbb9e8b2-2dbb-40f9-a051-9404624e3247
        userId:
          type: string
          nullable: true
          example: b7d9e979-4a16-437b-8571-4ed92a94faa8
        createdBy:
          type: string
          description: ID of the member who created it.
          example: b7d9e979-4a16-437b-8571-4ed92a94faa8
        updatedBy:
          type: string
          nullable: true
          description: ID of the member who last changed it.
          example: null
        name:
          type: string
          minLength: 1
          maxLength: 255
          description: >-
            Unique among the project's functions and macros. A `HELPER` macro's
            name is how prompts call it: `{{ <name>(...) }}`.
          example: has_card_number
        notes:
          type: string
          maxLength: 2000
          nullable: true
          description: Your own notes. Not used on calls.
          example: Matches 13 to 19 digits in a row
        type:
          type: string
          enum:
            - JAVASCRIPT_FUNCTION
            - JINJA_MACRO
          description: >-
            `JAVASCRIPT_FUNCTION`: the body of `function (text, vars) { ... }`.
            `JINJA_MACRO`: a Jinja template with `text`, `vars` and `args` in
            scope, or a `{% macro <name>(...) %}` definition.
          example: JAVASCRIPT_FUNCTION
        usedAs:
          type: string
          enum:
            - CHECK
            - ACTION
            - HELPER
          description: >-
            `CHECK`: answers "does this text match?" (a guardrail's `lookFor`).
            `ACTION`: answers "what changes?" (a guardrail's `behaviour`): new
            text, or variables to set. `HELPER`: a macro prompts call by its
            name, as `{{ <name>(...) }}` (`JINJA_MACRO` only).
          example: CHECK
        code:
          type: string
          minLength: 1
          maxLength: 20000
          description: >-
            The code. A function is the body of `function (text, vars) { ... }`.
            A macro is a template, or a `{% macro <name>(...) %}` definition
            (always a definition named like the record for `HELPER`).
          example: return /\d{13,19}/.test(text.replace(/[\s-]/g, ''));
        params:
          type: array
          nullable: true
          maxItems: 20
          items:
            type: string
            maxLength: 64
            pattern: ^[A-Za-z_][A-Za-z0-9_]*$
          description: >-
            `HELPER` macros only: the parameters its `{% macro <name>(...) %}`
            definition declares, in order. `null` for `CHECK` and `ACTION`.
          example: null
        requiredParams:
          type: array
          nullable: true
          readOnly: true
          items:
            type: string
          example:
            - name
          description: >-
            `HELPER` macros only: the parameters without a default value, in
            order; a prompt must pass these. `{% macro greet(name, tone="warm")
            %}` gives `params` `[name, tone]` and `requiredParams` `[name]`.
            `null` for `CHECK` and `ACTION`.
        createdAt:
          type: string
          format: date-time
          example: '2026-10-20T09:15:00.000Z'
        updatedAt:
          type: string
          format: date-time
          example: '2026-10-20T09:15:00.000Z'
    ErrorSchema:
      type: object
      properties:
        code:
          type: integer
          description: >-
            Service error code: the HTTP status with a service prefix (for
            example `8404`). Missing when the service has no code for that
            status.
          example: 8404
        message:
          type: string
          description: >-
            What went wrong. Some services (calls and recordings) put the error
            key here, for example `CallNotFoundOrUnavailable`, and send no
            `messageKey`.
          example: Policy not found!
        messageKey:
          type: string
          description: >-
            Stable key for the error, safe to match on in code. Not returned by
            every service.
          example: PolicyNotFound
        params:
          type: object
          additionalProperties: true
          description: >-
            Details for some errors (for example `reason`). Not returned by
            every service.
          example:
            reason: NO_DRAFT
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: >-
        Pass the API key in the Authorization header, You need to put Token
        keyword before the API key. e.g. 'Authorization: Token <api-key>'

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.