├── .gitignore ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leetcode", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "start": "node index.js", 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "description": "" 13 | } 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const LEETCODE_SESSION = "MY_LEETCODE_SESSION" 2 | const CSRF_TOKEN = "MY_CSRF_TOKEN" 3 | const NEW_SESSION_NAME = "MyNewSession1"; 4 | const UA = "Mozilla/5.0"; 5 | 6 | const fetchWithRetry = async (url, options, retries) => { 7 | for (let i = 0; i < retries; i++) { 8 | try { 9 | const response = await fetch(url, options); 10 | if (response.ok) { 11 | console.log('Request succeeded:', response); 12 | return response; 13 | } else { 14 | console.log('Request failed'); 15 | } 16 | } catch (error) { 17 | console.log('Fetch error:', error); 18 | } 19 | } 20 | console.log(`All ${retries} attempts failed.`); 21 | }; 22 | 23 | const url = "https://leetcode.com/session/"; 24 | const options = { 25 | headers: { 26 | "content-type": "application/json", 27 | "x-csrftoken": CSRF_TOKEN, 28 | "User-Agent": UA, 29 | "x-requested-with": "XMLHttpRequest", 30 | "cookie": `LEETCODE_SESSION=${LEETCODE_SESSION};` 31 | }, 32 | body: JSON.stringify({ 33 | func: "create", 34 | name: NEW_SESSION_NAME 35 | }), 36 | method: "PUT" 37 | }; 38 | const maxRetries = 50; // Set the maximum number of retries you are welcome to change this value 39 | 40 | fetchWithRetry(url, options, maxRetries); 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Session Creator 2 | 3 | **[DEPRECATED]** Leetcode has already moved on to their new lists feature. This repository will no longer be of use. 4 | 5 | ---------------------------------------------- 6 | 7 | Recently Leetcode started to deprecate the old session system, 8 | and the button to create a new session is not clickable, 9 | sooo we cannot create new sessions anymore. 10 | This is a simple script that creates a new session for you. 11 | 12 | ![image](https://github.com/user-attachments/assets/95a6c543-90d8-483c-ae67-63e1dc5e543a) 13 | 14 | ## How to use 15 | 16 | 1. Clone the repository, make sure you have Node.js version 18+ installed 17 | 2. Open index.js 18 | 3. Enter your Leetcode session under LEETCODE_SESSION 19 | 4. Enter your Leetcode csrf token under CSRF_TOKEN 20 | 5. Change NEW_SESSION_NAME to the name of the new session you want to create 21 | 6. Fire up your terminal and run `node index.js` 22 | 7. Enjoy 23 | 24 | WARNING: Sometimes Leetcode servers reject requests with 403 (maybe it hits new (post deprecation) server nodes with their Load Balancer 25 | (just a guess). 26 | 27 | I have added retries count to make requests run until it passes with 200. If it stops working completely (and I'm sure it will after some time) 28 | it's nothing we can do. 29 | 30 | ## How to get Leetcode session and csrf token 31 | 32 | 1. Open Leetcode in your browser 33 | 2. Open browser's developer tools 34 | 3. Go to Application tab 35 | 4. Find the cookies section 36 | 5. Copy the value of `LEETCODE_SESSION` and `csrftoken` 37 | 38 | ## Disclaimer 39 | 40 | Use at your own risk. Since this is not an official API. Leetcode may change the way they handle sessions at any time, 41 | remove this endpoint, rate limit it, or do anything else. Since this script is using deprecated way 42 | of creating sessions, it's just a matter of time when they will remove it and this script will stop working. 43 | --------------------------------------------------------------------------------