{
  "openapi": "3.1.0",
  "info": {
    "title": "messenger.ai provider bridge",
    "version": "1.1.0",
    "description": "Implement this contract on your own HTTPS adapter. The private messenger.ai app calls it using a server-held bearer token and workspace identifier. Provider OAuth, permissions and webhooks belong to your adapter. This contract does not create a public ingress route on messenger.ai."
  },
  "security": [
    {
      "bridgeToken": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bridgeToken": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "Customer": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "email": {
            "type": "string"
          },
          "phone": {
            "type": "string"
          },
          "company": {
            "type": "string"
          },
          "address": {
            "$ref": "#/components/schemas/Address"
          },
          "buyerId": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "name"
        ]
      },
      "Conversation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "contactId": {
            "type": "string"
          },
          "channel": {
            "type": "string"
          },
          "subject": {
            "type": "string"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          },
          "order": {
            "description": "Legacy inline order; separate orders plus orderId are preferred.",
            "allOf": [
              {
                "$ref": "#/components/schemas/Order"
              }
            ]
          },
          "orderId": {
            "type": "string",
            "description": "Exactly one queried order from the orders array. Its contactId must match this conversation."
          }
        },
        "required": [
          "id",
          "contactId",
          "channel",
          "subject",
          "updatedAt"
        ]
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "conversationId": {
            "type": "string"
          },
          "body": {
            "type": "string"
          },
          "kind": {
            "enum": [
              "incoming",
              "outgoing"
            ]
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "delivery": {
            "enum": [
              "received",
              "sent",
              "delivered",
              "accepted",
              "failed"
            ]
          },
          "clientMessageId": {
            "type": "string",
            "description": "For outgoing messages created by messenger.ai, echo the original POST /messages id. This correlates delivery updates without duplicate imports."
          }
        },
        "required": [
          "id",
          "conversationId",
          "body",
          "kind",
          "createdAt"
        ]
      },
      "Attachment": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "size": {
            "type": "integer"
          },
          "contentType": {
            "type": "string"
          },
          "dataBase64": {
            "type": "string",
            "contentEncoding": "base64"
          }
        },
        "required": [
          "id",
          "name",
          "size",
          "contentType",
          "dataBase64"
        ]
      },
      "Address": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "line1": {
            "type": "string"
          },
          "line2": {
            "type": "string"
          },
          "city": {
            "type": "string"
          },
          "county": {
            "type": "string"
          },
          "postcode": {
            "type": "string"
          },
          "country": {
            "type": "string"
          },
          "phone": {
            "type": "string"
          }
        },
        "required": []
      },
      "Product": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "sku": {
            "type": "string"
          },
          "price": {
            "type": "string"
          },
          "productId": {
            "type": "string"
          },
          "listingId": {
            "type": "string"
          },
          "brand": {
            "type": "string"
          },
          "gtin": {
            "type": "string"
          },
          "variation": {
            "type": "string"
          },
          "condition": {
            "type": "string"
          },
          "imageUrl": {
            "type": "string"
          },
          "productUrl": {
            "type": "string"
          },
          "tax": {
            "type": "string"
          },
          "discount": {
            "type": "string"
          },
          "lineTotal": {
            "type": "string"
          },
          "qty": {
            "type": "integer",
            "minimum": 1
          },
          "attributes": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          }
        },
        "required": [
          "name",
          "sku",
          "qty",
          "price"
        ]
      },
      "TrackingEvent": {
        "type": "object",
        "properties": {
          "time": {
            "type": "string",
            "format": "date-time"
          },
          "status": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "location": {
            "type": "string"
          }
        },
        "required": [
          "time",
          "status"
        ]
      },
      "Shipment": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "carrier": {
            "type": "string"
          },
          "service": {
            "type": "string"
          },
          "trackingNumber": {
            "type": "string"
          },
          "trackingUrl": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "dispatchedAt": {
            "type": "string",
            "format": "date-time"
          },
          "estimatedDelivery": {
            "type": "string",
            "format": "date-time"
          },
          "deliveredAt": {
            "type": "string",
            "format": "date-time"
          },
          "events": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/TrackingEvent"
            }
          }
        },
        "required": []
      },
      "Order": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "orderNumber": {
            "type": "string"
          },
          "contactId": {
            "type": "string"
          },
          "channel": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "total": {
            "type": "string"
          },
          "currency": {
            "type": "string"
          },
          "subtotal": {
            "type": "string"
          },
          "shippingCost": {
            "type": "string"
          },
          "tax": {
            "type": "string"
          },
          "discount": {
            "type": "string"
          },
          "refundTotal": {
            "type": "string"
          },
          "paymentStatus": {
            "type": "string"
          },
          "paymentMethod": {
            "type": "string"
          },
          "transactionId": {
            "type": "string"
          },
          "buyerUsername": {
            "type": "string"
          },
          "buyerEmail": {
            "type": "string"
          },
          "buyerPhone": {
            "type": "string"
          },
          "fulfilment": {
            "type": "string"
          },
          "returnStatus": {
            "type": "string"
          },
          "customerNote": {
            "type": "string"
          },
          "tracking": {
            "type": "string"
          },
          "carrier": {
            "type": "string"
          },
          "orderedAt": {
            "type": "string",
            "format": "date-time"
          },
          "paidAt": {
            "type": "string",
            "format": "date-time"
          },
          "dispatchedAt": {
            "type": "string",
            "format": "date-time"
          },
          "completedAt": {
            "type": "string",
            "format": "date-time"
          },
          "cancelledAt": {
            "type": "string",
            "format": "date-time"
          },
          "shippingAddress": {
            "$ref": "#/components/schemas/Address"
          },
          "billingAddress": {
            "$ref": "#/components/schemas/Address"
          },
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Product"
            }
          },
          "shipments": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Shipment"
            }
          },
          "metadata": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          }
        },
        "required": [
          "id",
          "contactId",
          "channel"
        ]
      },
      "Mailbox": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "provider": {
            "enum": [
              "google",
              "microsoft",
              "custom"
            ]
          },
          "aliases": {
            "type": "array",
            "items": {
              "type": "string",
              "format": "email"
            }
          },
          "config": {
            "type": "object",
            "properties": {
              "inboundProtocol": {
                "enum": [
                  "api",
                  "imap",
                  "pop3"
                ]
              },
              "inboundHost": {
                "type": "string"
              },
              "inboundPort": {
                "type": "integer",
                "minimum": 1,
                "maximum": 65535
              },
              "inboundSecurity": {
                "enum": [
                  "tls",
                  "starttls"
                ]
              },
              "outboundHost": {
                "type": "string"
              },
              "outboundPort": {
                "type": "integer",
                "minimum": 1,
                "maximum": 65535
              },
              "outboundSecurity": {
                "enum": [
                  "tls",
                  "starttls"
                ]
              },
              "credentialRef": {
                "type": "string",
                "description": "Opaque reference to a secret held in the connector, never a raw password."
              }
            },
            "required": []
          },
          "returnUrl": {
            "type": "string",
            "format": "uri"
          }
        },
        "required": [
          "id",
          "name",
          "email",
          "provider",
          "aliases",
          "config"
        ]
      }
    }
  },
  "paths": {
    "/channels": {
      "get": {
        "summary": "List authorised connected channel IDs",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "channels": {
                      "type": "array",
                      "items": {
                        "enum": [
                          "ebay",
                          "amazon",
                          "etsy",
                          "shopify",
                          "woocommerce",
                          "email",
                          "sms",
                          "whatsapp",
                          "facebook",
                          "instagram",
                          "slack",
                          "teams",
                          "custom"
                        ]
                      }
                    }
                  },
                  "required": [
                    "channels"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/workspace": {
      "get": {
        "summary": "Return a bounded workspace snapshot",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "description": "Stable channel-namespaced IDs. Include every referenced customer and order. Up to 1000 customers, 5000 orders, 1000 tickets and 5000 messages in 5 MB. Supply all available product details, dates, addresses, payment summaries and tracking events. One customer can have many orders; each ticket links to its queried order via orderId. Do not include full payment card data or secret credentials.",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "contacts": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Customer"
                      }
                    },
                    "conversations": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Conversation"
                      }
                    },
                    "messages": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Message"
                      }
                    },
                    "orders": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Order"
                      }
                    }
                  },
                  "required": [
                    "contacts",
                    "conversations",
                    "messages"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/messages": {
      "post": {
        "summary": "Send one reply through its original channel",
        "description": "Enforce provider authorisation, consent, recipient validation and provider messaging rules. Persist Idempotency-Key and return the original receipt on repeated keys. Return a receipt only after provider acknowledgement. Invalid receipts or timeouts are recorded as delivery unknown. Reject unsupported attachments instead of dropping them. Attachments are transferred as base64, with a combined decoded size limit of 10 MB.",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "in": "header",
            "name": "Idempotency-Key",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "conversationId": {
                    "type": "string"
                  },
                  "channel": {
                    "type": "string"
                  },
                  "subject": {
                    "type": "string"
                  },
                  "customer": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "email": {
                        "type": "string"
                      },
                      "phone": {
                        "type": "string"
                      }
                    },
                    "required": []
                  },
                  "body": {
                    "type": "string",
                    "maxLength": 15000
                  },
                  "attachments": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/Attachment"
                    }
                  },
                  "orderId": {
                    "type": [
                      "string",
                      "null"
                    ],
                    "description": "External ID of the order queried by this ticket, or null for a general enquiry."
                  },
                  "order": {
                    "type": "object",
                    "description": "The linked order details, if present."
                  },
                  "mailboxId": {
                    "type": [
                      "string",
                      "null"
                    ]
                  },
                  "fromAddress": {
                    "type": "string",
                    "description": "Verified primary or alias address of the selected mailbox. Enforce this identity on the provider; never spoof arbitrary senders."
                  }
                },
                "required": [
                  "id",
                  "conversationId",
                  "channel",
                  "subject",
                  "customer",
                  "body",
                  "attachments"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "messageId": {
                      "type": "string"
                    },
                    "status": {
                      "enum": [
                        "accepted",
                        "sent",
                        "delivered"
                      ]
                    }
                  },
                  "required": [
                    "messageId",
                    "status"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/mailboxes/connect": {
      "post": {
        "summary": "Start secure mailbox authorisation",
        "description": "Authorise Google or Microsoft via OAuth, or collect custom mail credentials in the connector vault. Preserve workspace and mailbox scope. Restrict protocol server destinations against SSRF. Do not return tokens or passwords. The return URL is allowlisted by the connector.",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Mailbox"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "authorizationUrl": {
                      "type": "string",
                      "format": "uri"
                    }
                  },
                  "required": [
                    "authorizationUrl"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/mailboxes/verify": {
      "post": {
        "summary": "Verify mailbox and approved sender identities",
        "description": "Check actual provider access and alias permissions. Only return addresses the provider authorises for this mailbox. SMTP/POP3/IMAP connections require TLS or STARTTLS. Protocol polling and OAuth token refresh run in this connector; messenger.ai does not open raw TCP connections.",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  }
                },
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "enum": [
                        "connected",
                        "not_connected",
                        "error"
                      ]
                    },
                    "verifiedSenders": {
                      "type": "array",
                      "items": {
                        "type": "string",
                        "format": "email"
                      }
                    },
                    "lastSyncAt": {
                      "type": "string",
                      "format": "date-time"
                    }
                  },
                  "required": [
                    "status",
                    "verifiedSenders"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/mailboxes/disconnect": {
      "post": {
        "summary": "Revoke provider credentials and stop mail synchronisation",
        "description": "Revoke the mailbox OAuth grant or connector credential access, then acknowledge. The app clears sender verification only after success.",
        "parameters": [
          {
            "in": "header",
            "name": "X-Messenger-Workspace",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  }
                },
                "required": [
                  "id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "ok"
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}
