{
  "openapi": "3.0.3",
  "info": {
    "title": "PyPI Stats API",
    "version": "1.0.0",
    "description": "Programmatic access to PyPI download statistics, package metadata, health scores, and more.\n\n**Authentication**: Pass your API key as `?apikey=YOUR_KEY`. Keys are generated from your [dashboard](/dashboard).\n\n**Rate limits**: Free — 10 req/min · Pro — 60 req/min · Enterprise — 300 req/min"
  },
  "servers": [
    { "url": "https://pypistats.com", "description": "Production" }
  ],
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "query",
        "name": "apikey",
        "description": "Your PyPI Stats API key (e.g. `pyps_...`). Generate one from your dashboard."
      }
    },
    "schemas": {
      "TrendRow": {
        "type": "object",
        "properties": {
          "date": { "type": "string", "format": "date", "example": "2026-02-01" },
          "total_downloads": { "type": "string", "example": "1234567" }
        }
      },
      "PackageMetadata": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "example": "requests" },
          "summary": { "type": "string", "example": "Python HTTP for Humans." },
          "version": { "type": "string", "example": "2.31.0" },
          "author": { "type": "string", "example": "Kenneth Reitz" },
          "license": { "type": "string", "example": "Apache 2.0" },
          "home_page": { "type": "string", "example": "https://requests.readthedocs.io" }
        }
      },
      "HealthScore": {
        "type": "object",
        "properties": {
          "package": { "type": "string", "example": "requests" },
          "score": { "type": "integer", "minimum": 0, "maximum": 100, "example": 92 },
          "trend": { "type": "string", "enum": ["growing", "stable", "declining"], "example": "stable" },
          "breakdown": {
            "type": "object",
            "properties": {
              "consistency": { "type": "number" },
              "popularity": { "type": "number" },
              "metadata": { "type": "number" },
              "growth": { "type": "number" },
              "community": { "type": "number" }
            }
          }
        }
      },
      "SearchResult": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "summary": { "type": "string" },
          "total_downloads": { "type": "string" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" }
        }
      }
    }
  },
  "paths": {
    "/api/packages/{name}": {
      "get": {
        "tags": ["Packages"],
        "summary": "Get package analytics",
        "description": "Returns download trend data and package metadata. History depth depends on your tier (Free: 30 days, Pro: 90 days, Enterprise: 365 days).",
        "operationId": "getPackage",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "description": "Package name (e.g. `requests`, `numpy`)",
            "schema": { "type": "string" },
            "example": "requests"
          },
          {
            "name": "days",
            "in": "query",
            "description": "Number of days of history to return (default: 30)",
            "schema": { "type": "integer", "default": 30, "minimum": 1 }
          },
          {
            "name": "excludeCI",
            "in": "query",
            "description": "Exclude CI/CD downloads — requires Pro or Enterprise tier",
            "schema": { "type": "boolean", "default": false }
          }
        ],
        "responses": {
          "200": {
            "description": "Package analytics",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "trend": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/TrendRow" }
                    },
                    "metadata": { "$ref": "#/components/schemas/PackageMetadata" },
                    "tier": { "type": "string", "enum": ["FREE", "PRO", "ENTERPRISE"] }
                  }
                }
              }
            }
          },
          "404": { "description": "Package not found" },
          "429": {
            "description": "Rate limit exceeded",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    },
    "/api/packages/{name}/summary": {
      "get": {
        "tags": ["Packages"],
        "summary": "AI health summary (Pro+)",
        "description": "Returns an AI-generated plain-English health summary for the package. Requires Pro or Enterprise tier.",
        "operationId": "getPackageSummary",
        "security": [{ "apiKey": [] }],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "AI summary",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "summary": { "type": "string" },
                    "score": { "type": "integer" }
                  }
                }
              }
            }
          },
          "403": { "description": "Pro or Enterprise tier required" }
        }
      }
    },
    "/api/health-score": {
      "get": {
        "tags": ["Packages"],
        "summary": "Get health score",
        "description": "Computes a 0–100 health score based on download consistency, popularity, metadata completeness, growth, and GitHub community activity.",
        "operationId": "getHealthScore",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "package",
            "in": "query",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "Health score with breakdown",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthScore" }
              }
            }
          },
          "400": { "description": "Missing package parameter" }
        }
      }
    },
    "/api/search": {
      "get": {
        "tags": ["Discovery"],
        "summary": "Search packages",
        "description": "Search for packages by name or intent. Supports natural-language queries (e.g. \"html parser\", \"http client\") using semantic matching, as well as direct name/keyword search. Returns up to 10 results with download counts.",
        "operationId": "searchPackages",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "description": "Search query (minimum 2 characters)",
            "schema": { "type": "string", "minLength": 2 },
            "example": "http"
          }
        ],
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "results": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/SearchResult" }
                    }
                  }
                }
              }
            }
          },
          "429": { "description": "Rate limit exceeded" }
        }
      }
    },
    "/api/compare": {
      "get": {
        "tags": ["Discovery"],
        "summary": "Compare packages",
        "description": "Compare download trends for multiple packages side by side. Free tier supports up to 2 packages; Pro supports up to 10.",
        "operationId": "comparePackages",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "packages",
            "in": "query",
            "required": true,
            "description": "Comma-separated package names",
            "schema": { "type": "string" },
            "example": "flask,django,fastapi"
          },
          {
            "name": "days",
            "in": "query",
            "description": "Number of days of history (default: 30)",
            "schema": { "type": "integer", "default": 30 }
          }
        ],
        "responses": {
          "200": {
            "description": "Comparison data",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "chartData": {
                      "type": "array",
                      "description": "Daily data points keyed by package name",
                      "items": {
                        "type": "object",
                        "additionalProperties": true,
                        "example": { "date": "2026-02-01", "flask": 50000, "django": 80000 }
                      }
                    },
                    "totals": {
                      "type": "object",
                      "additionalProperties": { "type": "number" },
                      "example": { "flask": 1500000, "django": 2400000 }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/dependencies": {
      "get": {
        "tags": ["Discovery"],
        "summary": "Get dependencies",
        "description": "Returns packages this package depends on (requires_dist from PyPI) and other tracked packages that depend on it.",
        "operationId": "getDependencies",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "package",
            "in": "query",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "Dependency graph",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "dependsOn": {
                      "type": "array",
                      "items": { "type": "string" },
                      "description": "Packages this package requires"
                    },
                    "usedBy": {
                      "type": "array",
                      "items": { "type": "string" },
                      "description": "Tracked packages that depend on this one"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/export": {
      "get": {
        "tags": ["Export"],
        "summary": "Export download data (Pro+)",
        "description": "Export download data as CSV or JSON. Requires Pro or Enterprise tier.",
        "operationId": "exportData",
        "security": [{ "apiKey": [] }],
        "parameters": [
          {
            "name": "package",
            "in": "query",
            "required": true,
            "description": "Package name (use `packages` for multi-package export)",
            "schema": { "type": "string" },
            "example": "requests"
          },
          {
            "name": "packages",
            "in": "query",
            "description": "Comma-separated package names for multi-package export",
            "schema": { "type": "string" }
          },
          {
            "name": "format",
            "in": "query",
            "description": "Output format",
            "schema": { "type": "string", "enum": ["csv", "json"], "default": "csv" }
          },
          {
            "name": "days",
            "in": "query",
            "description": "Number of days to export (default: 30)",
            "schema": { "type": "integer", "default": 30 }
          }
        ],
        "responses": {
          "200": {
            "description": "Download data",
            "content": {
              "text/csv": {
                "schema": { "type": "string" },
                "example": "date,downloads\n2026-02-01,1234567\n2026-02-02,1345678"
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/TrendRow" }
                }
              }
            }
          },
          "403": { "description": "Pro or Enterprise tier required" }
        }
      }
    },
    "/api/packages/{name}/similar": {
      "get": {
        "tags": ["Discovery"],
        "summary": "Get similar packages",
        "description": "Returns packages similar to the given one, based on shared dependencies. Results are sorted by shared dependency count and download volume.",
        "operationId": "getSimilarPackages",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "Similar packages",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "packages": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "package": { "type": "string", "example": "httpx" },
                          "summary": { "type": "string", "nullable": true, "example": "The next generation HTTP client." },
                          "total_downloads": { "type": "integer", "example": 8500000 },
                          "shared_deps": { "type": "integer", "description": "Number of shared dependencies", "example": 4 }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/packages/{name}/github": {
      "get": {
        "tags": ["Packages"],
        "summary": "Get GitHub stats",
        "description": "Returns GitHub repository stats (stars, forks, open issues, last push) for a package. Data is cached for 24 hours.",
        "operationId": "getPackageGitHub",
        "security": [{ "apiKey": [] }, {}],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "GitHub repository stats",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "stars": { "type": "integer", "example": 51000 },
                    "forks": { "type": "integer", "example": 9400 },
                    "openIssues": { "type": "integer", "example": 312 },
                    "lastPushedAt": { "type": "string", "format": "date-time", "nullable": true, "example": "2026-03-15T12:00:00Z" },
                    "repoUrl": { "type": "string", "example": "https://github.com/psf/requests" }
                  }
                }
              }
            }
          },
          "404": { "description": "No GitHub repository found for this package" }
        }
      }
    },
    "/api/packages/{name}/chat": {
      "post": {
        "tags": ["Packages"],
        "summary": "Ask AI about a package (Pro+)",
        "description": "Ask a natural-language question about a package. The AI answers using live download trends and package metadata. Requires Pro or Enterprise tier.",
        "operationId": "chatAboutPackage",
        "security": [{ "apiKey": [] }],
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["question"],
                "properties": {
                  "question": {
                    "type": "string",
                    "maxLength": 500,
                    "example": "Is this package actively maintained?"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "AI-generated answer",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "answer": { "type": "string", "example": "Yes, requests is actively maintained with regular releases..." }
                  }
                }
              }
            }
          },
          "403": { "description": "Pro or Enterprise tier required" }
        }
      }
    },
    "/api/badges/{name}": {
      "get": {
        "tags": ["Badges"],
        "summary": "Download count badge",
        "description": "Returns an SVG badge showing the package's recent download count. Embed in your README with `![Downloads](https://pypistats.com/api/badges/{name})`.",
        "operationId": "getBadge",
        "parameters": [
          {
            "name": "name",
            "in": "path",
            "required": true,
            "description": "Package name",
            "schema": { "type": "string" },
            "example": "requests"
          }
        ],
        "responses": {
          "200": {
            "description": "SVG badge image",
            "content": {
              "image/svg+xml": {
                "schema": { "type": "string" }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    { "name": "Packages", "description": "Package analytics, metadata, and GitHub stats" },
    { "name": "Discovery", "description": "Search, compare, explore, and find similar packages" },
    { "name": "Export", "description": "Download data in bulk formats" },
    { "name": "Badges", "description": "Embeddable SVG badges for READMEs" }
  ]
}
