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

# Generate Messages

> Synchronously materialize ordinary recipient drafts while preserving AI personalization slots.

**CLI:**

```bash theme={null}
puffle campaign generate-message --id <id> --action <action>
puffle campaign generate-message --id <id> --action <action> --count <count> --prospect-offset <prospect-offset>
```

## Overview

This endpoint supports one POST action, selected by the `action` field in the body:

* **`resolve`** — synchronously resolves `%variables%` for ordinary sendable nodes and materializes recipient rows in `campaign_prospect_messages` as `draft`. For personalized email nodes, it prepares drafts from the saved skeleton and preserves `$ai {instruction}$` slots for the outbound copy operations. Manually edited rows are preserved; template changes flow through to unedited rows.

The campaign must be in `draft` status. Legacy `generate_ai` generation is not supported by this endpoint.

## AI agent notes

<Note>
  **`resolve` is safe to re-run.** It upserts on `(campaign_id, prospect_id, node_id)` and excludes manually-edited rows from the upsert — template changes propagate without clobbering user customizations. If the "find edited messages" query itself fails, the server falls back to `ignoreDuplicates: true` for the whole batch to avoid silently overwriting edits.

  **AI copy is a separate operation.** `resolve` does not seed recipient drafts from legacy `generated_examples` and does not ask a copywriter to fill personalization. Use `puffleAgent.previewOutboundCopy` to fill a reusable preview for up to five recipients, then use `puffleAgent.writeOutboundCopy` after explicit user approval to fill every remaining `$ai {instruction}$` slot. Previewed copy and manual edits are preserved.

  **Check status before launch.** Use `puffleAgent.getOutboundCopyStatus` to determine whether copy is `ready`, `needs_generation`, or `needs_attention`. A `needs_attention` response can include `copywriterError`; surface it and retry the copy operation after correcting any manually unresolved slot. For reading the draft outbox or inline-editing one message, use `GET` or `PATCH` on this same campaign messages path.
</Note>


## OpenAPI

````yaml post /api/campaigns/{id}/generate-messages
openapi: 3.0.3
info:
  title: Puffle API
  version: 1.0.0
  description: >-
    HTTP API for the Puffle GTM platform - Feed and Lead Finder search
    preparation, campaign-backed Outbound execution, Puffle Agent runs, sender
    management, and Unibox reply workflows. Designed for operation by both
    humans and autonomous AI agents. Agents should start at the [Agent
    Playbook](/guides/agent-playbook) which prescribes a workspace-context check
    (`GET /api/context`) and core user journeys with exact call ordering.
  contact:
    name: Puffle Support
    url: https://puffle.ai
servers:
  - url: https://app.puffle.ai
    description: Production
security:
  - bearerAuth: []
tags:
  - name: Campaigns
    description: >-
      Campaign-backed Outbound execution. These endpoints manage draft,
      launching, active, paused, and completed Outbound runs with sequence
      nodes, contacts, and sender accounts.
  - name: Accounts
    description: Signed-in user account, settings, and workspace context endpoints.
  - name: Billing
    description: Billing customer portal and account billing endpoints.
  - name: Senders
    description: >-
      Connected sender accounts, email inboxes, sending domains, DNS
      verification, warmup, and sender capacity.
  - name: Socials
    description: >-
      Read connected LinkedIn and X accounts, review current and archived posts,
      and reconcile publication status. Creating, editing, scheduling,
      publishing, OAuth connection, analytics, and mentions remain
      dashboard-managed workflows and are not part of the public Bearer-token
      API.
paths:
  /api/campaigns/{id}/generate-messages:
    post:
      tags:
        - Campaigns
      summary: Generate Messages
      description: >-
        Synchronously materializes ordinary draft messages and resolves
        deterministic variables. `$ai {prompt}$` personalization slots are
        preserved for Puffle Agent preview/full copy operations.
      operationId: generateCampaignMessages
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            description: Campaign UUID
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                action:
                  type: string
                  enum:
                    - resolve
                count:
                  description: Optionally resolve only the first N campaign leads.
                  type: integer
                  exclusiveMinimum: true
                  maximum: 100
                  minimum: 0
                prospect_offset:
                  default: 0
                  description: >-
                    Offset into the stable campaign lead order when `count` is
                    provided.
                  type: integer
                  minimum: 0
                  maximum: 9007199254740991
              required:
                - action
                - prospect_offset
              additionalProperties: false
              description: >-
                Synchronously materializes ordinary draft messages. `$ai
                {prompt}$` personalization slots are preserved for
                puffleAgent.previewOutboundCopy and
                puffleAgent.writeOutboundCopy.
      responses:
        '200':
          description: Messages were resolved or AI generation was queued.
          content:
            application/json:
              schema:
                type: object
                properties:
                  messages:
                    type: array
                    items:
                      type: object
                      properties:
                        id:
                          type: string
                          description: String value with endpoint-specific validation.
                        campaign_id:
                          type: string
                          description: String value with endpoint-specific validation.
                        prospect_id:
                          type: string
                          description: String value with endpoint-specific validation.
                        node_id:
                          type: string
                        subject:
                          type: string
                          nullable: true
                        body_text:
                          type: string
                          nullable: true
                        body_html:
                          type: string
                          nullable: true
                        status:
                          type: string
                          enum:
                            - draft
                            - pending
                            - sending
                            - sent
                        manually_edited:
                          type: boolean
                        created_at:
                          type: string
                          format: date-time
                      required:
                        - id
                        - campaign_id
                        - prospect_id
                        - node_id
                        - subject
                        - body_text
                        - status
                        - manually_edited
                        - created_at
                      additionalProperties: false
                required:
                  - messages
                additionalProperties: false
        '400':
          description: Validation failure or campaign cannot be modified.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
        '401':
          description: Missing or invalid authentication.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
        '404':
          description: Campaign or AI node was not found.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
        '500':
          description: Resolve or generation dispatch failed.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error:
                    type: string
                required:
                  - error
                additionalProperties: false
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: pk_live_...

````