{
  "openapi": "3.1.0",
  "info": {
    "title": "IntelliDesk REST API",
    "version": "1.0.0",
    "description": "Chat with your IntelliDesk agent from your own backend, read conversations and leads, and add knowledge. Guide: /docs/api. Secret keys only: the API sends no CORS headers."
  },
  "servers": [
    {
      "url": "/api/v1",
      "description": "Relative to your IntelliDesk address"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/agents": {
      "get": {
        "summary": "List the agents this key can use",
        "operationId": "listAgents",
        "x-scope": "read",
        "responses": {
          "200": {
            "description": "Agents",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Agent"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}": {
      "get": {
        "summary": "Get an agent",
        "operationId": "getAgent",
        "x-scope": "read",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The agent",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Agent"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}/chat": {
      "post": {
        "summary": "Chat with an agent",
        "operationId": "chat",
        "x-scope": "chat",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "description": "Send a user's message and get the agent's reply. With stream: true the response is text/event-stream: token events ({token}), then done (ChatResult) or error (Error).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The reply (or a stream when stream is true)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/ChatResult"
                    }
                  }
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "402": {
            "$ref": "#/components/responses/OutOfCredits"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}/conversations": {
      "get": {
        "summary": "List conversations, newest first",
        "operationId": "listConversations",
        "x-scope": "read",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 25
            }
          },
          {
            "name": "before",
            "in": "query",
            "description": "The nextBefore value from the previous page.",
            "schema": {
              "type": "number"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A page of conversations",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Conversation"
                      }
                    },
                    "nextBefore": {
                      "type": [
                        "number",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/conversations/{conversationId}": {
      "get": {
        "summary": "Get a conversation with its messages",
        "operationId": "getConversation",
        "x-scope": "read",
        "parameters": [
          {
            "name": "conversationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The conversation",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/ConversationWithMessages"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}/sources": {
      "get": {
        "summary": "List knowledge sources",
        "operationId": "listSources",
        "x-scope": "read",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Sources",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Source"
                      }
                    },
                    "totals": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      },
      "post": {
        "summary": "Add a knowledge source (retrain afterwards)",
        "operationId": "addSource",
        "x-scope": "write",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewSource"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Added",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string"
                        },
                        "retrainRequired": {
                          "type": "boolean"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}/retrain": {
      "post": {
        "summary": "Retrain the agent on its sources",
        "operationId": "retrain",
        "x-scope": "write",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "202": {
            "description": "Queued",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "object",
                      "properties": {
                        "status": {
                          "const": "queued"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/agents/{agentId}/leads": {
      "get": {
        "summary": "The newest 200 leads",
        "operationId": "listLeads",
        "x-scope": "read",
        "parameters": [
          {
            "name": "agentId",
            "in": "path",
            "required": true,
            "description": "The agent's id or slug.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Leads",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Lead"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "A secret key (idk_live_\u2026) from API keys in the dashboard."
      }
    },
    "responses": {
      "BadRequest": {
        "description": "A field is missing or wrong",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "No key, a malformed key, or a revoked one",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "OutOfCredits": {
        "description": "The workspace is out of message credits (code out_of_credits)",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Missing scope, an agent the key can't use, or owner_unverified",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "No such agent, conversation (conversation_not_found) or endpoint",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Over 120 requests a minute for this key",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer"
            },
            "description": "Seconds to wait"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "status",
              "code",
              "message"
            ],
            "properties": {
              "status": {
                "type": "integer"
              },
              "code": {
                "type": "string",
                "examples": [
                  "invalid_request",
                  "unauthorized",
                  "out_of_credits",
                  "forbidden",
                  "owner_unverified",
                  "not_found",
                  "conversation_not_found",
                  "rate_limited",
                  "internal_error"
                ]
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      },
      "Agent": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "slug": {
            "type": "string"
          },
          "model": {
            "type": "string"
          },
          "trainedStatus": {
            "enum": [
              "untrained",
              "training",
              "trained",
              "retraining_required"
            ]
          },
          "lastTrainedAt": {
            "type": [
              "number",
              "null"
            ]
          },
          "sources": {
            "type": "object"
          }
        }
      },
      "ChatRequest": {
        "type": "object",
        "required": [
          "message"
        ],
        "properties": {
          "message": {
            "type": "string",
            "maxLength": 8000
          },
          "conversationId": {
            "type": "string",
            "description": "Continue a conversation; send the same user or visitorId as when it started."
          },
          "user": {
            "type": "object",
            "required": [
              "id"
            ],
            "description": "Who is asking. Ties the chat to their contact; the key vouches for it.",
            "properties": {
              "id": {
                "type": "string",
                "maxLength": 256
              },
              "email": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "phone": {
                "type": "string"
              },
              "attributes": {
                "type": "object"
              }
            }
          },
          "visitorId": {
            "type": "string",
            "maxLength": 120,
            "description": "An anonymous id of your choosing."
          },
          "metadata": {
            "type": "object",
            "description": "Extra context for the agent."
          },
          "stream": {
            "type": "boolean",
            "default": false
          }
        }
      },
      "ChatResult": {
        "type": "object",
        "properties": {
          "conversationId": {
            "type": "string"
          },
          "messageId": {
            "type": "string"
          },
          "reply": {
            "type": "string"
          },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "title": {
                  "type": "string"
                },
                "url": {
                  "type": [
                    "string",
                    "null"
                  ]
                }
              }
            }
          },
          "handoff": {
            "enum": [
              "ai",
              "waiting",
              "operator"
            ]
          },
          "collectLead": {
            "type": "boolean"
          },
          "bookCall": {
            "type": [
              "object",
              "null"
            ]
          }
        }
      },
      "Conversation": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "agentId": {
            "type": "string"
          },
          "channel": {
            "type": "string"
          },
          "visitorId": {
            "type": [
              "string",
              "null"
            ]
          },
          "createdAt": {
            "type": "number"
          },
          "lastMessageAt": {
            "type": [
              "number",
              "null"
            ]
          },
          "status": {
            "enum": [
              "open",
              "with_human",
              "ended"
            ]
          },
          "summary": {
            "type": [
              "string",
              "null"
            ]
          },
          "sentiment": {
            "enum": [
              "positive",
              "neutral",
              "negative",
              null
            ]
          },
          "resolution": {
            "enum": [
              "resolved",
              "unresolved",
              "escalated",
              "in_progress",
              null
            ]
          },
          "rating": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1,
            "maximum": 5
          }
        }
      },
      "ConversationWithMessages": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Conversation"
          },
          {
            "type": "object",
            "properties": {
              "messages": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "role": {
                      "enum": [
                        "visitor",
                        "agent",
                        "teammate"
                      ]
                    },
                    "content": {
                      "type": "string"
                    },
                    "createdAt": {
                      "type": "number"
                    },
                    "sources": {
                      "type": "array",
                      "items": {
                        "type": "object"
                      }
                    },
                    "verified": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          }
        ]
      },
      "Source": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "url": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "type": "string"
          },
          "bytes": {
            "type": [
              "number",
              "null"
            ]
          },
          "dirty": {
            "type": "boolean"
          }
        }
      },
      "NewSource": {
        "oneOf": [
          {
            "type": "object",
            "required": [
              "type",
              "body"
            ],
            "properties": {
              "type": {
                "const": "text"
              },
              "title": {
                "type": "string"
              },
              "body": {
                "type": "string"
              }
            }
          },
          {
            "type": "object",
            "required": [
              "type",
              "answer"
            ],
            "properties": {
              "type": {
                "const": "qa"
              },
              "title": {
                "type": "string"
              },
              "question": {
                "type": "string"
              },
              "questions": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "answer": {
                "type": "string"
              }
            }
          },
          {
            "type": "object",
            "required": [
              "type",
              "url"
            ],
            "properties": {
              "type": {
                "const": "website"
              },
              "url": {
                "type": "string"
              },
              "mode": {
                "enum": [
                  "crawl",
                  "sitemap",
                  "link"
                ]
              },
              "includePaths": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "excludePaths": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          }
        ]
      },
      "Lead": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": [
              "string",
              "null"
            ]
          },
          "email": {
            "type": [
              "string",
              "null"
            ]
          },
          "phone": {
            "type": [
              "string",
              "null"
            ]
          },
          "conversationId": {
            "type": [
              "string",
              "null"
            ]
          },
          "createdAt": {
            "type": "number"
          },
          "score": {
            "enum": [
              "hot",
              "warm",
              "cold",
              null
            ]
          },
          "customFields": {
            "type": "object"
          }
        }
      }
    }
  }
}
