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

# Replace end function

> Replace the entire code of the end function. This runs asynchronously after every conversation ends — this is a complete replacement, not a partial edit.



## OpenAPI

````yaml api-reference/agents/openapi.json PUT /v1/agents/{agentId}/branches/{branchId}/functions/end
openapi: 3.1.0
info:
  title: PolyAI Agents API
  version: 1.0.11
  description: Manage PolyAI agents, telephony, deployments, and real-time configs.
servers:
  - url: https://api.us.poly.ai
    description: US region
  - url: https://api.eu.poly.ai
    description: EU region
  - url: https://api.uk.poly.ai
    description: UK region
  - url: https://api.studio.poly.ai
    description: Studio region
security:
  - polyApiKey: []
paths:
  /v1/agents/{agentId}/branches/{branchId}/functions/end:
    parameters:
      - name: agentId
        in: path
        required: true
        schema:
          type: string
      - name: branchId
        in: path
        required: true
        schema:
          type: string
    put:
      tags:
        - Functions
      summary: Replace the end function code.
      description: >-
        Replace the entire code of the end function. This runs asynchronously
        after every conversation ends — this is a complete replacement, not a
        partial edit.
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpdateSpecialFunctionPublicBody'
      responses:
        '200':
          description: Updated end function
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SpecialFunctionPublicResponse'
components:
  schemas:
    UpdateSpecialFunctionPublicBody:
      title: UpdateSpecialFunctionPublicBody
      description: New code for a branch's start or end special function.
      type: object
      properties:
        code:
          type: string
          minLength: 1
      required:
        - code
      additionalProperties: false
    SpecialFunctionPublicResponse:
      title: SpecialFunctionPublicResponse
      description: >-
        The code, syntax errors, and parameters for a branch's start or end
        special function.
      type: object
      properties:
        code:
          type: string
        errors:
          type: array
          items:
            type: object
            properties:
              lineno:
                type: number
              message:
                type: string
              text:
                type: string
            required:
              - lineno
              - message
            additionalProperties: false
        parameters:
          type: array
          items:
            type: object
            properties:
              id:
                type: string
              name:
                type: string
              description:
                type: string
              type:
                type: string
            required:
              - id
              - name
              - description
              - type
            additionalProperties: false
      required:
        - code
        - errors
        - parameters
      additionalProperties: false
  securitySchemes:
    polyApiKey:
      type: apiKey
      in: header
      name: X-API-KEY

````