Updated November 5, 2026 · Maintained by huanglizhuo

Can the GitHub API count lines of code?

No — neither the GitHub REST API nor the GraphQL API has an endpoint that returns line counts. The closest signal, GET /repos/{owner}/{repo}/languages, returns bytes per language, and the GraphQL Repository.diskUsage field returns kilobytes. For actual line counts over an API, use the OctoCounts API: it downloads a public repository's source archive, counts it with tokei, and returns files, code lines, comment lines, blank lines, and per-language totals as JSON, pinned to a commit SHA.

Why doesn't the GitHub API return line counts?

GitHub's language API reports bytes per detected language, not source lines. Linguist uses language detection and repository attributes, and excludes categories such as vendored and generated files from language statistics. Byte share can differ from code-line share, and the API does not expose code, comment, or blank line totals. See GitHub Linguist's documentation at GitHub Linguist for its classification rules.

Which API returns line counts for a public GitHub repository?

Use three endpoint types, with no token. Poll the job endpoint until completed; queued or running responses require another poll, and failed jobs must not be fetched as reports:

curl -s -X POST https://api.octocounts.com/api/analyze \
  -H "Content-Type: application/json" \
  -d '{"repoUrl": "https://github.com/react/react"}'
# => {"jobId": "..."}

curl -s "https://api.octocounts.com/api/jobs/:jobId?wait=20"
# => {"status": "completed", "reportId": "..."}

curl -s https://api.octocounts.com/api/reports/:reportId
# => totals for files, lines, code, comments, blanks + per-language rows

The long-poll form GET /api/jobs/:jobId?wait=20 holds the request until a status change or timeout (capped at 25 seconds). It may return queued or running; repeat the poll until the job completes or fails. Reports are cached by repository, commit SHA, tokei version, and analysis options, so a warm repository can return completed from cache. The full request body — including refName for a branch, tag, or commit and the analysis options — is documented in the OctoCounts API docs.

How do I count lines of code in Python?

A minimal Python client uses a five-minute polling deadline and fails explicitly on analysis errors. Keep the printed job ID to resume polling after a timeout or HTTP error:

import json, sys, time, urllib.request

API = "https://api.octocounts.com/api"
body = json.dumps({"repoUrl": "https://github.com/react/react"}).encode()
job = json.load(urllib.request.urlopen(urllib.request.Request(
    f"{API}/analyze", data=body, headers={"Content-Type": "application/json"}), timeout=30))
print(f"Job ID: {job['jobId']}", file=sys.stderr)

deadline = time.monotonic() + 300
while time.monotonic() < deadline:
    status = json.load(urllib.request.urlopen(
        f"{API}/jobs/{job['jobId']}?wait=20", timeout=30))
    if status["status"] == "failed":
        raise RuntimeError(status.get("error") or "Analysis failed")
    if status["status"] == "completed":
        break
    time.sleep(1)
else:
    raise TimeoutError(f"Resume polling job {job['jobId']} instead of submitting again")

report = json.load(urllib.request.urlopen(
    f"{API}/reports/{status['reportId']}", timeout=30))
print(report["totals"])  # {"files": ..., "lines": ..., "code": ..., ...}

Retry semantics matter in CI: treat 429 and 503 responses as retryable using their Retry-After header, never replay an analyze POST after an ambiguous network failure, and resume from jobId instead of submitting again. The API docs spell out the full failure-handling contract.

Can I get line counts without writing any code?

Yes — two zero-code routes. The badge endpoints return an SVG with the number inside, usable anywhere an image works:

https://api.octocounts.com/badge/:owner/:repo                 # total code lines
https://api.octocounts.com/badge/:owner/:repo?type=lines      # all lines
https://api.octocounts.com/badge/:owner/:repo?lang=Rust       # one language

And every report page renders as plain markdown by appending .md, so an agent or script can read the counts as text: https://octocounts.com/github/react/react.md. The web app itself needs nothing but a pasted URL.

How do I count lines of code at a specific commit via API?

Set refName to a branch, tag, or full commit SHA. The report records the resolved commit and engine version. Matching results require the same source tree, engine version, and effective counting rules; matching retained cache entries avoid a new count. A commit URL selects the current canonical analysis for that commit, while a saved-report link and exported JSON identify a particular analysis while it is retained.

Does the OctoCounts API need a token or account?

No. It analyzes public github.com repositories only, does not require GitHub account access for analysis, and does not accept uploads. Default analyze limits are 5 requests/minute per IP (burst 10); force-refresh also defaults to 2 requests/minute (burst 2). Deployments may configure different limits. For private repositories, clone locally and run tokei or cloc — the tokei online guide compares the two workflows.