Automatically archive your LeetCode solutions to a GitHub repo — no n8n, no low-code tools, just Python.
Every time you solve a problem on LeetCode, running this script pushes:
- The problem description (with example testcases) as
problem.md - Your actual accepted solution code, versioned by attempt number and date
...straight to a GitHub repo, so your solving history lives somewhere permanent and browsable.
I kept manually copy-pasting solutions into GitHub after solving problems. This automates that — but stays honest about what it actually is: a manually-triggered sync script, not a background daemon. You run it, it checks your recent accepted submissions, and pushes anything new.
- Hits LeetCode's unofficial GraphQL endpoint (
leetcode.com/graphql) — there's no official public API for this — authenticated via your session cookie. - Fetches your recent accepted submissions, filters out anything already synced (tracked locally in
sync_state.json). - For each new submission: fetches the full code + the problem description, converts the description HTML to Markdown, and pushes both to GitHub via the Contents API (no local git clone needed).
your-solutions-repo/
├── two-sum/
│ ├── problem.md
│ └── solutions/
│ ├── solution_1_2026-07-15.py
│ └── solution_2_2026-07-20.py
├── add-two-numbers/
│ ├── problem.md
│ └── solutions/
│ └── solution_1_2026-07-16.java
problem.mdis overwritten each time (the description doesn't change).- Every new accepted submission for the same question gets a new versioned file — previous attempts are never overwritten. Useful for seeing how your approach to a problem evolved over time.
- File extension is chosen automatically based on the submission's language.
python -m venv venv
venv\Scripts\activate # Windowspip install -r requirements.txtCopy .env.example to .env and fill in:
| Variable | Where to get it |
|---|---|
LEETCODE_SESSION |
Browser DevTools → Cookies → leetcode.com → LEETCODE_SESSION value |
LEETCODE_CSRF_TOKEN |
Same place → csrftoken value |
LEETCODE_USERNAME |
Your LeetCode profile URL: leetcode.com/u/<username>/ |
GITHUB_TOKEN |
GitHub → Settings → Developer settings → Fine-grained PAT, scoped to Contents: read/write on one repo only |
GITHUB_REPO |
Format: username/repo-name |
GITHUB_BRANCH |
Defaults to main |
Never commit .env — it's already covered by .gitignore.
⚠️ LEETCODE_SESSIONis a login session, not a long-lived token. It will expire periodically and needs to be manually refreshed from DevTools when the script starts failing with an auth error.
python sync.pyThat's it — one command. It checks your most recent accepted submissions (default: last 20, configurable via SUBMISSIONS_PER_POLL), skips anything already synced, and pushes the rest.
This is manually triggered, by design — run python sync.py after you solve a problem (or whenever you want to catch up). There's no background loop or scheduler set up by default; run_once() is the intended entry point, not run_forever().
If you want it to run automatically, wire it up to Windows Task Scheduler (or cron elsewhere) to call python sync.py on an interval — but note this means failures (e.g. an expired session cookie) can go unnoticed until you check.
- Unofficial API. LeetCode has no public API for this. The GraphQL schema could change without notice — there's no SLA. If a call that used to work suddenly errors, this is the first thing to suspect.
- Session expiry.
LEETCODE_SESSIONneeds periodic manual refresh — there's no auto-login flow. - Only example testcases. Hidden judge testcases are intentionally not captured — only the example testcases shown in the problem description.
- HTML→Markdown conversion.
markdownifyhandles most LeetCode HTML cleanly, but edge cases exist — for example,<sup>tags (used for exponents like10^9) are stripped by default and were patched to convert to^notation before conversion. Watch for similarly unusual HTML in problem descriptions.
leet-to-github/
├── config.py # loads and validates env vars
├── state.py # local JSON state (dedup + version counters)
├── leetcode_client.py # unofficial LeetCode GraphQL client
├── github_pusher.py # GitHub Contents API wrapper
├── sync.py # orchestrates everything, run_once() entry point
├── requirements.txt
├── .env.example
└── sync_state.json # generated at runtime, not committed
requestspython-dotenvmarkdownify