{
  "openapi": "3.1.0",
  "info": {
    "title": "TryDesk incoming messages API",
    "version": "1.0.0",
    "description": "Server-to-server API. Integration credentials determine workspace, source and reply channel. No projectId is accepted. Image limits: 3 files, 10485760 bytes each, 20971520 bytes total, 16384 pixels per side, 40 MP. PNG, JPEG, static WebP. Multipart body <=22020096 bytes; JSON <=65536 bytes."
  },
  "servers": [
    {
      "url": "https://api.trydesk.io"
    }
  ],
  "paths": {
    "/v1/messages": {
      "post": {
        "operationId": "sendIncomingMessage",
        "security": [
          {
            "integrationKey": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/IncomingMessage"
              },
              "example": {
                "eventId": "telegram:7487652982:123456",
                "externalConversationId": "telegram-chat-42",
                "sender": {
                  "externalId": "42",
                  "displayName": "Alex"
                },
                "message": {
                  "text": "Please help"
                }
              }
            },
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "payload",
                  "images"
                ],
                "properties": {
                  "payload": {
                    "type": "string",
                    "maxLength": 65536,
                    "description": "One TEXT field containing serialized IncomingMessage JSON, <=65536 UTF-8 bytes. Not a file."
                  },
                  "images": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 3,
                    "items": {
                      "type": "string",
                      "format": "binary"
                    },
                    "description": "Repeat the images file field for each attachment."
                  }
                }
              },
              "encoding": {
                "payload": {
                  "contentType": "text/plain"
                },
                "images": {
                  "contentType": "image/png, image/jpeg, image/webp"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Event already processed; no duplicate message created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MessageResult"
                }
              }
            }
          },
          "201": {
            "description": "Message persisted.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MessageResult"
                }
              }
            }
          },
          "400": {
            "description": "Invalid payload, multipart or image.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "401": {
            "description": "Invalid integration key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "403": {
            "description": "Inactive integration or workspace.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "409": {
            "description": "Same event with different content or incompatible conversation/sender.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "413": {
            "description": "Exceeded image count, byte or pixel limit.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "415": {
            "description": "Unsupported content type or image format.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; retry unchanged event after Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer",
                  "minimum": 0
                },
                "description": "Delay in seconds before retrying."
              }
            }
          },
          "503": {
            "description": "Temporary failure; retry unchanged event after Retry-After.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer",
                  "minimum": 0
                },
                "description": "Delay in seconds before retrying."
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "integrationKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "tdk_<keyId>.<secret>",
        "description": "Server-side integration key. Never expose in browser code."
      }
    },
    "schemas": {
      "Sender": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "externalId"
        ],
        "properties": {
          "externalId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "displayName": {
            "type": "string",
            "minLength": 1,
            "maxLength": 120
          },
          "kind": {
            "type": "string",
            "enum": [
              "customer",
              "service"
            ],
            "default": "customer"
          },
          "email": {
            "type": "string",
            "format": "email",
            "maxLength": 254
          }
        }
      },
      "IncomingMessage": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "eventId",
          "externalConversationId",
          "sender",
          "message"
        ],
        "properties": {
          "eventId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "description": "Stable source event ID. Repeat unchanged for retries."
          },
          "externalConversationId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "sender": {
            "$ref": "#/components/schemas/Sender"
          },
          "message": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "text": {
                "type": "string",
                "maxLength": 10000
              }
            },
            "description": "Nonblank text is required unless multipart images are provided."
          }
        }
      },
      "MessageResult": {
        "type": "object",
        "required": [
          "conversationId",
          "messageId",
          "duplicate"
        ],
        "properties": {
          "conversationId": {
            "type": "string"
          },
          "messageId": {
            "type": "string"
          },
          "duplicate": {
            "type": "boolean"
          }
        }
      },
      "ApiError": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}
