{
 "openapi": "3.1.0",
 "info": {
  "title": "Aniwatch API",
  "version": "1.0.0",
  "description": "Public catalogue API plus OAuth-protected user endpoints for Aniwatch. Base URL is your Aniwatch domain + /api/v1. Anonymous: 60 req/min per IP. API key (X-API-Key): per-key limit (default 600/min). User tokens: 300 req/min per connection."
 },
 "servers": [
  {
   "url": "/api/v1",
   "description": "This Aniwatch site"
  }
 ],
 "security": [
  {},
  {
   "apiKey": []
  }
 ],
 "tags": [
  {
   "name": "Catalogue"
  },
  {
   "name": "Account (OAuth)"
  }
 ],
 "paths": {
  "/": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "API info and your current rate limit",
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object"
        }
       }
      }
     }
    }
   }
  },
  "/anime": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Browse / filter the catalogue",
    "parameters": [
     {
      "name": "q",
      "in": "query",
      "required": false,
      "description": "Text search across English, romaji and native titles",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "genre",
      "in": "query",
      "required": false,
      "description": "Comma-separated genres, all must match",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "type",
      "in": "query",
      "required": false,
      "description": "TV, Movie, ONA, OVA, Special",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "year",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "season",
      "in": "query",
      "required": false,
      "description": "Winter, Spring, Summer, Fall",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "status",
      "in": "query",
      "required": false,
      "description": "Substring match, e.g. 'currently' or 'finished'",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "lang",
      "in": "query",
      "required": false,
      "description": "Only titles with this audio",
      "schema": {
       "type": "string",
       "enum": [
        "sub",
        "dub"
       ]
      }
     },
     {
      "name": "sort",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "string",
       "enum": [
        "updated",
        "score",
        "year",
        "title"
       ]
      }
     },
     {
      "name": "page",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "integer",
       "minimum": 1
      }
     },
     {
      "name": "per_page",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "integer",
       "minimum": 1,
       "maximum": 100
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/AnimeCard"
           }
          },
          "pagination": {
           "$ref": "#/components/schemas/Pagination"
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/search": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Quick title search",
    "parameters": [
     {
      "name": "q",
      "in": "query",
      "required": true,
      "description": "",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "limit",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "integer",
       "maximum": 25
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/AnimeCard"
           }
          },
          "total": {
           "type": "integer"
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/lookup": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Find the Aniwatch title for a show from another database (MyAnimeList id is exact; title is best-effort)",
    "parameters": [
     {
      "name": "mal_id",
      "in": "query",
      "required": false,
      "description": "MyAnimeList id (AniList exposes it as idMal)",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "title",
      "in": "query",
      "required": false,
      "description": "Fallback when no MAL id",
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "year",
      "in": "query",
      "required": false,
      "description": "Helps title matching",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "episode",
      "in": "query",
      "required": false,
      "description": "Builds watch_url for this episode",
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "match": {
           "type": "string",
           "enum": [
            "mal_id",
            "title_exact",
            "title_fuzzy"
           ]
          },
          "data": {
           "$ref": "#/components/schemas/Anime"
          }
         }
        }
       }
      }
     },
     "404": {
      "$ref": "#/components/responses/NotFound"
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/genres": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "All genres with title counts",
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "array",
           "items": {
            "type": "object",
            "properties": {
             "name": {
              "type": "string"
             },
             "count": {
              "type": "integer"
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }
  },
  "/home": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Home sections: recent, airing, top",
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "recent": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/AnimeCard"
           }
          },
          "airing": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/AnimeCard"
           }
          },
          "top": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/AnimeCard"
           }
          }
         }
        }
       }
      }
     }
    }
   }
  },
  "/anime/{id}": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Full details with episode list",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "allOf": [
            {
             "$ref": "#/components/schemas/Anime"
            },
            {
             "type": "object",
             "properties": {
              "likes": {
               "type": "integer"
              },
              "episodes": {
               "type": "array",
               "items": {
                "$ref": "#/components/schemas/Episode"
               }
              }
             }
            }
           ]
          }
         }
        }
       }
      }
     },
     "404": {
      "$ref": "#/components/responses/NotFound"
     },
     "503": {
      "description": "Episode source busy, retry shortly"
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/anime/{id}/episodes": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "Episode list",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/Episode"
           }
          }
         }
        }
       }
      }
     },
     "404": {
      "$ref": "#/components/responses/NotFound"
     }
    }
   }
  },
  "/anime/{id}/episodes/{number}": {
   "get": {
    "tags": [
     "Catalogue"
    ],
    "summary": "One episode",
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "number",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "number"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "$ref": "#/components/schemas/Episode"
          }
         }
        }
       }
      }
     },
     "404": {
      "$ref": "#/components/responses/NotFound"
     }
    }
   }
  },
  "/me": {
   "get": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "The signed-in user",
    "security": [
     {
      "oauth": [
       "profile"
      ]
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "object",
           "properties": {
            "id": {
             "type": "string"
            },
            "name": {
             "type": "string"
            },
            "created_at": {
             "type": "string",
             "format": "date-time"
            }
           }
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     },
     "403": {
      "$ref": "#/components/responses/Forbidden"
     }
    }
   }
  },
  "/me/library": {
   "get": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Saved list, favorites and likes",
    "security": [
     {
      "oauth": [
       "library:read"
      ]
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "object",
           "properties": {
            "saved": {
             "type": "array",
             "items": {
              "$ref": "#/components/schemas/LibraryItem"
             }
            },
            "favorites": {
             "type": "array",
             "items": {
              "$ref": "#/components/schemas/LibraryItem"
             }
            },
            "liked": {
             "type": "array",
             "items": {
              "$ref": "#/components/schemas/LibraryItem"
             }
            }
           }
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/me/library/{id}": {
   "put": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Add a title to saved or favorites",
    "security": [
     {
      "oauth": [
       "library:write"
      ]
     }
    ],
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     }
    ],
    "requestBody": {
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "list": {
          "type": "string",
          "enum": [
           "saved",
           "favorites"
          ],
          "default": "saved"
         }
        }
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "object",
           "properties": {
            "id": {
             "type": "integer"
            },
            "list": {
             "type": "string"
            },
            "in_list": {
             "type": "boolean"
            }
           }
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   },
   "delete": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Remove a title from saved or favorites",
    "security": [
     {
      "oauth": [
       "library:write"
      ]
     }
    ],
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "list",
      "in": "query",
      "required": false,
      "description": "",
      "schema": {
       "type": "string",
       "enum": [
        "saved",
        "favorites"
       ]
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object"
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/me/history": {
   "get": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Watch history (most recent first, up to 60 titles)",
    "security": [
     {
      "oauth": [
       "history:read"
      ]
     }
    ],
    "parameters": [
     {
      "name": "since",
      "in": "query",
      "required": false,
      "description": "Only entries updated after this time",
      "schema": {
       "type": "string",
       "format": "date-time"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "type": "array",
           "items": {
            "$ref": "#/components/schemas/HistoryItem"
           }
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   },
   "post": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Mark episodes as watched",
    "security": [
     {
      "oauth": [
       "history:write"
      ]
     }
    ],
    "requestBody": {
     "required": true,
     "content": {
      "application/json": {
       "schema": {
        "type": "object",
        "properties": {
         "anime_id": {
          "type": "integer"
         },
         "mal_id": {
          "type": "integer"
         },
         "episode": {
          "type": "number"
         },
         "episodes": {
          "type": "array",
          "items": {
           "type": "number"
          }
         },
         "language": {
          "type": "string",
          "enum": [
           "sub",
           "dub",
           "hsub"
          ]
         }
        },
        "description": "Give anime_id or mal_id, and episode or episodes."
       }
      }
     }
    },
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "$ref": "#/components/schemas/HistoryItem"
          }
         }
        }
       }
      }
     },
     "401": {
      "$ref": "#/components/responses/Unauthorized"
     },
     "429": {
      "$ref": "#/components/responses/RateLimited"
     }
    }
   }
  },
  "/me/history/{id}": {
   "get": {
    "tags": [
     "Account (OAuth)"
    ],
    "summary": "Progress for one title",
    "security": [
     {
      "oauth": [
       "history:read"
      ]
     }
    ],
    "parameters": [
     {
      "name": "id",
      "in": "path",
      "required": true,
      "description": "",
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "content": {
       "application/json": {
        "schema": {
         "type": "object",
         "properties": {
          "data": {
           "$ref": "#/components/schemas/HistoryItem"
          }
         }
        }
       }
      }
     },
     "404": {
      "$ref": "#/components/responses/NotFound"
     }
    }
   }
  }
 },
 "components": {
  "securitySchemes": {
   "apiKey": {
    "type": "apiKey",
    "in": "header",
    "name": "X-API-Key",
    "description": "Server-side keys created in Admin \u2192 Developers. Raises the rate limit."
   },
   "oauth": {
    "type": "oauth2",
    "description": "Authorization code flow. PKCE (S256) required for public (mobile/browser) apps. Access tokens last 1 hour; refresh tokens 60 days and rotate on every use.",
    "flows": {
     "authorizationCode": {
      "authorizationUrl": "/oauth/authorize",
      "tokenUrl": "/oauth/token",
      "refreshUrl": "/oauth/token",
      "scopes": {
       "profile": "See your display name",
       "library:read": "See your saved list, favorites and likes",
       "library:write": "Add and remove titles in your saved list and favorites",
       "history:read": "See your watch history and episode progress",
       "history:write": "Mark episodes as watched"
      }
     }
    }
   }
  },
  "responses": {
   "Unauthorized": {
    "description": "Missing/invalid key or token",
    "content": {
     "application/json": {
      "schema": {
       "$ref": "#/components/schemas/Error"
      }
     }
    }
   },
   "Forbidden": {
    "description": "Token lacks the needed scope",
    "content": {
     "application/json": {
      "schema": {
       "$ref": "#/components/schemas/Error"
      }
     }
    }
   },
   "NotFound": {
    "description": "Not found",
    "content": {
     "application/json": {
      "schema": {
       "$ref": "#/components/schemas/Error"
      }
     }
    }
   },
   "RateLimited": {
    "description": "Too many requests. See X-RateLimit-* headers.",
    "content": {
     "application/json": {
      "schema": {
       "$ref": "#/components/schemas/Error"
      }
     }
    }
   }
  },
  "schemas": {
   "Error": {
    "type": "object",
    "properties": {
     "error": {
      "type": "object",
      "properties": {
       "code": {
        "type": "string"
       },
       "message": {
        "type": "string"
       }
      }
     }
    }
   },
   "Pagination": {
    "type": "object",
    "properties": {
     "page": {
      "type": "integer"
     },
     "per_page": {
      "type": "integer"
     },
     "total": {
      "type": "integer"
     },
     "total_pages": {
      "type": "integer"
     }
    }
   },
   "AnimeCard": {
    "type": "object",
    "properties": {
     "id": {
      "type": "integer"
     },
     "mal_id": {
      "type": [
       "integer",
       "null"
      ]
     },
     "title": {
      "type": "string"
     },
     "type": {
      "type": "string"
     },
     "year": {
      "type": [
       "integer",
       "null"
      ]
     },
     "status": {
      "type": [
       "string",
       "null"
      ]
     },
     "score": {
      "type": [
       "number",
       "null"
      ]
     },
     "episodes_total": {
      "type": [
       "integer",
       "null"
      ]
     },
     "sub_episodes": {
      "type": "integer"
     },
     "dub_episodes": {
      "type": "integer"
     },
     "poster": {
      "type": [
       "string",
       "null"
      ],
      "format": "uri"
     },
     "url": {
      "type": "string",
      "format": "uri"
     }
    }
   },
   "Anime": {
    "type": "object",
    "properties": {
     "id": {
      "type": "integer"
     },
     "mal_id": {
      "type": [
       "integer",
       "null"
      ]
     },
     "title": {
      "type": "string"
     },
     "alt_title": {
      "type": [
       "string",
       "null"
      ]
     },
     "native_title": {
      "type": [
       "string",
       "null"
      ]
     },
     "type": {
      "type": "string"
     },
     "status": {
      "type": [
       "string",
       "null"
      ]
     },
     "year": {
      "type": [
       "integer",
       "null"
      ]
     },
     "season": {
      "type": [
       "string",
       "null"
      ]
     },
     "aired": {
      "type": [
       "string",
       "null"
      ]
     },
     "score": {
      "type": [
       "number",
       "null"
      ]
     },
     "rating": {
      "type": [
       "string",
       "null"
      ]
     },
     "duration_min": {
      "type": [
       "integer",
       "null"
      ]
     },
     "episodes_total": {
      "type": [
       "integer",
       "null"
      ]
     },
     "sub_episodes": {
      "type": "integer"
     },
     "dub_episodes": {
      "type": "integer"
     },
     "genres": {
      "type": "array",
      "items": {
       "type": "string"
      }
     },
     "studios": {
      "type": "array",
      "items": {
       "type": "string"
      }
     },
     "description": {
      "type": [
       "string",
       "null"
      ]
     },
     "poster": {
      "type": [
       "string",
       "null"
      ]
     },
     "banner": {
      "type": [
       "string",
       "null"
      ]
     },
     "next_episode": {
      "type": [
       "object",
       "null"
      ],
      "properties": {
       "number": {
        "type": [
         "integer",
         "null"
        ]
       },
       "airs_at": {
        "type": "string",
        "format": "date-time"
       }
      }
     },
     "updated_at": {
      "type": [
       "string",
       "null"
      ]
     },
     "url": {
      "type": "string"
     },
     "watch_url": {
      "type": "string",
      "description": "Only on /lookup"
     }
    }
   },
   "Episode": {
    "type": "object",
    "properties": {
     "number": {
      "type": "number"
     },
     "title": {
      "type": [
       "string",
       "null"
      ]
     },
     "title_jp": {
      "type": [
       "string",
       "null"
      ]
     },
     "updated_at": {
      "type": [
       "string",
       "null"
      ]
     },
     "languages": {
      "type": "array",
      "items": {
       "type": "string",
       "enum": [
        "sub",
        "dub",
        "hsub"
       ]
      }
     },
     "watch_url": {
      "type": "string",
      "description": "Aniwatch watch page"
     },
     "embed_url": {
      "type": "string",
      "description": "Chrome-free player page to load in an iframe or WKWebView. Add ?lang=dub for dub."
     },
     "sources": {
      "type": "object",
      "additionalProperties": {
       "type": "string"
      },
      "description": "Raw third-party player URLs per language"
     }
    }
   },
   "LibraryItem": {
    "type": "object",
    "properties": {
     "id": {
      "type": "integer"
     },
     "mal_id": {
      "type": [
       "integer",
       "null"
      ]
     },
     "title": {
      "type": "string"
     },
     "poster": {
      "type": [
       "string",
       "null"
      ]
     },
     "added_at": {
      "type": "string",
      "format": "date-time"
     },
     "url": {
      "type": "string"
     }
    }
   },
   "HistoryItem": {
    "type": "object",
    "properties": {
     "anime_id": {
      "type": "integer"
     },
     "mal_id": {
      "type": [
       "integer",
       "null"
      ]
     },
     "title": {
      "type": "string"
     },
     "poster": {
      "type": [
       "string",
       "null"
      ]
     },
     "last_episode": {
      "type": "number"
     },
     "language": {
      "type": "string"
     },
     "episodes_watched": {
      "type": "array",
      "items": {
       "type": "number"
      }
     },
     "episodes_total": {
      "type": [
       "integer",
       "null"
      ]
     },
     "updated_at": {
      "type": "string",
      "format": "date-time"
     },
     "resume_url": {
      "type": "string"
     }
    }
   }
  }
 }
}