openapi: 3.1.0
info:
  title: msend agent API
  version: "1.0.0"
  summary: Send and receive messages on Email, Slack and WhatsApp from your own AI agent.
  description: |
    msend is the channel provider for your agent. Your agent (LangGraph, CrewAI, the Claude Agents SDK, the OpenAI
    Agents SDK, or anything else) runs elsewhere. msend relays messages between your contacts and your agent.

    **Authentication.** Every operation except the two that serve this document needs an API key, sent as
    `Authorization: Bearer msk_...`. A key belongs to one agent, or to the whole organization (then pass `agent_id`
    or `channel_id` where an operation needs to know which agent or channel).

    **Conversations.** `conversation.id` is the stable session or thread id: key your agent's memory on it.

    **Receiving.** Choose webhooks (msend POSTs signed events to your URL, see `webhooks`) or polling
    (`list_events`). Both read the same event log.

    **Pagination.** List operations take `limit` (default 50, at most 200) and `after`, and return
    `{ "data": [...], "next_cursor": "..." | null }`. Pass `next_cursor` as `after` for the next page.

    **Errors.** Every error has the same shape, see `Error`. Status codes: 401 (missing, wrong or revoked key),
    403 (agent paused), 404, 409 (channel disabled), 422 (invalid request), 429 (rate limited: wait `Retry-After` seconds).

    **Idempotency.** Sending operations accept an `Idempotency-Key` header. Repeating a request with the same key and
    body returns the original message with `200`; the same key with a different body returns `422`.

    **Rate limits.** Each key is limited per minute (600 by default). A request over the limit gets `429` with a
    `Retry-After` header. Sends are also paced per channel by delaying them, never dropping them.
servers:
  - url: "{{BASE_URL}}/api/v1"
    description: This msend installation
security:
  - bearerAuth: []
tags:
  - name: Agent
  - name: Events
  - name: Conversations
  - name: Messages
  - name: Contacts
  - name: Consent
  - name: Templates
  - name: Webhook deliveries
  - name: Reference

paths:
  /agent:
    get:
      operationId: get_agent
      tags: [Agent]
      summary: Get the agent this API key belongs to. Use it to check that your key works and to read the agent's delivery mode.
      responses:
        "200": { description: The agent., content: { application/json: { schema: { $ref: "#/components/schemas/Agent" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/AgentPaused" }
        "404": { description: The key is organization-wide and has no agent., content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } }
        "429": { $ref: "#/components/responses/RateLimited" }

  /agent/test_event:
    post:
      operationId: send_test_event
      tags: [Agent]
      summary: Record a test `ping` event for this agent. Use it to check that your webhook receiver or poller works.
      responses:
        "202": { description: The recorded event., content: { application/json: { schema: { $ref: "#/components/schemas/Event" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { description: The key is organization-wide and has no agent., content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } }
        "429": { $ref: "#/components/responses/RateLimited" }

  /events:
    get:
      operationId: list_events
      tags: [Events]
      summary: Read the agent's event log, oldest first. Use it to poll for new messages and status changes when you do not use webhooks.
      description: Store the response's `cursor` after processing a page and send it as `after` on the next poll, so you only receive new events. An organization-wide key must pass `agent_id`.
      parameters:
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: agent_id
          in: query
          description: Which agent's events to read. Only needed for organization-wide keys.
          schema: { type: string, example: agt_x7k2m9q4w1z8a3b5c6d7e8f9 }
      responses:
        "200": { description: A page of events., content: { application/json: { schema: { $ref: "#/components/schemas/EventPage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /conversations:
    get:
      operationId: list_conversations
      tags: [Conversations]
      summary: List conversations the agent can see, oldest first. Use it to find a conversation id or to see what changed recently.
      parameters:
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: channel_id
          in: query
          description: Only conversations on this channel.
          schema: { type: string, example: chn_a1b2c3d4e5f6g7h8i9j0k1l2 }
        - name: status
          in: query
          schema: { type: string, enum: [open, closed, archived] }
        - name: updated_after
          in: query
          description: Only conversations changed after this time (ISO 8601).
          schema: { type: string, format: date-time }
      responses:
        "200": { description: A page of conversations., content: { application/json: { schema: { $ref: "#/components/schemas/ConversationPage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /conversations/{id}:
    get:
      operationId: get_conversation
      tags: [Conversations]
      summary: Get one conversation with its participants. For WhatsApp it also says whether the 24-hour window for free-form replies is still open.
      parameters:
        - { $ref: "#/components/parameters/ConversationId" }
      responses:
        "200": { description: The conversation., content: { application/json: { schema: { $ref: "#/components/schemas/ConversationDetail" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /conversations/{conversation_id}/messages:
    get:
      operationId: list_messages
      tags: [Messages]
      summary: Read the messages of a conversation, oldest first. Use it to load the history before answering.
      parameters:
        - { $ref: "#/components/parameters/ConversationIdInPath" }
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
      responses:
        "200": { description: A page of messages., content: { application/json: { schema: { $ref: "#/components/schemas/MessagePage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }
    post:
      operationId: send_message
      tags: [Messages]
      summary: Reply in an existing conversation. Use it to answer a person; the message is queued and delivered in the background.
      description: |
        Returns `202` with the queued message. Follow its progress through `message.status` events or `get_message`.
        Send either `text` or `parts`. On WhatsApp, free-form replies only work within 24 hours of the person's last
        message (see `Conversation.window`); outside it, send a `template` part.
      parameters:
        - { $ref: "#/components/parameters/ConversationIdInPath" }
        - { $ref: "#/components/parameters/IdempotencyKey" }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/SendMessageRequest" }
            examples:
              text: { summary: A plain text reply, value: { text: "Thanks, your refund is on its way.", in_reply_to: msg_p9o8i7u6y5t4r3e2w1q0a9s8 } }
              buttons:
                summary: A message with buttons
                value:
                  parts:
                    - { type: buttons, text: "Was that helpful?", buttons: [ { id: yes, title: "Yes" }, { id: no, title: "No" } ] }
      responses:
        "202": { description: The message was queued., content: { application/json: { schema: { $ref: "#/components/schemas/Message" } } } }
        "200": { description: This Idempotency-Key was used before with the same request; here is the original message., content: { application/json: { schema: { $ref: "#/components/schemas/Message" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/AgentPaused" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { description: The channel is disabled., content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /messages:
    post:
      operationId: start_conversation
      tags: [Messages]
      summary: Send the first message to someone, starting a conversation (or continuing the existing one with that person). Use it when nobody has written to you yet.
      description: |
        `channel_id` is needed unless the agent has exactly one channel. `to` is `{ kind, external_id }` (an email
        address, a phone number in international format, or a Slack user id with its team as `scope`), or a list of
        them for a group where the channel supports it. On WhatsApp, a person who has not written to you in the last
        24 hours can only be sent a `template` part.
      parameters:
        - { $ref: "#/components/parameters/IdempotencyKey" }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/StartConversationRequest" }
            examples:
              email: { summary: An email, value: { channel_id: chn_a1b2c3d4e5f6g7h8i9j0k1l2, to: { kind: email, external_id: "person@example.com" }, subject: "Your order", text: "Hello, your order has shipped." } }
      responses:
        "202": { description: The message was queued., content: { application/json: { schema: { $ref: "#/components/schemas/Message" } } } }
        "200": { description: This Idempotency-Key was used before with the same request; here is the original message., content: { application/json: { schema: { $ref: "#/components/schemas/Message" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/AgentPaused" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { description: The channel is disabled., content: { application/json: { schema: { $ref: "#/components/schemas/Error" } } } }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /messages/{id}:
    get:
      operationId: get_message
      tags: [Messages]
      summary: Get one message, including its delivery status and, if it failed, why. Use it to check whether a message you sent was delivered.
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, example: msg_p9o8i7u6y5t4r3e2w1q0a9s8 }
      responses:
        "200": { description: The message., content: { application/json: { schema: { $ref: "#/components/schemas/Message" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /contacts:
    get:
      operationId: list_contacts
      tags: [Contacts]
      summary: List the organization's contacts with their addresses. Use it to look someone up before writing to them.
      parameters:
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: updated_after
          in: query
          schema: { type: string, format: date-time }
      responses:
        "200": { description: A page of contacts., content: { application/json: { schema: { $ref: "#/components/schemas/ContactPage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /contacts/{id}:
    get:
      operationId: get_contact
      tags: [Contacts]
      summary: Get one contact with all their addresses.
      parameters:
        - { $ref: "#/components/parameters/ContactId" }
      responses:
        "200": { description: The contact., content: { application/json: { schema: { $ref: "#/components/schemas/Contact" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }
    delete:
      operationId: erase_contact
      tags: [Contacts]
      summary: Erase a contact's personal data on request (their addresses, messages, files and raw events). This cannot be undone; only use it when the person asked to be forgotten.
      description: Returns `202`; the erasure runs in the background. The contact's opt-outs are kept, reduced to a hash of the address, so they are still honoured.
      parameters:
        - { $ref: "#/components/parameters/ContactId" }
      responses:
        "202": { description: The erasure was queued., content: { application/json: { schema: { $ref: "#/components/schemas/ErasureAccepted" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /consents:
    get:
      operationId: get_consent
      tags: [Consent]
      summary: Read recorded consent (opt-in and opt-out) for a person, or list all of it. Check it before starting a conversation on WhatsApp.
      description: Give `kind` and `external_id` (and `scope` for Slack) to look up one person; without them all consents are listed.
      parameters:
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: kind
          in: query
          schema: { type: string, enum: [email, slack, whatsapp] }
        - name: external_id
          in: query
          schema: { type: string, example: "+14155550123" }
        - name: scope
          in: query
          description: The Slack team id, for Slack addresses.
          schema: { type: string }
        - name: channel_kind
          in: query
          schema: { type: string, enum: [email, slack, whatsapp] }
      responses:
        "200": { description: A page of consents., content: { application/json: { schema: { $ref: "#/components/schemas/ConsentPage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }
    put:
      operationId: set_consent
      tags: [Consent]
      summary: Record that a person opted in or opted out. Use it when your own form or system collected their choice.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/SetConsentRequest" }
            examples:
              optIn: { value: { kind: whatsapp, external_id: "+14155550123", status: opted_in, source: form, note: "Checked the box on the signup page" } }
      responses:
        "200": { description: The recorded consent., content: { application/json: { schema: { $ref: "#/components/schemas/Consent" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /channels/{channel_id}/templates:
    get:
      operationId: list_templates
      tags: [Templates]
      summary: List a WhatsApp channel's message templates and whether each is approved. Use it to pick a template when the 24-hour window is closed.
      parameters:
        - name: channel_id
          in: path
          required: true
          schema: { type: string, example: chn_a1b2c3d4e5f6g7h8i9j0k1l2 }
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: status
          in: query
          schema: { type: string, enum: [approved, pending, rejected, paused, disabled] }
        - name: name
          in: query
          schema: { type: string }
      responses:
        "200": { description: A page of templates., content: { application/json: { schema: { $ref: "#/components/schemas/TemplatePage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /templates/{id}:
    get:
      operationId: get_template
      tags: [Templates]
      summary: Get one WhatsApp template with its body and components.
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, example: tpl_c3d4e5f6g7h8i9j0k1l2m3n4 }
      responses:
        "200": { description: The template., content: { application/json: { schema: { $ref: "#/components/schemas/Template" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /webhook_deliveries:
    get:
      operationId: list_webhook_deliveries
      tags: [Webhook deliveries]
      summary: List attempts to deliver events to your webhook, with their status and last error. Use it to debug a receiver that is not getting events.
      parameters:
        - { $ref: "#/components/parameters/Limit" }
        - { $ref: "#/components/parameters/After" }
        - name: status
          in: query
          schema: { type: string, enum: [pending, delivered, failed, dead] }
        - name: agent_id
          in: query
          description: Only needed for organization-wide keys.
          schema: { type: string }
      responses:
        "200": { description: A page of deliveries., content: { application/json: { schema: { $ref: "#/components/schemas/WebhookDeliveryPage" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /webhook_deliveries/{id}/replay:
    post:
      operationId: replay_webhook_delivery
      tags: [Webhook deliveries]
      summary: Send an event to your webhook again, with a fresh set of retries. Use it after you fixed a receiver that was failing.
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, example: whd_e5f6g7h8i9j0k1l2m3n4o5p6 }
        - name: agent_id
          in: query
          description: Only needed for organization-wide keys.
          schema: { type: string }
      responses:
        "202": { description: The delivery was queued again., content: { application/json: { schema: { $ref: "#/components/schemas/WebhookDelivery" } } } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "422": { $ref: "#/components/responses/Invalid" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /openapi.yaml:
    get:
      operationId: get_openapi_yaml
      tags: [Reference]
      summary: This document as YAML. No API key needed.
      security: []
      responses:
        "200": { description: The OpenAPI document., content: { application/yaml: { schema: { type: string } } } }

  /openapi.json:
    get:
      operationId: get_openapi_json
      tags: [Reference]
      summary: This document as JSON. No API key needed.
      security: []
      responses:
        "200": { description: The OpenAPI document., content: { application/json: { schema: { type: object } } } }

webhooks:
  message.received:
    post:
      operationId: webhook_message_received
      summary: A person sent a message. Delivered to your webhook URL when the agent uses webhook delivery.
      description: |
        For group conversations only messages that address the agent are delivered under the default reply policy;
        `message.addressed_to_agent` tells you whether the agent was addressed.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/WebhookMessageReceived" }
      parameters:
        - { $ref: "#/components/parameters/WebhookEventId" }
        - { $ref: "#/components/parameters/WebhookTimestamp" }
        - { $ref: "#/components/parameters/WebhookSignature" }
      responses:
        "200": { description: Answer with any 2xx within 10 seconds. Anything else is retried. }
  message.status:
    post:
      operationId: webhook_message_status
      summary: A message you sent changed status (sending, sent, delivered, read or failed).
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/WebhookMessageStatus" }
      parameters:
        - { $ref: "#/components/parameters/WebhookEventId" }
        - { $ref: "#/components/parameters/WebhookTimestamp" }
        - { $ref: "#/components/parameters/WebhookSignature" }
      responses:
        "200": { description: Answer with any 2xx within 10 seconds. }
  conversation.created:
    post:
      operationId: webhook_conversation_created
      summary: A new conversation started.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/WebhookConversationCreated" }
      parameters:
        - { $ref: "#/components/parameters/WebhookEventId" }
        - { $ref: "#/components/parameters/WebhookTimestamp" }
        - { $ref: "#/components/parameters/WebhookSignature" }
      responses:
        "200": { description: Answer with any 2xx within 10 seconds. }
  channel.status:
    post:
      operationId: webhook_channel_status
      summary: A channel went into an error state (for example an expired token).
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/WebhookChannelStatus" }
      parameters:
        - { $ref: "#/components/parameters/WebhookEventId" }
        - { $ref: "#/components/parameters/WebhookTimestamp" }
        - { $ref: "#/components/parameters/WebhookSignature" }
      responses:
        "200": { description: Answer with any 2xx within 10 seconds. }
  ping:
    post:
      operationId: webhook_ping
      summary: A test event created by send_test_event.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/WebhookPing" }
      parameters:
        - { $ref: "#/components/parameters/WebhookEventId" }
        - { $ref: "#/components/parameters/WebhookTimestamp" }
        - { $ref: "#/components/parameters/WebhookSignature" }
      responses:
        "200": { description: Answer with any 2xx within 10 seconds. }

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: An API key (`msk_` followed by 32 characters), created on the agent's page. It is shown once.

  parameters:
    Limit:
      name: limit
      in: query
      description: How many items to return (default 50, at most 200).
      schema: { type: integer, minimum: 1, maximum: 200, default: 50 }
    After:
      name: after
      in: query
      description: The `next_cursor` of the previous page.
      schema: { type: string }
    ConversationId:
      name: id
      in: path
      required: true
      schema: { type: string, example: cnv_q1w2e3r4t5y6u7i8o9p0a1s2 }
    ConversationIdInPath:
      name: conversation_id
      in: path
      required: true
      schema: { type: string, example: cnv_q1w2e3r4t5y6u7i8o9p0a1s2 }
    ContactId:
      name: id
      in: path
      required: true
      schema: { type: string, example: con_d4f5g6h7j8k9l0z1x2c3v4b5 }
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      description: A unique string per logical send (for example a UUID). Repeating the request with the same key returns the original message.
      schema: { type: string, maxLength: 255 }
    WebhookEventId:
      name: Msend-Event-Id
      in: header
      required: true
      description: The event id. Deduplicate on it, events can arrive more than once.
      schema: { type: string }
    WebhookTimestamp:
      name: Msend-Timestamp
      in: header
      required: true
      description: Unix seconds when the request was signed. Reject requests more than 5 minutes old.
      schema: { type: string }
    WebhookSignature:
      name: Msend-Signature
      in: header
      required: true
      description: "`v1=` followed by the hex HMAC-SHA256 of `<timestamp>.<raw body>` with the agent's webhook secret. Compare in constant time."
      schema: { type: string, example: "v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd" }

  responses:
    Unauthorized:
      description: The API key is missing, wrong or revoked.
      content: { application/json: { schema: { $ref: "#/components/schemas/Error" }, example: { error: { type: invalid_api_key, message: "The API key is invalid or has been revoked.", details: {} } } } }
    AgentPaused:
      description: The agent is paused.
      content: { application/json: { schema: { $ref: "#/components/schemas/Error" }, example: { error: { type: agent_paused, message: "This agent is paused.", details: {} } } } }
    NotFound:
      description: The resource does not exist, or this key cannot see it.
      content: { application/json: { schema: { $ref: "#/components/schemas/Error" }, example: { error: { type: not_found, message: "Resource not found.", details: {} } } } }
    Invalid:
      description: The request is not valid. `details` says which parameter.
      content: { application/json: { schema: { $ref: "#/components/schemas/Error" }, example: { error: { type: invalid_request, message: "Send text or parts.", details: { param: text } } } } }
    RateLimited:
      description: Too many requests. Wait `Retry-After` seconds.
      headers:
        Retry-After: { description: Seconds to wait., schema: { type: integer } }
      content: { application/json: { schema: { $ref: "#/components/schemas/Error" }, example: { error: { type: rate_limited, message: "Too many requests. Retry in 20 seconds.", details: { retry_after: 20 } } } } }

  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [type, message, details]
          properties:
            type: { type: string, description: "A stable code, for example `invalid_api_key`, `not_found`, `invalid_request`, `idempotency_conflict`, `channel_unavailable`, `rate_limited`." }
            message: { type: string }
            details: { type: object, additionalProperties: true }

    Agent:
      type: object
      required: [id, name, status, delivery_mode, webhook_url, created_at]
      properties:
        id: { type: string, example: agt_x7k2m9q4w1z8a3b5c6d7e8f9 }
        name: { type: string, example: Support agent }
        status: { type: string, enum: [active, paused] }
        delivery_mode: { type: string, enum: [webhook, poll] }
        webhook_url: { type: [string, "null"], example: "https://agent.example.com/msend" }
        created_at: { type: string, format: date-time }
      example: { id: agt_x7k2m9q4w1z8a3b5c6d7e8f9, name: Support agent, status: active, delivery_mode: poll, webhook_url: null, created_at: "2026-01-05T10:00:00Z" }

    Event:
      type: object
      description: One entry of the event log, the same envelope as a webhook body.
      required: [id, kind, created_at, data]
      properties:
        id: { type: string, example: evt_m1n2b3v4c5x6z7a8s9d0f1g2 }
        kind: { type: string, enum: [message.received, message.status, conversation.created, channel.status, ping] }
        created_at: { type: string, format: date-time }
        data: { type: object, description: "The payload for this kind, see the `webhooks` section.", additionalProperties: true }
      example: { id: evt_m1n2b3v4c5x6z7a8s9d0f1g2, kind: ping, created_at: "2026-01-05T10:00:00Z", data: { message: "This is a test event from msend." } }

    EventPage:
      type: object
      required: [data, next_cursor, cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Event" } }
        next_cursor: { type: [string, "null"], description: "More events are waiting: pass this as `after` to read them. Null on the last page." }
        cursor: { type: [string, "null"], description: "Where to resume polling: the position after the last event in `data`, or the `after` you sent when nothing new arrived. Store it and send it as `after` next time." }

    MessagePart:
      description: One piece of a message. `type` decides which other fields apply.
      oneOf:
        - { $ref: "#/components/schemas/TextPart" }
        - { $ref: "#/components/schemas/MediaPart" }
        - { $ref: "#/components/schemas/LocationPart" }
        - { $ref: "#/components/schemas/ButtonsPart" }
        - { $ref: "#/components/schemas/ListPart" }
        - { $ref: "#/components/schemas/TemplatePart" }
        - { $ref: "#/components/schemas/ReactionPart" }
    TextPart:
      type: object
      required: [type, text]
      properties: { type: { const: text }, text: { type: string } }
      additionalProperties: false
    MediaPart:
      type: object
      description: An image, audio, video or file. Reference an uploaded file by `attachment_id`, or a provider media id by `media_ref`.
      required: [type]
      properties:
        type: { type: string, enum: [image, audio, video, file] }
        attachment_id: { type: string }
        media_ref: { type: string }
        mime_type: { type: string }
        filename: { type: string }
        caption: { type: string }
      additionalProperties: false
    LocationPart:
      type: object
      required: [type, latitude, longitude]
      properties: { type: { const: location }, latitude: { type: number }, longitude: { type: number }, name: { type: string } }
      additionalProperties: false
    ButtonsPart:
      type: object
      required: [type, text, buttons]
      properties:
        type: { const: buttons }
        text: { type: string }
        buttons:
          type: array
          items: { type: object, required: [id, title], properties: { id: { type: string }, title: { type: string } } }
      additionalProperties: false
    ListPart:
      type: object
      required: [type, text, items]
      properties:
        type: { const: list }
        text: { type: string }
        items:
          type: array
          items: { type: object, required: [id, title], properties: { id: { type: string }, title: { type: string }, description: { type: string } } }
      additionalProperties: false
    TemplatePart:
      type: object
      description: A WhatsApp template message (needed outside the 24-hour window). `variables` fill `{{1}}` or `{{name}}`.
      required: [type, name, language]
      properties:
        type: { const: template }
        name: { type: string }
        language: { type: string, example: en }
        variables: { oneOf: [ { type: array, items: { type: string } }, { type: object, additionalProperties: { type: string } } ] }
        header: { type: object, additionalProperties: true }
        buttons: { type: array, items: { type: object, required: [index], properties: { index: { type: integer }, payload: { type: string }, text: { type: string } } } }
      additionalProperties: false
    ReactionPart:
      type: object
      required: [type, emoji, message_id]
      properties: { type: { const: reaction }, emoji: { type: string }, message_id: { type: string } }
      additionalProperties: false

    Message:
      type: object
      required: [id, conversation_id, direction, status, text, parts, addressed_to_agent, in_reply_to, sender, error_code, error_detail, metadata, sent_at, created_at]
      properties:
        id: { type: string, example: msg_p9o8i7u6y5t4r3e2w1q0a9s8 }
        conversation_id: { type: string }
        direction: { type: string, enum: [inbound, outbound] }
        status: { type: string, enum: [received, queued, sending, sent, delivered, read, failed] }
        text: { type: string, description: "Always present: a plain-text rendering of the message for language models." }
        parts: { type: array, items: { $ref: "#/components/schemas/MessagePart" } }
        addressed_to_agent: { type: boolean }
        in_reply_to: { type: [string, "null"] }
        sender:
          oneOf:
            - { type: "null" }
            - { $ref: "#/components/schemas/Sender" }
        error_code: { type: [string, "null"], description: "Why a message failed, for example `window_closed`, `consent_blocked`, `invalid_recipient`, `bounced`." }
        error_detail: { type: [string, "null"] }
        metadata: { type: object, additionalProperties: true, description: Your own metadata for messages you sent; empty for inbound messages. }
        sent_at: { type: [string, "null"], format: date-time }
        created_at: { type: string, format: date-time }
      example:
        id: msg_p9o8i7u6y5t4r3e2w1q0a9s8
        conversation_id: cnv_q1w2e3r4t5y6u7i8o9p0a1s2
        direction: inbound
        status: received
        text: "Hello, where is my order?"
        parts: [ { type: text, text: "Hello, where is my order?" } ]
        addressed_to_agent: true
        in_reply_to: null
        sender: { contact_id: con_d4f5g6h7j8k9l0z1x2c3v4b5, kind: email, scope: "", external_id: "person@example.com", display_name: "Ada Lovelace" }
        error_code: null
        error_detail: null
        metadata: {}
        sent_at: "2026-01-05T10:00:00Z"
        created_at: "2026-01-05T10:00:00Z"
    Sender:
      type: object
      required: [contact_id, kind, scope, external_id, display_name]
      properties:
        contact_id: { type: string }
        kind: { type: string, enum: [email, slack, whatsapp] }
        scope: { type: string }
        external_id: { type: string }
        display_name: { type: [string, "null"] }
    MessagePage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Message" } }
        next_cursor: { type: [string, "null"] }

    SendMessageRequest:
      type: object
      description: Send `text` or `parts`, not both.
      properties:
        text: { type: string, description: A plain text message. }
        parts: { type: array, minItems: 1, items: { $ref: "#/components/schemas/MessagePart" } }
        metadata: { type: object, additionalProperties: true, description: Stored with the message and returned to you. }
        in_reply_to: { type: string, description: The id of the message you are answering. }
        thread_key: { type: string, description: A thread to reply in, for channels that support threads (Slack). }
    StartConversationRequest:
      type: object
      required: [to]
      properties:
        channel_id: { type: string, description: Required unless the agent has exactly one channel. }
        to:
          description: Who to write to; a list starts a group where the channel supports it.
          oneOf:
            - { $ref: "#/components/schemas/Recipient" }
            - { type: array, minItems: 1, items: { $ref: "#/components/schemas/Recipient" } }
        subject: { type: string, description: The email subject or conversation title. }
        text: { type: string }
        parts: { type: array, minItems: 1, items: { $ref: "#/components/schemas/MessagePart" } }
        metadata: { type: object, additionalProperties: true }
    Recipient:
      type: object
      required: [external_id]
      properties:
        kind: { type: string, enum: [email, slack, whatsapp], description: Defaults to the channel's kind. }
        external_id: { type: string, description: "An email address, a phone number in international format (+14155550123), or a Slack user id.", example: "person@example.com" }
        scope: { type: string, description: The Slack team id (Slack only). }
        display_name: { type: string }

    Conversation:
      type: object
      required: [id, channel_id, kind, subject, thread_key, status, external_id, last_message_at, last_inbound_at, window, created_at, updated_at]
      properties:
        id: { type: string, example: cnv_q1w2e3r4t5y6u7i8o9p0a1s2, description: "The stable session or thread id. Key your agent's memory on it." }
        channel_id: { type: string }
        kind: { type: string, enum: [direct, group, channel] }
        subject: { type: [string, "null"] }
        thread_key: { type: string, description: "A Slack thread timestamp; empty otherwise." }
        status: { type: string, enum: [open, closed, archived] }
        external_id: { type: string, description: The provider's id for this chat. }
        last_message_at: { type: [string, "null"], format: date-time }
        last_inbound_at: { type: [string, "null"], format: date-time }
        window:
          oneOf:
            - { type: "null" }
            - { $ref: "#/components/schemas/Window" }
          description: WhatsApp's 24-hour window for free-form replies; null for channels that have none.
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
      example:
        id: cnv_q1w2e3r4t5y6u7i8o9p0a1s2
        channel_id: chn_a1b2c3d4e5f6g7h8i9j0k1l2
        kind: direct
        subject: Your order
        thread_key: ""
        status: open
        external_id: "14155550123"
        last_message_at: "2026-01-05T10:00:00Z"
        last_inbound_at: "2026-01-05T10:00:00Z"
        window: { open: true, expires_at: "2026-01-06T10:00:00Z" }
        created_at: "2026-01-05T09:00:00Z"
        updated_at: "2026-01-05T10:00:00Z"
    Window:
      type: object
      required: [open, expires_at]
      properties:
        open: { type: boolean }
        expires_at: { type: [string, "null"], format: date-time, description: "last_inbound_at plus 24 hours; null if the person never wrote." }
    ConversationDetail:
      allOf:
        - { $ref: "#/components/schemas/Conversation" }
        - type: object
          required: [participants]
          properties:
            participants:
              type: array
              items: { $ref: "#/components/schemas/Participant" }
    Participant:
      type: object
      required: [contact_id, kind, scope, external_id, display_name, role, joined_at, left_at]
      properties:
        contact_id: { type: string }
        kind: { type: string }
        scope: { type: string }
        external_id: { type: string }
        display_name: { type: [string, "null"] }
        role: { type: string, enum: [member, admin] }
        joined_at: { type: string, format: date-time }
        left_at: { type: [string, "null"], format: date-time }
    ConversationPage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Conversation" } }
        next_cursor: { type: [string, "null"] }

    Contact:
      type: object
      required: [id, name, metadata, identities, created_at]
      properties:
        id: { type: string, example: con_d4f5g6h7j8k9l0z1x2c3v4b5 }
        name: { type: [string, "null"] }
        metadata: { type: object, additionalProperties: true }
        identities: { type: array, items: { $ref: "#/components/schemas/Identity" } }
        created_at: { type: string, format: date-time }
      example: { id: con_d4f5g6h7j8k9l0z1x2c3v4b5, name: Ada Lovelace, metadata: {}, identities: [ { kind: email, scope: "", external_id: "person@example.com", display_name: Ada Lovelace, handle: null } ], created_at: "2026-01-05T09:00:00Z" }
    Identity:
      type: object
      required: [kind, scope, external_id, display_name, handle]
      properties:
        kind: { type: string, enum: [email, slack, whatsapp] }
        scope: { type: string }
        external_id: { type: string }
        display_name: { type: [string, "null"] }
        handle: { type: [string, "null"] }
    ContactPage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Contact" } }
        next_cursor: { type: [string, "null"] }
    ErasureAccepted:
      type: object
      required: [id, status]
      properties:
        id: { type: string }
        status: { type: string, enum: [erasing] }

    Consent:
      type: object
      required: [identity, channel_kind, status, source, recorded_at, note]
      properties:
        identity: { type: object, required: [kind, scope, external_id], properties: { kind: { type: string }, scope: { type: string }, external_id: { type: string } } }
        channel_kind: { type: string, enum: [email, slack, whatsapp] }
        status: { type: string, enum: [opted_in, opted_out] }
        source: { type: string, enum: [api, inbound_keyword, import, form] }
        recorded_at: { type: string, format: date-time }
        note: { type: [string, "null"] }
      example: { identity: { kind: whatsapp, scope: "", external_id: "+14155550123" }, channel_kind: whatsapp, status: opted_in, source: form, recorded_at: "2026-01-05T10:00:00Z", note: null }
    ConsentPage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Consent" } }
        next_cursor: { type: [string, "null"] }
    SetConsentRequest:
      type: object
      required: [kind, external_id, status]
      properties:
        kind: { type: string, enum: [email, slack, whatsapp] }
        external_id: { type: string }
        scope: { type: string }
        status: { type: string, enum: [opted_in, opted_out] }
        channel_kind: { type: string, enum: [email, slack, whatsapp], description: Defaults to `kind`. }
        source: { type: string, enum: [api, import, form], default: api }
        note: { type: string }

    Template:
      type: object
      required: [id, channel_id, name, language, category, status, body, components, rejected_reason, synced_at]
      properties:
        id: { type: string, example: tpl_c3d4e5f6g7h8i9j0k1l2m3n4 }
        channel_id: { type: string }
        name: { type: string, example: order_shipped }
        language: { type: string, example: en }
        category: { type: string, enum: [marketing, utility, authentication] }
        status: { type: string, enum: [approved, pending, rejected, paused, disabled] }
        body: { type: string, example: "Hello {{1}}, your order {{2}} has shipped." }
        components: { type: array, items: { type: object, additionalProperties: true }, description: "Meta's definition of the template." }
        rejected_reason: { type: [string, "null"] }
        synced_at: { type: [string, "null"], format: date-time }
    TemplatePage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/Template" } }
        next_cursor: { type: [string, "null"] }

    WebhookDelivery:
      type: object
      required: [id, event_id, status, attempts, next_attempt_at, response_code, last_error, delivered_at, created_at]
      properties:
        id: { type: string, example: whd_e5f6g7h8i9j0k1l2m3n4o5p6 }
        event_id: { type: string }
        status: { type: string, enum: [pending, delivered, failed, dead], description: "`failed` will be retried; `dead` gave up after 8 attempts." }
        attempts: { type: integer }
        next_attempt_at: { type: [string, "null"], format: date-time }
        response_code: { type: [integer, "null"] }
        last_error: { type: [string, "null"] }
        delivered_at: { type: [string, "null"], format: date-time }
        created_at: { type: string, format: date-time }
    WebhookDeliveryPage:
      type: object
      required: [data, next_cursor]
      properties:
        data: { type: array, items: { $ref: "#/components/schemas/WebhookDelivery" } }
        next_cursor: { type: [string, "null"] }

    WebhookEnvelope:
      type: object
      description: "The body of every webhook request. Retries after about 10 seconds, 1 minute, 5 minutes, 30 minutes, 2 hours, 6 hours and 12 hours; the 8th failure marks the delivery dead."
      required: [id, kind, created_at, data]
      properties:
        id: { type: string }
        kind: { type: string }
        created_at: { type: string, format: date-time }
    WebhookMessageReceived:
      allOf:
        - { $ref: "#/components/schemas/WebhookEnvelope" }
        - type: object
          properties:
            kind: { const: message.received }
            data:
              type: object
              required: [conversation, message, sender, participants]
              properties:
                conversation: { $ref: "#/components/schemas/WebhookConversation" }
                message:
                  type: object
                  required: [id, text, parts, addressed_to_agent, in_reply_to, sent_at]
                  properties:
                    id: { type: string }
                    text: { type: string }
                    parts: { type: array, items: { $ref: "#/components/schemas/MessagePart" } }
                    addressed_to_agent: { type: boolean }
                    in_reply_to: { type: [string, "null"] }
                    sent_at: { type: [string, "null"], format: date-time }
                sender: { $ref: "#/components/schemas/WebhookPerson" }
                participants: { type: array, items: { $ref: "#/components/schemas/WebhookPerson" } }
    WebhookConversation:
      type: object
      required: [id, kind, subject, channel]
      properties:
        id: { type: string }
        kind: { type: string, enum: [direct, group, channel] }
        subject: { type: [string, "null"] }
        channel: { type: object, required: [id, kind], properties: { id: { type: string }, kind: { type: string } } }
    WebhookPerson:
      type: object
      required: [contact_id, identity]
      properties:
        contact_id: { type: string }
        identity:
          type: object
          required: [kind, scope, external_id, display_name]
          properties:
            kind: { type: string }
            scope: { type: string }
            external_id: { type: string }
            display_name: { type: [string, "null"] }
    WebhookMessageStatus:
      allOf:
        - { $ref: "#/components/schemas/WebhookEnvelope" }
        - type: object
          properties:
            kind: { const: message.status }
            data:
              type: object
              required: [conversation, message, occurred_at]
              properties:
                conversation: { type: object, required: [id], properties: { id: { type: string } } }
                message:
                  type: object
                  required: [id, status, error_code, error_detail]
                  properties:
                    id: { type: string }
                    status: { type: string, enum: [sending, sent, delivered, read, failed] }
                    error_code: { type: [string, "null"] }
                    error_detail: { type: [string, "null"] }
                occurred_at: { type: string, format: date-time }
    WebhookConversationCreated:
      allOf:
        - { $ref: "#/components/schemas/WebhookEnvelope" }
        - type: object
          properties:
            kind: { const: conversation.created }
            data:
              type: object
              required: [conversation]
              properties:
                conversation: { $ref: "#/components/schemas/WebhookConversation" }
    WebhookChannelStatus:
      allOf:
        - { $ref: "#/components/schemas/WebhookEnvelope" }
        - type: object
          properties:
            kind: { const: channel.status }
            data:
              type: object
              required: [channel, error]
              properties:
                channel: { type: object, required: [id, kind, status], properties: { id: { type: string }, kind: { type: string }, status: { type: string } } }
                error: { type: [string, "null"] }
    WebhookPing:
      allOf:
        - { $ref: "#/components/schemas/WebhookEnvelope" }
        - type: object
          properties:
            kind: { const: ping }
            data: { type: object, properties: { message: { type: string } } }
