Optimizing LeetCode Workflows with leetcode-cli
Move your LeetCode practice from the browser to your local IDE. Learn how to install, configure, and use leetcode-cli to scaffold problems and submit solutions via the terminal.
17 Feb 2026, 08:11 UTC

The Friction of Browser-Based Coding
Solving algorithmic problems in a browser-based IDE often introduces friction: lack of custom keybindings, limited debugging tools, and the inability to leverage local linting or static analysis. For engineers preparing for technical interviews, moving the development cycle to a local environment increases productivity and mirrors real-world software engineering workflows.
The leetcode-cli tool allows you to fetch problems, scaffold files, and submit solutions directly from your terminal. This removes the need to manually copy-paste code between an IDE and a browser window.
Prerequisites
- Node.js: Version 18 or higher is required to run the CLI.
- NPM: Installed alongside Node.js for package management.
- LeetCode Account: An active account (Free or Premium) to authenticate session cookies.
- Local Editor: A configured text editor (e.g., VS Code, Vim, or IntelliJ) for writing the solutions.
Implementation Procedure
1. Installation and Environment Setup
Install the CLI globally using npm. Run this command in your terminal with the necessary permissions to write to global node_modules:
npm i -g leetcode-cli
Verify the installation by checking the version:
leetcode --version
2. Authentication via Session Cookies
Because LeetCode does not provide a public API key for users, the CLI authenticates using session cookies. This process links your local environment to your account identity.
- Run the authentication command:
leetcode auth -u <your_username> - Follow the terminal prompts to provide the required cookies (usually
LEETCODE_SESSIONandcsrftoken) captured from your browser's developer tools. - Verify the connection by running
leetcode user. This should display your username and current subscription status.
3. Local Problem Scaffolding
Instead of manually creating files, use the show command to download the problem description and the starter boilerplate. This ensures the function signatures match exactly what the LeetCode judge expects.
Example: Fetching "Two Sum" in Python 3
leetcode show two-sum --lang python3
This command creates a local file (e.g., two-sum.py) containing the class Solution: structure. You can now open this file in your preferred IDE to implement the logic.
4. Testing and Submission
The CLI interacts with LeetCode's GraphQL endpoints to run your code against the judge's test cases.
- Testing: Run
leetcode test two-sum.py. This sends your code to the server and returns the result (e.g., Accepted, Wrong Answer, or Time Limit Exceeded) without recording a formal submission. - Submitting: Once the tests pass, run
leetcode submit two-sum.py. This creates a permanent record of the submission on your profile.
Configuration and Customization
The CLI behavior is governed by a configuration file located at ~/.leetcode/leetcode.toml. Modifying this file allows you to set defaults and avoid repetitive flags.
| Setting | Purpose | Example Value |
|---|---|---|
default_language |
Sets the language for show commands. |
"python3" or "cpp" |
editor |
The command used to open files. | "code" (for VS Code) |
template |
Custom boilerplate to inject into new files. | Path to a .txt file |
Operational Risks and Limitations
API Stability
The leetcode-cli relies on LeetCode's private GraphQL API. Since this API is undocumented, changes to the site's backend can break CLI functionality. If leetcode test or leetcode submit suddenly fails with 403 or 500 errors, check for a package update via npm update -g leetcode-cli.
Security Considerations
Authentication cookies are stored in plain text within ~/.leetcode/credentials. On shared machines, this is a security risk. It is recommended to use a dedicated user profile or a containerized environment to isolate these credentials.
Execution Logic
It is important to note that leetcode test is not a local execution. The code is sent to LeetCode's servers for evaluation. You cannot run these tests offline, and they still consume your account's submission/request quota.
Verification and Rollback
To verify the setup is working correctly, execute the following sequence:
- Run
leetcode userto confirm authentication. - Run
leetcode show two-sum --lang python3and verify the file exists in your directory. - Run
leetcode test two-sum.pyand ensure the terminal returns a judge response.
Rollback: To remove the CLI and all stored credentials, run npm uninstall -g leetcode-cli and delete the configuration directory: rm -rf ~/.leetcode.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.