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

# Assign AI Agent

> Assigns or removes an agent configuration from a lead.
Pass `null` to unassign.
Requires `leads:import` permission.




## OpenAPI

````yaml PATCH /api/leads/{id}/agent
openapi: 3.1.0
info:
  title: Naturalead Leads API
  version: 1.0.0
  description: |
    API for managing leads in the Naturalead platform. All endpoints require
    API key authentication and are scoped to the caller's account.
servers:
  - url: '{baseUrl}'
    description: API server
    variables:
      baseUrl:
        default: http://localhost:3001
security:
  - ApiKeyAuth: []
  - BearerAuth: []
tags:
  - name: Leads
    description: Lead CRUD, search, filtering, and bulk operations
paths:
  /api/leads/{id}/agent:
    patch:
      tags:
        - Leads
      summary: Assign agent to lead
      description: |
        Assigns or removes an agent configuration from a lead.
        Pass `null` to unassign.
        Requires `leads:import` permission.
      operationId: assignLeadAgent
      parameters:
        - $ref: '#/components/parameters/LeadId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                assignedAgentId:
                  type:
                    - string
                    - 'null'
                  description: AgentConfig ObjectId or null to unassign
              required:
                - assignedAgentId
      responses:
        '200':
          description: Updated lead
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Lead'
        '400':
          $ref: '#/components/responses/BadRequest'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  parameters:
    LeadId:
      name: id
      in: path
      required: true
      description: Account-scoped sequential lead ID
      schema:
        type: integer
  schemas:
    Lead:
      type: object
      properties:
        id:
          type: integer
          description: Sequential, account-scoped identifier
        name:
          type: string
        phone:
          type: string
          description: Unique per account
        email:
          type: string
          default: ''
        status:
          $ref: '#/components/schemas/LeadStatus'
        accountId:
          type: string
        source:
          type: string
          default: api
        tags:
          type: array
          items:
            type: string
        assignedAgentId:
          type: string
          description: ObjectId reference to AgentConfig
        abTestId:
          type: string
          description: ObjectId reference to AbTest
        agentAssignedBy:
          type: string
          enum:
            - manual
            - ab_test
            - auto
          default: auto
        customFields:
          type: object
          additionalProperties:
            type: string
          description: Key-value string map of custom fields
        lastContactedAt:
          type: string
          format: date-time
        importBatchId:
          type: string
        externalId:
          type: string
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time
      required:
        - id
        - name
        - phone
        - email
        - status
        - accountId
    LeadStatus:
      type: string
      enum:
        - new
        - contacted
        - qualified
        - disqualified
  responses:
    BadRequest:
      description: Bad request
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
            required:
              - error
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
            required:
              - error
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
      description: API key prefixed with `nl_live_` or `nl_test_`
    BearerAuth:
      type: http
      scheme: bearer
      description: Bearer token using an API key (format nl_live_* or nl_test_*)

````