Skip to content

svandragt/chatgpt-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatGPT Conversation Exporter

Export a single ChatGPT conversation to Markdown (and optionally raw JSON) using its conversation ID.

This tool fetches the same structured JSON the ChatGPT web app uses and converts it into a clean, portable Markdown file suitable for archiving, version control, or further processing.


Features

  • Export a single conversation by ID
  • Clean Markdown output
  • Optional raw JSON export
  • Preserves roles and timestamps
  • No scraping — uses the same backend endpoint as the web UI

Example Output

# How to export chat

_Exported: 2026-02-25T12:34:56+00:00_

## USER (2026-02-25T12:00:00+00:00)

How do I export this chat?

## ASSISTANT (2026-02-25T12:00:02+00:00)

Here are your options...

Requirements

  • Python 3.10+
  • requests
  • A valid authenticated ChatGPT web session

Installation

Using UV (recommended)

uv init
uv add requests

Using pip

pip install requests

Clone the repository and place yourself in the project directory.


Usage

Basic export to Markdown:

python export_chat.py <CONVERSATION_ID> --bearer "$CHATGPT_BEARER"

Export Markdown + raw JSON:

python export_chat.py <CONVERSATION_ID> \
  --bearer "$CHATGPT_BEARER" \
  --out-md chat.md \
  --out-json chat.json

Using a session cookie instead of a Bearer token:

python export_chat.py <CONVERSATION_ID> --cookie "$CHATGPT_COOKIE"

Getting the Conversation ID

From the browser URL:

https://chatgpt.com/c/<CONVERSATION_ID>

Copy the <CONVERSATION_ID> part.


Getting Authentication

Open DevTools → Network → reload the conversation page.

Find the request:

/backend-api/conversation/<id>

From Request Headers, copy either:

Option A — Bearer token (preferred)

Authorization: Bearer <token>

Then:

export CHATGPT_BEARER="PASTE_TOKEN"

Option B — Cookie

Copy the full Cookie: header or just the session token:

export CHATGPT_COOKIE="__Secure-next-auth.session-token=..."

Then run the script.


Output Files

By default:

<conversation_id>.md

Optional:

<conversation_id>.json

Markdown includes:

  • Conversation title
  • Export timestamp
  • Chronologically sorted messages
  • Roles (user, assistant, etc.)
  • ISO 8601 timestamps (UTC)

How It Works

  1. Calls:

    GET /backend-api/conversation/<id>
    
  2. Parses the mapping message tree

  3. Extracts message content

  4. Sorts by create_time

  5. Renders Markdown

No DOM inspection. No scrolling hacks. No partial exports.


Limitations

  • Requires active authenticated session
  • API endpoint is undocumented and may change
  • Messages are sorted strictly by timestamp (not branch-aware)
  • Attachments are not downloaded (text content only)

Security Notes

  • Treat Bearer tokens like passwords
  • Never commit tokens to version control
  • Revoke your session if a token is exposed
  • JSON output may contain metadata

Suggested Archival Workflow

python export_chat.py <id>
git add <id>.md
git commit -m "Archive chat <id>"

Keeps conversations searchable and portable without using the full account export.


Possible Extensions

  • Branch-aware export (follow active path)
  • HTML output
  • JSONL output for indexing
  • Batch export multiple conversation IDs
  • Attachment downloading

Disclaimer

This project uses an internal backend endpoint exposed to the ChatGPT web client. It is not an officially documented public API and may change without notice. This project is based on an initial version, generated by ChatGPT.

About

Exporting chat conversations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages