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

# Generate Custom Function

> AI-generate a custom function from a natural language description

AI-generate a custom function from a natural language description. Provide a description of what the function should do, and optionally provide existing code to improve or a cURL command to convert into a function.

## Request Body

<ParamField body="query" type="string">
  Natural language description of what the function should do (e.g., "fetch stock prices from Yahoo Finance")
</ParamField>

<ParamField body="user_function" type="string">
  Existing Python code to improve or refactor
</ParamField>

<ParamField body="import_curl" type="string">
  A cURL command to convert into a custom function
</ParamField>

## Response

<ResponseField name="custom_function" type="string">
  The generated Python source code ready to use with [Create Custom Function](/api-reference/v1/custom_functions/create-custom-function)
</ResponseField>

## Example Request

```bash theme={"dark"}
curl -X POST "https://api.xpander.ai/v1/custom_functions/generate" \
  -H "Content-Type: application/json" \
  -H "x-api-key: <your-api-key>" \
  -d '{"query": "Create a function that fetches the current Bitcoin price from CoinGecko API"}'
```

## Notes

* The generated code includes a `xpander_run_action(...)` entry point
* Review the generated code before creating the function
* You can provide `user_function` to improve existing code, or `import_curl` to convert a cURL command


## OpenAPI

````yaml POST /v1/custom_functions/generate
openapi: 3.1.0
info:
  title: xpander.ai API Service
  description: |2-

        The xpander.ai API Service provides a unified REST API for managing AI agents,
        executing tasks, managing knowledge bases, and integrating with external systems.
        
        Features:
        - Agent Management: Create, update, deploy, and delete AI agents
        - Task Execution: Invoke agents with support for sync, async, and streaming modes
        - Knowledge Bases: Manage knowledge bases and documents for RAG workflows
        - Tools: Discover, connect, and attach tools (connectors, custom functions, MCP servers, sub-agents, workflows) to agents and workflows
        - MCP Integration: Model Context Protocol support for standardized AI interactions
        
        Authentication: All endpoints require authentication via either an API key (`x-api-key`) or an OAuth2 JWT (`Authorization: Bearer <jwt>`).
        
  version: '0.001'
servers:
  - url: https://api.xpander.ai
security: []
paths:
  /v1/custom_functions/generate:
    post:
      tags:
        - API v1
        - Custom Functions
        - Custom Functions
      summary: Generate Custom Function
      description: >-
        AI-generate a custom function from a natural language description.
        Provide a 'query' describing what the function should do, and optionally
        'user_function' (existing code to improve) or 'import_curl' (a cURL
        command to convert into a function). Returns the generated Python source
        code.
      operationId: Generate_custom_function_v1_custom_functions_generate_post
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateCustomFunctionRequest'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenerateCustomFunctionResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyHeader: []
components:
  schemas:
    GenerateCustomFunctionRequest:
      properties:
        query:
          anyOf:
            - type: string
            - type: 'null'
          title: Query
          description: >-
            Natural language description of what the function should do.
            Examples: 'Calculate compound interest given principal, rate, and
            years', 'Parse a CSV string and return the headers and first 5
            rows'.
          default: ''
        user_function:
          anyOf:
            - type: string
            - type: 'null'
          title: User Function
          description: >-
            Optional existing source code to improve or modify. The AI will use
            this as a starting point.
          default: ''
        import_curl:
          anyOf:
            - type: string
            - type: 'null'
          title: Import Curl
          description: >-
            Optional cURL command to convert into a custom function. The AI will
            parse the cURL and generate a function that makes the equivalent
            HTTP request.
          default: ''
      type: object
      title: GenerateCustomFunctionRequest
      description: >-
        Request model for AI-generating a custom function from a description.


        Provide a natural language description of what you need, and the AI will

        generate the Python source code with proper xpander_run_action
        signature.
    GenerateCustomFunctionResponse:
      properties:
        custom_function:
          type: string
          title: Custom Function
          description: Generated Python source code defining xpander_run_action(...).
      type: object
      required:
        - custom_function
      title: GenerateCustomFunctionResponse
      description: Response from the AI custom function generator.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      description: API Key for authentication
      in: header
      name: x-api-key

````