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

# Send a message

> Free-form text to a lead, or an approved template to a lead or a phone number. If it cannot be sent, returns an error: 409 if we know before sending (24-hour window, do not contact, unapproved template), 400 recipient_invalid if WhatsApp rejects the recipient, and 502 if WhatsApp does not respond.



## OpenAPI

````yaml /openapi.yaml post /messages
openapi: 3.1.0
info:
  title: NewLead API
  version: 1.0.0
  description: >-
    The NewLead public API. Each API key belongs to one subaccount and can read
    and change only that subaccount's data. Errors, pagination, idempotency and
    rate limits are documented at https://docs.newlead.ai.
servers:
  - url: https://rest.newlead.ai/v1
security:
  - BearerAuth: []
  - ApiKeyHeader: []
tags:
  - name: Specification
    description: This document.
paths:
  /messages:
    post:
      tags:
        - Messages
      summary: Send a message
      description: >-
        Free-form text to a lead, or an approved template to a lead or a phone
        number. If it cannot be sent, returns an error: 409 if we know before
        sending (24-hour window, do not contact, unapproved template), 400
        recipient_invalid if WhatsApp rejects the recipient, and 502 if WhatsApp
        does not respond.
      operationId: createMessage
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MessageCreateRequest'
      responses:
        '201':
          description: >-
            The message, with lead_created (false for text) and warnings (empty
            if there were none).
          headers:
            X-Request-ID:
              $ref: '#/components/headers/X-Request-ID'
            RateLimit-Limit:
              $ref: '#/components/headers/RateLimit-Limit'
            RateLimit-Remaining:
              $ref: '#/components/headers/RateLimit-Remaining'
            RateLimit-Reset:
              $ref: '#/components/headers/RateLimit-Reset'
            Idempotent-Replayed:
              $ref: '#/components/headers/Idempotent-Replayed'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MessageCreated'
        '429':
          $ref: '#/components/responses/RateLimited'
        4XX:
          $ref: '#/components/responses/Error'
        5XX:
          $ref: '#/components/responses/Error'
components:
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: >-
        Up to 255 characters. Repeat the key with the same body within 24 hours
        and you get the same response plus an Idempotent-Replayed header set to
        true. 402, 409, 429 and 5xx responses (except send_outcome_unknown) are
        not stored: fix the cause and retry with the same key.
      schema:
        type: string
        minLength: 1
        maxLength: 255
  schemas:
    MessageCreateRequest:
      type: object
      additionalProperties: false
      required:
        - type
      description: >-
        type text: free-form text to a lead_id; with the 24-hour window closed
        on WhatsApp it goes out as the agent's reopening template, and on
        Instagram it returns 409. type template: an approved template to a
        WhatsApp lead_id, or to a phone number (to) of an agent (text_agent_id),
        creating the lead if it does not exist.
      properties:
        type:
          type: string
          enum:
            - text
            - template
        lead_id:
          type: string
          format: uuid
        text_agent_id:
          type: string
          format: uuid
        to:
          $ref: '#/components/schemas/PhoneE164'
        text:
          type: string
          minLength: 1
          maxLength: 4096
        template:
          $ref: '#/components/schemas/TemplateSend'
        lead_variables:
          $ref: '#/components/schemas/LeadVariables'
        pause_ai:
          type: boolean
          description: 'Templates only: pauses the AI so a person can take over.'
        reset_conversation:
          type: boolean
          description: 'Templates only: an existing lead starts over.'
        source_lead_id:
          type: string
          format: uuid
          description: >-
            Only with type template: the lead from another channel for the same
            person, for example the call that opens this WhatsApp conversation.
            Both end up with the same contact_id.
    MessageCreated:
      description: >-
        The message just sent, with lead_created (false for text) and warnings
        (empty if there were none).
      allOf:
        - $ref: '#/components/schemas/Message'
        - type: object
          required:
            - lead_created
            - warnings
          properties:
            lead_created:
              type: boolean
            warnings:
              type: array
              items:
                $ref: '#/components/schemas/MessageWarning'
    PhoneE164:
      type: string
      pattern: ^\+[1-9]\d{6,14}$
      description: Phone number in E.164 format.
      examples:
        - '+34600111222'
    TemplateSend:
      type: object
      additionalProperties: false
      required:
        - name
        - language
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 512
        language:
          type: string
          pattern: ^[a-z]{2}(?:[_-][A-Za-z]{2})?$
          examples:
            - es
            - pt_BR
        components:
          type: array
          maxItems: 10
          items:
            $ref: '#/components/schemas/TemplateSendComponent'
    LeadVariables:
      type: object
      description: >-
        Flat customer variables. They are merged with the lead's existing ones;
        null or "" deletes the key.
      maxProperties: 50
      propertyNames:
        minLength: 1
        maxLength: 100
        pattern: ^\S(?:.*\S)?$
      additionalProperties:
        type:
          - string
          - number
          - boolean
          - 'null'
        maxLength: 1000
    Message:
      type: object
      additionalProperties: false
      required:
        - object
        - id
        - lead_id
        - direction
        - sender
        - origin
        - type
        - text
        - template
        - attachments
        - reply_to_message_id
        - status
        - error
        - created_at
        - sent_at
        - delivered_at
        - read_at
        - failed_at
      properties:
        object:
          type: string
          const: message
        id:
          type: string
          format: uuid
        lead_id:
          type: string
          format: uuid
        direction:
          type: string
          enum:
            - inbound
            - outbound
        sender:
          type: string
          enum:
            - lead
            - agent
            - skills
          description: >-
            lead: written by the lead. agent: sent by the agent (the AI or a
            person). skills: an agent tool.
        origin:
          type: string
          enum:
            - lead
            - ai
            - manual
            - api
            - system
            - unclassified
          description: >-
            Who originated it. lead: the lead. ai: the AI agent. manual: a
            person from the dashboard. api: an API request. system: the
            platform, for example a notice. unclassified: historical rows with
            no known origin.
        type:
          type: string
          enum:
            - text
            - template
            - media
        text:
          type:
            - string
            - 'null'
          description: null on a lead message with an attachment and no caption.
        template:
          type:
            - object
            - 'null'
          additionalProperties: false
          required:
            - name
            - language
          properties:
            name:
              type: string
            language:
              type:
                - string
                - 'null'
        attachments:
          type: array
          items:
            $ref: '#/components/schemas/Attachment'
        reply_to_message_id:
          type:
            - string
            - 'null'
          format: uuid
        status:
          type: string
          enum:
            - received
            - queued
            - accepted
            - sent
            - delivered
            - read
            - failed
          description: >-
            received: a message from the lead. queued: stored but not yet sent
            to WhatsApp (in the queue, outside sending hours, waiting for
            template approval or with no channel connected). accepted: WhatsApp
            accepted it. sent, delivered, read and failed are confirmed by
            WhatsApp later.
        error:
          type:
            - object
            - 'null'
          additionalProperties: false
          required:
            - code
            - category
          description: Why it failed, if it failed.
          properties:
            code:
              type:
                - integer
                - 'null'
              description: The Meta error code.
            category:
              type:
                - string
                - 'null'
              examples:
                - recipient_not_reachable
                - template_paused
        created_at:
          type: string
          format: date-time
        sent_at:
          type:
            - string
            - 'null'
          format: date-time
        delivered_at:
          type:
            - string
            - 'null'
          format: date-time
        read_at:
          type:
            - string
            - 'null'
          format: date-time
        failed_at:
          type:
            - string
            - 'null'
          format: date-time
        lead_created:
          type: boolean
          description: >-
            Only in the POST /messages response: whether the send created the
            lead.
        warnings:
          type: array
          description: >-
            Only in the POST /messages response: steps after the send that
            failed (the template was already sent). Empty if there were no
            warnings.
          items:
            $ref: '#/components/schemas/MessageWarning'
    MessageWarning:
      type: string
      enum:
        - pause_ai_failed
        - variables_failed
        - conversation_reset_failed
    ErrorResponse:
      type: object
      additionalProperties: false
      required:
        - error
      properties:
        error:
          type: object
          additionalProperties: false
          required:
            - type
            - code
            - message
            - param
            - request_id
            - doc_url
          properties:
            type:
              $ref: '#/components/schemas/ErrorType'
            code:
              $ref: '#/components/schemas/ErrorCode'
            message:
              type: string
              description: 'A human-readable explanation. It can change: never match on it.'
            param:
              type:
                - string
                - 'null'
              description: The offending field, dot-separated (variables.city), or null.
            request_id:
              type: string
              description: The same value as the x-request-id header.
            doc_url:
              type: string
              format: uri
            details:
              type: object
              additionalProperties: true
              description: >-
                On agent_not_ready, carries blockers. On send_outcome_unknown
                from POST /calls, carries call_id if the call is known.
      examples:
        - error:
            type: invalid_request_error
            code: parameter_invalid
            message: phone must match pattern "^\+[1-9]\d{6,14}$".
            param: phone
            request_id: req_m3k2a9_4f1c2b7e
            doc_url: https://docs.newlead.ai/errors#parameter_invalid
    TemplateSendComponent:
      type: object
      additionalProperties: false
      required:
        - type
        - parameters
      description: A template component with its values, in WhatsApp format.
      properties:
        type:
          type: string
          enum:
            - header
            - body
            - button
        sub_type:
          type: string
          enum:
            - url
          description: Buttons only.
        index:
          type: string
          pattern: ^\d$
          description: 'Buttons only: its position in the template.'
        parameters:
          type: array
          items:
            $ref: '#/components/schemas/TemplateSendParameter'
    Attachment:
      type: object
      additionalProperties: false
      required:
        - object
        - id
        - type
        - mime_type
        - file_name
        - file_size
        - transcription
      description: >-
        The attachment metadata, without a URL: downloading will be a separate
        operation.
      properties:
        object:
          type: string
          const: attachment
        id:
          type: string
          format: uuid
        type:
          type: string
          enum:
            - image
            - video
            - audio
            - voice
            - document
            - sticker
            - location
            - contact
        mime_type:
          type:
            - string
            - 'null'
        file_name:
          type:
            - string
            - 'null'
        file_size:
          type:
            - integer
            - 'null'
        transcription:
          type:
            - string
            - 'null'
          description: >-
            What the lead said in a voice note or an audio, transcribed. null
            for other types or when it could not be transcribed.
    ErrorType:
      type: string
      enum:
        - invalid_request_error
        - authentication_error
        - billing_error
        - permission_error
        - not_found_error
        - conflict_error
        - rate_limit_error
        - api_error
        - provider_error
    ErrorCode:
      type: string
      description: >-
        The closed list of error causes. The table is at
        https://docs.newlead.ai/errors.
      enum:
        - parameter_missing
        - parameter_invalid
        - parameter_unknown
        - body_invalid_json
        - recipient_invalid
        - api_key_missing
        - api_key_invalid
        - insufficient_balance
        - subscription_inactive
        - api_key_forbidden
        - feature_disabled
        - resource_not_found
        - route_not_found
        - agent_not_ready
        - lead_do_not_contact
        - lead_ai_paused
        - lead_ai_not_paused
        - messaging_window_closed
        - template_not_approved
        - outside_initiation_hours
        - voice_number_missing
        - webhook_endpoint_limit_reached
        - webhook_endpoint_disabled
        - idempotency_key_reused
        - idempotency_key_in_use
        - rate_limit_exceeded
        - internal_error
        - provider_unavailable
        - send_outcome_unknown
    TemplateSendParameter:
      type: object
      additionalProperties: false
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - text
            - image
            - video
            - document
        text:
          type: string
        image:
          type: object
          additionalProperties: false
          required:
            - link
          properties:
            link:
              type: string
              format: uri
        video:
          type: object
          additionalProperties: false
          required:
            - link
          properties:
            link:
              type: string
              format: uri
        document:
          type: object
          additionalProperties: false
          required:
            - link
          properties:
            link:
              type: string
              format: uri
            filename:
              type: string
  headers:
    X-Request-ID:
      description: The id of this request. Quote it when you contact support.
      schema:
        type: string
    RateLimit-Limit:
      description: Requests allowed per minute for this API key.
      schema:
        type: integer
    RateLimit-Remaining:
      description: Requests left in the current minute.
      schema:
        type: integer
    RateLimit-Reset:
      description: Seconds until the counter resets.
      schema:
        type: integer
    Idempotent-Replayed:
      description: true when the response is the stored one for a repeated Idempotency-Key.
      schema:
        type: string
        const: 'true'
    Retry-After:
      description: Seconds to wait before repeating the request.
      schema:
        type: integer
  responses:
    RateLimited:
      description: Too many requests for this API key. Wait for Retry-After and repeat.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        RateLimit-Limit:
          $ref: '#/components/headers/RateLimit-Limit'
        RateLimit-Remaining:
          $ref: '#/components/headers/RateLimit-Remaining'
        RateLimit-Reset:
          $ref: '#/components/headers/RateLimit-Reset'
        Retry-After:
          $ref: '#/components/headers/Retry-After'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    Error:
      description: An error. type is the family and code is the cause.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        RateLimit-Limit:
          $ref: '#/components/headers/RateLimit-Limit'
        RateLimit-Remaining:
          $ref: '#/components/headers/RateLimit-Remaining'
        RateLimit-Reset:
          $ref: '#/components/headers/RateLimit-Reset'
        Retry-After:
          $ref: '#/components/headers/Retry-After'
        Idempotent-Replayed:
          $ref: '#/components/headers/Idempotent-Replayed'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: >-
        The API key, created in Settings › API, in the header Authorization:
        Bearer <key>. The default.
    ApiKeyHeader:
      type: apiKey
      in: header
      name: x-api-key
      description: The same key, in the x-api-key header. Still accepted.

````

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