A script that fetches conversations from ChatGPT to archive them locally.
Fetch conversations with the
fetchcommand, and convert them to Markdown withconvert. See Usage for details.
Before running the script, you'll need to acquire the cookie and token for ChatGPT. Open ChatGPT in a browser window and open the browser DevTools.
⚠️ NB: You must be logged in
⚠️ Make sure to keep these values secret!
Run the following in the Console, and paste the output of the command in a file called .env.
fetch(`${new URL(location.href).origin}/api/auth/session`).then((res) => res.json().then((session) => {
console.log(`export OAI_AUTHZ_TOKEN="${session.accessToken}"\nexport OAI_ACC_ID="${session.account.id}"\nexport OAI_COOKIE="${document.cookie}"`);
}))You may alternatively acquire the access token, account ID, and cookie by hand. This can be done via the browser DevTools (Network tab) by inspecting the requests and locating the values for the following (request) headers:
authorization: Remove theBearerprefix, store asOAI_AUTHZ_TOKENchatgpt-account-id: Store asOAI_ACC_IDcookie: Store asOAI_COOKIE
You may filter by "Fetch/XHR" requests to the path /backend-api/ to find eligible requests.
Save these values in a file called .env in the following format:
export OAI_AUTHZ_TOKEN="<authorization>"
export OAI_ACC_ID="<chatgpt-account-id>"
export OAI_COOKIE="<cookie>"Install the dependencies
pip install -r requirements.txt# Print the help
python main.py --help
Usage: main.py [OPTIONS] COMMAND [ARGS]...
╭─ Options ────────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────╮
│ fetch-project Fetch conversations for a specific project from ChatGPT │
│ fetch Fetch conversations from ChatGPT │
│ convert Convert fetched conversations to Markdown │
╰──────────────────────────────────────────────────────────────────────────────────╯# Source the environment variables with the cookie and token from the Configuration step above
source .env
# Run the script to fetch the conversations
python main.py fetch
# Run the script to convert the conversations from JSON to Markdown
python main.py convert
# If you want to re-fetch or re-convert all the files, run with the `--no-skip-existing` flag
python main.py fetch --no-skip-existing
python main.py convert --no-skip-existing
# If you want to fetch conversations for a specific project, run 'fetch-project'. Note that <project_name> is case-insensitive.
python main.py fetch-project <project_name>- The output files are stored in the directory
out/. - The command
fetchretrieves and saves conversation files as JSON. - The command
convertconverts the retrieved JSON files to Markdown.
- Individual conversation files are named by the conversation's title.
- Conversations associated with a project are stored in a subdirectory named after the project.
- Conversations without a project are stored in a subdirectory named
recent_conversations.
- A list of projects associated with the ChatGPT account is stored in the file
projects.json. - A list of conversations associated with a project is stored in a file named
<project_name>/conversations_g-p-<project_id>.
Not affiliated with OpenAI or ChatGPT. This project is provided as-is. You are responsible for your own usage and any violation of terms, agreements or law.