{
  "openapi": "3.0.0",
  "info": {
    "title": "Podr API",
    "version": "1.0.0",
    "description": "RESTful API for podcast search and discovery powered by iTunes API",
    "contact": {
      "name": "Podr",
      "url": "https://www.podrapp.com/"
    },
    "license": {
      "name": "MIT",
      "url": "https://github.com/cascadiacollections/podr-service/blob/main/LICENSE"
    }
  },
  "servers": [
    {
      "url": "https://podr-service.cascadiacollections.workers.dev",
      "description": "Production server"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "summary": "Podcast API Endpoint",
        "description": "Multi-purpose endpoint that serves API schema (no query params), searches podcasts (with q parameter), or returns top podcasts (with q=toppodcasts)",
        "operationId": "podcastApi",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "description": "Query parameter that determines the operation. Omit to get API schema. Set to search term to search podcasts. Set to \"toppodcasts\" to get top podcasts.",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 200,
              "example": "javascript"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Number of results to return (applies to search and top podcasts)",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 15,
              "minimum": 1,
              "maximum": 200,
              "example": 15
            }
          },
          {
            "name": "genre",
            "in": "query",
            "description": "Genre ID to filter by (applies only to top podcasts). Available genres: 1301 (Arts), 1302 (Comedy), 1303 (Education), 1304 (Kids & Family), 1305 (Health & Fitness), 1306 (TV & Film), 1307 (Music), 1308 (News), 1309 (Religion & Spirituality), 1310 (Science), 1311 (Sports), 1312 (Technology), 1313 (Business), 1314 (Society & Culture), 1315 (Government), 1321 (Fiction), 1323 (History), 1324 (True Crime), 1325 (Leisure), 1326 (Documentary)",
            "required": false,
            "schema": {
              "type": "integer",
              "enum": [
                1301,
                1302,
                1303,
                1304,
                1305,
                1306,
                1307,
                1308,
                1309,
                1310,
                1311,
                1312,
                1313,
                1314,
                1315,
                1321,
                1323,
                1324,
                1325,
                1326
              ],
              "example": 1312
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response - format depends on query parameters",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "object",
                      "description": "OpenAPI schema (when no q parameter)",
                      "properties": {
                        "openapi": {
                          "type": "string"
                        },
                        "info": {
                          "type": "object"
                        },
                        "paths": {
                          "type": "object"
                        }
                      }
                    },
                    {
                      "type": "object",
                      "description": "Search results (when q is a search term)",
                      "properties": {
                        "resultCount": {
                          "type": "integer"
                        },
                        "results": {
                          "type": "array",
                          "items": {
                            "type": "object"
                          }
                        }
                      }
                    },
                    {
                      "type": "object",
                      "description": "Top podcasts feed (when q=toppodcasts)",
                      "properties": {
                        "feed": {
                          "type": "object",
                          "properties": {
                            "entry": {
                              "type": "array",
                              "items": {
                                "type": "object"
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "Cache duration: 1 year (immutable) for schema, 24 hours for search, 2 hours for top podcasts",
                "schema": {
                  "type": "string",
                  "example": "public, max-age=31536000, immutable"
                }
              },
              "X-Cache": {
                "description": "Cache hit indicator",
                "schema": {
                  "type": "string",
                  "enum": [
                    "HIT",
                    "MISS"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request - missing or invalid query parameter"
          },
          "405": {
            "description": "Method not allowed - only GET is supported"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    },
    "/health": {
      "get": {
        "summary": "Health Check",
        "description": "Basic liveness check returning service status and placement info",
        "operationId": "healthCheck",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": [
                        "healthy"
                      ]
                    },
                    "timestamp": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "version": {
                      "type": "string"
                    },
                    "circuitBreaker": {
                      "type": "string",
                      "enum": [
                        "closed",
                        "open",
                        "half-open"
                      ]
                    },
                    "placement": {
                      "type": "object",
                      "properties": {
                        "colo": {
                          "type": "string"
                        },
                        "country": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/health/deep": {
      "get": {
        "summary": "Deep Health Check",
        "description": "Tests upstream iTunes API connectivity",
        "operationId": "deepHealthCheck",
        "responses": {
          "200": {
            "description": "All systems healthy"
          },
          "503": {
            "description": "Upstream service degraded"
          }
        }
      }
    },
    "/trending": {
      "get": {
        "summary": "Trending Queries",
        "description": "Returns trending podcast search queries from the last 7 days. Feature-flagged endpoint - returns 404 when disabled. Supports geographic filtering with fallback to global trending.",
        "operationId": "trendingQueries",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Number of trending queries to return",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 10,
              "minimum": 1,
              "maximum": 50
            }
          },
          {
            "name": "country",
            "in": "query",
            "description": "ISO 3166-1 alpha-2 country code to filter trending queries (e.g., US, GB, JP). Falls back to global trending if no country-specific data available.",
            "required": false,
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z]{2}$",
              "example": "US"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Trending queries list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "trending": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "query": {
                            "type": "string",
                            "description": "The search query"
                          },
                          "count": {
                            "type": "integer",
                            "description": "Number of searches in the period"
                          }
                        }
                      }
                    },
                    "period": {
                      "type": "string",
                      "description": "Time period for trending data",
                      "example": "7d"
                    },
                    "country": {
                      "type": "string",
                      "description": "Requested country code for the trending data (or \"global\" if no valid country was provided). Data may fall back to global results when no country-specific data exists.",
                      "example": "US"
                    },
                    "generatedAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "When this data was generated"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Feature not enabled"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    },
    "/suggest": {
      "get": {
        "summary": "Search Suggestions",
        "description": "Returns autocomplete suggestions based on popular search queries. Feature-flagged endpoint (uses trendingQueries flag) - returns 404 when disabled.",
        "operationId": "searchSuggestions",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "description": "Search prefix to get suggestions for (minimum 2 characters)",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 2,
              "example": "jav"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Number of suggestions to return",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 5,
              "minimum": 1,
              "maximum": 10
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Search suggestions list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "suggestions": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "List of suggested search queries",
                      "example": [
                        "javascript",
                        "java programming",
                        "jazz podcasts"
                      ]
                    },
                    "query": {
                      "type": "string",
                      "description": "The original query prefix",
                      "example": "jav"
                    }
                  }
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "Cache duration: 5 minutes",
                "schema": {
                  "type": "string",
                  "example": "public, max-age=300"
                }
              }
            }
          },
          "404": {
            "description": "Feature not enabled"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    },
    "/semantic-search": {
      "get": {
        "summary": "Semantic Search",
        "description": "Performs semantic search using AI embeddings to find podcasts similar to the query meaning, even without exact keyword matches. Feature-flagged endpoint - returns 404 when disabled.",
        "operationId": "semanticSearch",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "description": "Search query for semantic similarity matching",
            "required": true,
            "schema": {
              "type": "string",
              "maxLength": 200,
              "example": "learn about artificial intelligence"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Number of results to return",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 10,
              "minimum": 1,
              "maximum": 10
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Semantic search results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "query": {
                      "type": "string",
                      "description": "The original search query",
                      "example": "learn about artificial intelligence"
                    },
                    "results": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string",
                            "description": "Unique podcast identifier"
                          },
                          "score": {
                            "type": "number",
                            "description": "Similarity score (0-1, higher is more similar)",
                            "example": 0.89
                          },
                          "title": {
                            "type": "string",
                            "description": "Podcast title"
                          },
                          "description": {
                            "type": "string",
                            "description": "Podcast description"
                          },
                          "artworkUrl": {
                            "type": "string",
                            "description": "Podcast artwork URL"
                          },
                          "feedUrl": {
                            "type": "string",
                            "description": "Podcast RSS feed URL"
                          }
                        }
                      },
                      "description": "List of semantically similar podcasts"
                    },
                    "resultCount": {
                      "type": "integer",
                      "description": "Number of results returned",
                      "example": 10
                    }
                  }
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "Cache duration: 4 hours",
                "schema": {
                  "type": "string",
                  "example": "public, max-age=14400"
                }
              },
              "X-Cache": {
                "description": "Cache hit indicator",
                "schema": {
                  "type": "string",
                  "enum": [
                    "HIT",
                    "MISS"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request - missing or invalid query parameter"
          },
          "404": {
            "description": "Feature not enabled"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    },
    "/podcast/{id}": {
      "get": {
        "summary": "Podcast Detail",
        "description": "Returns detailed information about a specific podcast including recent episodes. Optionally includes the podcast description from the RSS feed when the summary query parameter is set to true.",
        "operationId": "podcastDetail",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "The iTunes podcast ID",
            "required": true,
            "schema": {
              "type": "integer",
              "example": 1535809341
            }
          },
          {
            "name": "summary",
            "in": "query",
            "description": "When set to true, includes the podcast description from the RSS feed (feature-flagged)",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "true",
                "false"
              ],
              "default": "false"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Podcast details with episodes",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "podcast": {
                      "type": "object",
                      "properties": {
                        "trackId": {
                          "type": "integer",
                          "description": "iTunes podcast ID"
                        },
                        "trackName": {
                          "type": "string",
                          "description": "Podcast name"
                        },
                        "artworkUrl600": {
                          "type": "string",
                          "description": "Podcast artwork URL"
                        },
                        "feedUrl": {
                          "type": "string",
                          "description": "RSS feed URL"
                        },
                        "genres": {
                          "type": "array",
                          "items": {
                            "type": "string"
                          },
                          "description": "Podcast genres"
                        }
                      }
                    },
                    "episodes": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "trackId": {
                            "type": "integer",
                            "description": "Episode ID"
                          },
                          "trackName": {
                            "type": "string",
                            "description": "Episode title"
                          },
                          "releaseDate": {
                            "type": "string",
                            "format": "date-time",
                            "description": "Episode release date"
                          },
                          "trackTimeMillis": {
                            "type": "integer",
                            "description": "Episode duration in milliseconds"
                          },
                          "description": {
                            "type": "string",
                            "description": "Episode description"
                          }
                        }
                      },
                      "description": "List of recent episodes (up to 20)"
                    },
                    "summary": {
                      "type": "string",
                      "description": "Podcast description from the RSS feed (only included when summary=true and feature is enabled)",
                      "maxLength": 500
                    }
                  }
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "Cache duration: 4 hours",
                "schema": {
                  "type": "string",
                  "example": "public, max-age=14400"
                }
              },
              "X-Cache": {
                "description": "Cache hit indicator",
                "schema": {
                  "type": "string",
                  "enum": [
                    "HIT",
                    "MISS"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Invalid podcast ID"
          },
          "404": {
            "description": "Podcast not found"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    },
    "/related": {
      "get": {
        "summary": "Related Podcasts",
        "description": "Returns similar podcasts based on a given podcast ID. Matches by genre.",
        "operationId": "relatedPodcasts",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "description": "The iTunes podcast ID to find related podcasts for",
            "required": true,
            "schema": {
              "type": "integer",
              "example": 1535809341
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Number of related podcasts to return",
            "required": false,
            "schema": {
              "type": "integer",
              "default": 10,
              "minimum": 1,
              "maximum": 20,
              "example": 10
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Related podcasts list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "related": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "trackId": {
                            "type": "integer",
                            "description": "iTunes podcast ID"
                          },
                          "trackName": {
                            "type": "string",
                            "description": "Podcast name"
                          },
                          "genre": {
                            "type": "string",
                            "description": "Matched genre"
                          },
                          "artworkUrl600": {
                            "type": "string",
                            "description": "Podcast artwork URL"
                          },
                          "artistName": {
                            "type": "string",
                            "description": "Podcast artist/author"
                          }
                        }
                      },
                      "description": "List of related podcasts"
                    },
                    "sourceId": {
                      "type": "integer",
                      "description": "The source podcast ID used for matching"
                    },
                    "matchedBy": {
                      "type": "string",
                      "enum": [
                        "genre"
                      ],
                      "description": "The method used to find related podcasts"
                    }
                  }
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "description": "Cache duration: 4 hours",
                "schema": {
                  "type": "string",
                  "example": "public, max-age=14400"
                }
              },
              "X-Cache": {
                "description": "Cache hit indicator",
                "schema": {
                  "type": "string",
                  "enum": [
                    "HIT",
                    "MISS"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Missing or invalid id parameter"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    }
  }
}