{
  "openapi": "3.0.3",
  "info": {
    "title": "Biky Analytics API",
    "version": "1.0.0",
    "description": "Read the metrics behind the Biky analytics dashboard for your app: sales funnel, lead sources and paid ads, products, advisor performance, chat sessions, CSAT and what customers talk about with Biky."
  },
  "servers": [
    {
      "url": "https://analytics.keybeapi.com",
      "description": "Production"
    }
  ],
  "tags": [
    {
      "name": "Metrics",
      "description": "Dashboard metrics of one app for a date range."
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/metrics/{appUUID}": {
      "get": {
        "tags": [
          "Metrics"
        ],
        "operationId": "getAppMetrics",
        "summary": "Get dashboard metrics of an app",
        "description": "Returns the requested metrics of your app for a date range in one call, with the same numbers as the Biky analytics dashboard. Request only the metrics you need: the call times out after about 29 seconds (504), so split heavy combinations. Each metric is cached for up to 1 hour (`fromCache`) and identical requests may be served from a 3-minute gateway cache. Filters that do not apply to a metric are ignored for it.",
        "parameters": [
          {
            "name": "appUUID",
            "in": "path",
            "required": true,
            "description": "Your app UUID. The credential must belong to this app.",
            "schema": {
              "type": "string"
            },
            "example": "app_7f3k2m9x1q"
          },
          {
            "name": "metrics",
            "in": "query",
            "required": false,
            "description": "Comma-separated list of metrics to return. Default: `leads,sales,mql`. Each one comes back as a key of `data`.\n\n\n**Funnel**\n\n- `leads`: New leads (sales cards) created per day, with running total and growth vs. the previous period of the same length.\n- `sales`: Leads in a won step (sale, delivery, billed...) per day, with amounts when available.\n- `mql`: Leads in a marketing-qualified (MQL) step, by day of their last step change.\n- `mqlNotResponded`: Leads whose outreach was delivered but who have not responded yet, per day.\n- `sql`: Leads in a sales-qualified (SQL) step, per day.\n- `scheduling`: Leads that booked an appointment, per day.\n- `show`: Leads that attended their appointment, per day.\n- `negotiating`: Leads in negotiation or quote steps, per day.\n- `lost`: Leads marked as lost, per day.\n- `funnelSteps`: Cumulative leads per funnel step with the conversion percentage from the previous step.\n- `conversionRates`: Conversion rate (sales / SQL), scheduling rate and show rate, each with growth.\n- `funnelByAgent`: Leads per funnel step and conversion rate for each advisor, ranked.\n- `salesBySpace`: Sales count and amount per space (branch or team) per day.\n\n**Sources and ads**\n\n- `sources`: Leads per source (`utm_source`) with their share, plus breakdowns by owner, channel and bill-to.\n- `sourcesCampaign`: Leads per paid campaign, ad set and ad, with spend, cost per lead, ROAS, reach and other Meta/TikTok ad data.\n- `sourcesKeybe`: Leads per source as attributed by Biky (`utm_source_keybe`).\n- `adsMetrics`: Meta ads per ad: spend, leads reported by Meta vs. leads in Biky, cost per lead and resulting sales.\n\n**Products**\n\n- `topProducts`: Most sold products.\n- `interestedProducts`: Products leads asked about, with their share.\n\n**Advisors**\n\n- `advisorPerformance`: Per advisor and space: interactions, responded vs. unanswered chats, leads created, average response time and rating.\n- `advisorGlobalPerformance`: Advisor ranking by a global score, with a team summary and the top performer.\n- `advisorActiveTime`: Advisor availability: active and working minutes per day, with the time ranges.\n\n**Leads**\n\n- `leadsList`: Lead-level list for the period: step, advisor, space, UTM and ad attribution, amount, product and appointment (not paginated).\n- `leadsHeatmapByHour`: Leads created per day of the month and hour of the day (local time).\n- `mqlDropReasons`: Why leads did not move from MQL to SQL (no conversation, no client response, send failed...), as a distribution.\n- `mqlDropReasonsList`: Lead-level list of MQL drop reasons with message counts (paginated with `page`/`perPage`).\n\n**Conversations**\n\n- `sessionsByAdvisor`: Chat sessions attended, closed and open per advisor, average duration and origin (inbound or proactive).\n- `closeReasonDistribution`: Closed chat sessions grouped by close reason.\n- `openSessionsByChannel`: Open chat sessions per channel.\n- `closedSessionsList`: Closed chat sessions with customer, advisors, channel, space, duration and close reason (paginated with `page`/`perPage`).\n- `csatByAdvisor`: CSAT survey answers per advisor: total plus one column per answer value.\n- `csatByDay`: CSAT survey answers per day: total plus one column per answer value.\n- `csatDistribution`: Share of each CSAT answer value.\n\n**AI mentions (what customers talk about with Biky; pick the kind with `aiMentionType`)**\n\n- `aiMentionsTop5`: Top 5 mentioned items with mentions, unique users and share.\n- `aiMentionsTrend`: Mentions per item per day, with running total.\n- `aiMentionsUserUniques`: Mentioned items with their mentions and unique users.\n- `aiMentionsPorcentDistribution`: Share of mentions per item.\n- `aiMentionsListResumenTotal`: Every mentioned item with totals and shares (paginated with `page`, 25 per page).\n- `aiMentionsNube`: Word-cloud weights of the mentioned terms.\n\n**Other**\n\n- `filterOptions`: Values available in your app for the `source` and `funnel` filters.",
            "schema": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "leads",
                  "sales",
                  "mql",
                  "mqlNotResponded",
                  "sql",
                  "scheduling",
                  "show",
                  "negotiating",
                  "lost",
                  "funnelSteps",
                  "conversionRates",
                  "funnelByAgent",
                  "salesBySpace",
                  "sources",
                  "sourcesCampaign",
                  "sourcesKeybe",
                  "adsMetrics",
                  "topProducts",
                  "interestedProducts",
                  "advisorPerformance",
                  "advisorGlobalPerformance",
                  "advisorActiveTime",
                  "leadsList",
                  "leadsHeatmapByHour",
                  "mqlDropReasons",
                  "mqlDropReasonsList",
                  "sessionsByAdvisor",
                  "closeReasonDistribution",
                  "openSessionsByChannel",
                  "closedSessionsList",
                  "csatByAdvisor",
                  "csatByDay",
                  "csatDistribution",
                  "aiMentionsTop5",
                  "aiMentionsTrend",
                  "aiMentionsUserUniques",
                  "aiMentionsPorcentDistribution",
                  "aiMentionsListResumenTotal",
                  "aiMentionsNube",
                  "filterOptions"
                ]
              }
            },
            "example": [
              "leads",
              "sales",
              "csatByAdvisor"
            ],
            "style": "form",
            "explode": false
          },
          {
            "name": "startDate",
            "in": "query",
            "required": false,
            "description": "First day of the period, `YYYY-MM-DD`, in your app's time zone. Send it together with `endDate`; without both, the period is the current month to date.",
            "schema": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "example": "2026-10-01"
          },
          {
            "name": "endDate",
            "in": "query",
            "required": false,
            "description": "Last day of the period (inclusive), `YYYY-MM-DD`. Must not be before `startDate`.",
            "schema": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "example": "2026-10-31"
          },
          {
            "name": "advisorIds",
            "in": "query",
            "required": false,
            "description": "Only data of these advisors (comma-separated user UUIDs). `unassigned` matches leads without an advisor. Each id may only contain letters, digits, `-` and `_`; anything else answers `400`.",
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9_-]{1,64}(,[A-Za-z0-9_-]{1,64})*$"
            },
            "example": "usr_2b7c9d4f,unassigned"
          },
          {
            "name": "agentType",
            "in": "query",
            "required": false,
            "description": "Only advisors of this type, as set in your team: `digital` or `floor` (in-store). Advisors without a type count as `digital`. Combined with `advisorIds`.",
            "schema": {
              "type": "string",
              "enum": [
                "digital",
                "floor"
              ]
            },
            "example": "digital"
          },
          {
            "name": "space",
            "in": "query",
            "required": false,
            "description": "Only these spaces (comma-separated space labels).",
            "schema": {
              "type": "string"
            },
            "example": "Bogotá Norte,Medellín"
          },
          {
            "name": "whatsappLine",
            "in": "query",
            "required": false,
            "description": "Only leads that came through these WhatsApp lines (comma-separated phone numbers).",
            "schema": {
              "type": "string"
            },
            "example": "+573001112233"
          },
          {
            "name": "funnel",
            "in": "query",
            "required": false,
            "description": "Funnel to compute the metrics on. Default `$funnel_sales$`; `filterOptions` lists the ones your app has.",
            "schema": {
              "type": "string",
              "enum": [
                "$funnel_sales$",
                "$funnel_sales_fleet$"
              ]
            },
            "example": "$funnel_sales$"
          },
          {
            "name": "stepName",
            "in": "query",
            "required": false,
            "description": "Only leads currently in these funnel steps (comma-separated step keys, e.g. `$step_sql$`). Applies to funnel, source and lead-list metrics.",
            "schema": {
              "type": "string"
            },
            "example": "$step_scheduling$"
          },
          {
            "name": "source",
            "in": "query",
            "required": false,
            "description": "Only leads from these sources (`utm_source`, comma-separated, case-insensitive). `filterOptions` lists the values.",
            "schema": {
              "type": "string"
            },
            "example": "facebook,google"
          },
          {
            "name": "channel",
            "in": "query",
            "required": false,
            "description": "Only leads from these channels (comma-separated, case-insensitive): `Paid Search`, `Paid Social`, `Organic Social`, `Organic / Direct`, `Email / SMS`, `Messaging`, `Referral`, `Offline`, `CRM Sync`, `Unknown`. Leads without a channel count as `Unknown`.",
            "schema": {
              "type": "string"
            },
            "example": "Paid Social,Messaging"
          },
          {
            "name": "owner",
            "in": "query",
            "required": false,
            "description": "Only leads owned by `oem` (the brand) or `dealer` (comma-separated). Leads without an owner count as `dealer`.",
            "schema": {
              "type": "string"
            },
            "example": "dealer"
          },
          {
            "name": "billTo",
            "in": "query",
            "required": false,
            "description": "Only leads billed to `oem` or `dealer` (comma-separated). Leads without a value count as `dealer`.",
            "schema": {
              "type": "string"
            },
            "example": "oem"
          },
          {
            "name": "campaign",
            "in": "query",
            "required": false,
            "description": "Only leads from these ad campaigns (`utm_campaign`, comma-separated).",
            "schema": {
              "type": "string"
            },
            "example": "promo_noviembre"
          },
          {
            "name": "adSet",
            "in": "query",
            "required": false,
            "description": "Only leads from these ad sets (`utm_term`, comma-separated).",
            "schema": {
              "type": "string"
            },
            "example": "retargeting_30d"
          },
          {
            "name": "ad",
            "in": "query",
            "required": false,
            "description": "Only leads from these ads (`utm_content`, comma-separated).",
            "schema": {
              "type": "string"
            },
            "example": "video_suv_15s"
          },
          {
            "name": "aiMentionType",
            "in": "query",
            "required": false,
            "description": "Kind of AI mention for the `aiMentions*` metrics.",
            "schema": {
              "type": "string",
              "enum": [
                "products_interests",
                "intentions",
                "keyconcepts",
                "keywords"
              ],
              "default": "products_interests"
            },
            "example": "intentions"
          },
          {
            "name": "scheduleDate",
            "in": "query",
            "required": false,
            "description": "`leadsList` only: leads whose appointment falls on this local date (`YYYY-MM-DD`). Any other format answers `400`.",
            "schema": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "example": "2026-10-15"
          },
          {
            "name": "page",
            "in": "query",
            "required": false,
            "description": "Page for the paginated metrics (`closedSessionsList`, `mqlDropReasonsList`, `aiMentionsListResumenTotal`), starting at 1.",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "default": 1
            },
            "example": 1
          },
          {
            "name": "perPage",
            "in": "query",
            "required": false,
            "description": "Items per page for `closedSessionsList` and `mqlDropReasonsList`.",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "default": 50
            },
            "example": 50
          }
        ],
        "responses": {
          "200": {
            "description": "Requested metrics, one key per metric",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MetricsResponse"
                },
                "example": {
                  "success": true,
                  "message": "Analytics metrics retrieved successfully",
                  "data": {
                    "leads": {
                      "data": [
                        {
                          "date": "2026-10-01",
                          "leads": 34,
                          "accumulated_leads": 34
                        },
                        {
                          "date": "2026-10-02",
                          "leads": 51,
                          "accumulated_leads": 85
                        }
                      ],
                      "total": 85,
                      "range": {
                        "start": "2026-10-01",
                        "end": "2026-10-02"
                      },
                      "growth": {
                        "absolute_increase": 12,
                        "percentage_increase": 16.4,
                        "previous_period_total": 73,
                        "previous_period_range": {
                          "start": "2026-09-29",
                          "end": "2026-09-30"
                        }
                      },
                      "fromCache": true
                    },
                    "csatByAdvisor": {
                      "data": [
                        {
                          "advisor_uuid": "usr_2b7c9d4f",
                          "advisor_name": "Laura Gómez",
                          "total": 18,
                          "excelente": 12,
                          "bueno": 4,
                          "regular": 2
                        },
                        {
                          "advisor_uuid": "usr_8e1a3c5d",
                          "advisor_name": "Carlos Pérez",
                          "total": 9,
                          "excelente": 7,
                          "bueno": 2,
                          "regular": 0
                        }
                      ],
                      "total": 27,
                      "range": {
                        "start": "2026-10-01",
                        "end": "2026-10-02"
                      },
                      "fromCache": false
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "504": {
            "description": "The metrics took too long; request fewer metrics or a shorter range",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "success": false,
                  "message": "The request exceeded the maximum execution time of 27 seconds"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Your app's API key (Settings → API keys) or an Identity access token."
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid metric name, date or filter value. `error` lists the valid values.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "success": false,
              "error": "Both startDate and endDate parameters are required together"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing or invalid credential, or it does not belong to `appUUID`.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "message": "Unauthorized"
            }
          }
        }
      },
      "Forbidden": {
        "description": "The user behind the credential is restricted to some spaces (coordinator or agent role) and asked for another one. `allowedSpaces` lists theirs.",
        "content": {
          "application/json": {
            "schema": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Error"
                },
                {
                  "type": "object",
                  "properties": {
                    "allowedSpaces": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              ]
            },
            "example": {
              "success": false,
              "message": "Forbidden: No access to specified space",
              "allowedSpaces": [
                "Bogotá Norte"
              ]
            }
          }
        }
      }
    },
    "schemas": {
      "DateRange": {
        "type": "object",
        "properties": {
          "start": {
            "type": "string",
            "nullable": true,
            "example": "2026-10-01"
          },
          "end": {
            "type": "string",
            "nullable": true,
            "example": "2026-10-31"
          }
        }
      },
      "Growth": {
        "type": "object",
        "description": "Change vs. the previous period of the same length.",
        "properties": {
          "absolute_increase": {
            "type": "number"
          },
          "percentage_increase": {
            "type": "number"
          },
          "previous_period_total": {
            "type": "number"
          },
          "previous_period_range": {
            "$ref": "#/components/schemas/DateRange"
          }
        }
      },
      "MetricResult": {
        "type": "object",
        "description": "Result of one metric. Most metrics share this envelope: `data` (rows; their fields depend on the metric), `total`, `range` and, for funnel metrics, `growth`. Some add their own aggregates (e.g. `totals`, `summary`, `pagination`). If one metric fails, its key carries `error: true` and the others are still returned.",
        "additionalProperties": true,
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": true
            }
          },
          "total": {
            "type": "number"
          },
          "range": {
            "$ref": "#/components/schemas/DateRange"
          },
          "growth": {
            "$ref": "#/components/schemas/Growth"
          },
          "pagination": {
            "$ref": "#/components/schemas/Pagination"
          },
          "fromCache": {
            "type": "boolean",
            "description": "`true` when served from the metrics cache (up to 1 hour old)."
          },
          "error": {
            "type": "boolean",
            "description": "`true` if this metric could not be computed."
          },
          "errorMessage": {
            "type": "string"
          }
        }
      },
      "Pagination": {
        "type": "object",
        "description": "Pagination of list metrics (1-based).",
        "properties": {
          "current_page": {
            "type": "integer"
          },
          "per_page": {
            "type": "integer"
          },
          "total_pages": {
            "type": "integer"
          },
          "total_items": {
            "type": "integer"
          },
          "has_next_page": {
            "type": "boolean"
          },
          "has_previous_page": {
            "type": "boolean"
          }
        }
      },
      "MetricsResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean"
          },
          "message": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "description": "One key per requested metric.",
            "additionalProperties": {
              "$ref": "#/components/schemas/MetricResult"
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean",
            "example": false
          },
          "error": {
            "type": "string",
            "description": "Validation error."
          },
          "message": {
            "type": "string"
          }
        }
      }
    }
  }
}
