{
  "openapi": "3.1.0",
  "info": {
    "title": "ReX API",
    "version": "1",
    "description": "Recommendations for IPTV and OTT apps. Apps send viewing events and read personal rows for each household. Guide: https://rexai.tv/docs"
  },
  "servers": [
    {
      "url": "https://api.rexai.tv",
      "description": "Production"
    },
    {
      "url": "https://rex-api-470663927456.northamerica-northeast1.run.app",
      "description": "Production (Cloud Run address, same service)"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "paths": {
    "/v1/events": {
      "post": {
        "summary": "Send viewing events",
        "description": "Up to 500 events per request. Each event is checked on its own: valid ones are stored even when others are refused. Events of a type this ReX does not know yet are kept. A request sent with `keepalive` while the app closes may use `content-type: text/plain` and the key as `?key=` instead of the header.",
        "operationId": "sendEvents",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EventBatch"
              }
            },
            "text/plain": {
              "schema": {
                "$ref": "#/components/schemas/EventBatch"
              }
            }
          }
        },
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "The API key, only where a header is impossible (keepalive while the page closes)."
          }
        ],
        "responses": {
          "202": {
            "description": "Received.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EventsAnswer"
                }
              }
            }
          },
          "400": {
            "description": "The body is not a batch at all (not JSON, no `events` array, or more than 500).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or unknown API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/v1/rows": {
      "get": {
        "summary": "A household's rows",
        "description": "Personal rows for one household, best first, ended live broadcasts already removed. Rows change overnight and, for what is on now, every 15 minutes; answers may be cached for 60 s.",
        "operationId": "getRows",
        "parameters": [
          {
            "name": "household",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "maxLength": 256
            },
            "description": "The same household id the app sends in events."
          },
          {
            "name": "packages",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 2000
            },
            "description": "Comma-separated packages the household has; items from other packages are left out. Without it, nothing is filtered."
          }
        ],
        "responses": {
          "200": {
            "description": "The rows (an unknown household gets the operator's general rows).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RowsAnswer"
                }
              }
            }
          },
          "400": {
            "description": "`household` missing or too long.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or unknown API key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "summary": "Health check",
        "security": [],
        "operationId": "health",
        "responses": {
          "200": {
            "description": "ReX is up.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-rex-key",
        "description": "The operator's API key."
      }
    },
    "schemas": {
      "ContentRef": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "live",
              "catchup",
              "startover",
              "recording",
              "vod"
            ],
            "description": "How it is watched."
          },
          "channelId": {
            "description": "The operator's channel id (live, catch-up, start-over, recordings).",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "programmeId": {
            "description": "Programme being watched, when the app knows it; otherwise ReX finds it from channel and time.",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "airingId": {
            "description": "The guide entry's id, as the operator's guide gives it.",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "seriesId": {
            "description": "The series, when known.",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "titleId": {
            "description": "VOD title id (`kind: \"vod\"`).",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          }
        },
        "required": [
          "kind"
        ]
      },
      "ViewingEvent": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "minLength": 8,
            "maxLength": 64,
            "description": "Unique per event (a UUID), so a resent batch is not counted twice."
          },
          "type": {
            "type": "string",
            "enum": [
              "play_start",
              "progress",
              "play_stop",
              "zap",
              "record",
              "remind",
              "search",
              "details",
              "impression",
              "select"
            ],
            "description": "What happened (see the table of event types)."
          },
          "at": {
            "type": "string",
            "format": "date-time",
            "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$",
            "description": "When it happened (ISO 8601 with offset or Z)."
          },
          "householdId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "description": "The operator's account or household id. ReX stores only a pseudonym of it."
          },
          "profileId": {
            "description": "Profile within the household, when the app has profiles.",
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          },
          "deviceId": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "description": "The device (stored as a pseudonym)."
          },
          "platform": {
            "type": "string",
            "minLength": 1,
            "maxLength": 32,
            "description": "What the app runs on: \"web\", \"android\", \"ios\", \"lg\", \"tizen\", \"roku\"…"
          },
          "appVersion": {
            "description": "The app's version.",
            "type": "string",
            "maxLength": 32
          },
          "content": {
            "description": "What was watched, picked or looked at.",
            "$ref": "#/components/schemas/ContentRef"
          },
          "watchedSeconds": {
            "description": "Seconds actually watched since the previous event of this playback.",
            "type": "integer",
            "minimum": 0,
            "maximum": 3600
          },
          "positionSeconds": {
            "description": "Playback position.",
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          },
          "durationSeconds": {
            "description": "Length of the content.",
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          },
          "query": {
            "description": "`search`: the search text.",
            "type": "string",
            "maxLength": 200
          },
          "dwellSeconds": {
            "description": "`details`: seconds the details stayed open.",
            "type": "integer",
            "minimum": 0,
            "maximum": 36000
          },
          "items": {
            "description": "`impression`: the titles shown.",
            "maxItems": 200,
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ContentRef"
            }
          },
          "surface": {
            "description": "`impression`: where they were shown (\"home\", \"movies\").",
            "type": "string",
            "maxLength": 32
          },
          "source": {
            "description": "Where a playback or pick came from: \"rex:<row id>\" for a ReX row.",
            "type": "string",
            "maxLength": 64
          },
          "sourcePosition": {
            "description": "Position of the picked card in its row (1 = first).",
            "type": "integer",
            "minimum": 1,
            "maximum": 500
          }
        },
        "required": [
          "id",
          "type",
          "at",
          "householdId",
          "deviceId",
          "platform"
        ]
      },
      "RowItem": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "live",
              "catchup",
              "recording",
              "vod"
            ],
            "description": "What playing it means: a live channel, catch-up, a recording or a VOD title."
          },
          "channelId": {
            "description": "The operator's channel id (live, catch-up, recordings).",
            "type": "string"
          },
          "programmeId": {
            "description": "The operator's programme id, when the guide has one.",
            "type": "string"
          },
          "airingId": {
            "description": "The guide entry, as the app sent it in events. Match it together with `startsAt`: repeats can share it.",
            "type": "string"
          },
          "seriesId": {
            "description": "The series, when known.",
            "type": "string"
          },
          "titleId": {
            "description": "The operator's VOD title id.",
            "type": "string"
          },
          "startsAt": {
            "description": "Start of the broadcast (ISO 8601).",
            "type": "string"
          },
          "endsAt": {
            "description": "End of the broadcast (ISO 8601).",
            "type": "string"
          },
          "packages": {
            "description": "Packages that include it.",
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "becauseOf": {
            "description": "\"Because you watched\": the show or title that led to this one.",
            "type": "string"
          },
          "title": {
            "description": "Programme or title name, to draw the card.",
            "type": "string"
          },
          "subtitle": {
            "description": "Episode title.",
            "type": "string"
          },
          "image": {
            "description": "Poster (https).",
            "type": "string"
          },
          "channelName": {
            "description": "Channel name.",
            "type": "string"
          },
          "channelLogo": {
            "description": "Channel logo (https).",
            "type": "string"
          },
          "becauseOfTitle": {
            "description": "Name of `becauseOf`, for a heading such as \"Because you watched …\".",
            "type": "string"
          }
        },
        "required": [
          "kind"
        ]
      },
      "Row": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "enum": [
              "continue_watching",
              "new_episodes",
              "popular_now",
              "because_you_watched",
              "popular",
              "trending"
            ],
            "description": "The row's kind. The app chooses the heading (in its own language) from it."
          },
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/RowItem"
            },
            "description": "In order: the first item is the best pick."
          }
        },
        "required": [
          "id",
          "items"
        ]
      },
      "EventBatch": {
        "type": "object",
        "required": [
          "events"
        ],
        "properties": {
          "events": {
            "type": "array",
            "minItems": 1,
            "maxItems": 500,
            "items": {
              "$ref": "#/components/schemas/ViewingEvent"
            }
          }
        }
      },
      "EventsAnswer": {
        "type": "object",
        "required": [
          "accepted",
          "rejected"
        ],
        "properties": {
          "accepted": {
            "type": "integer",
            "description": "Events stored."
          },
          "rejected": {
            "type": "array",
            "description": "Events refused, each with the reason. The others are stored all the same.",
            "items": {
              "type": "object",
              "required": [
                "index",
                "error"
              ],
              "properties": {
                "index": {
                  "type": "integer",
                  "description": "Position in `events` (0 = first)."
                },
                "id": {
                  "type": "string",
                  "description": "The event's id, when it had one."
                },
                "error": {
                  "type": "string",
                  "description": "What was wrong, e.g. \"at: Invalid ISO datetime\"."
                }
              }
            }
          }
        }
      },
      "RowsAnswer": {
        "type": "object",
        "required": [
          "rows"
        ],
        "properties": {
          "rows": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Row"
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "enum": [
              "unauthorized",
              "invalid_batch",
              "invalid_query"
            ]
          }
        }
      }
    }
  }
}
