Skip to main content

GET /api/document

Retrieve all versions of a document by ID.

Authentication

Requires authenticated session. Returns 401 if not authenticated.

Query parameters

id
string
required
The document ID to retrieve

Request example

GET /api/document?id=550e8400-e29b-41d4-a716-446655440000

Response

Returns an array of document versions ordered by creation date.
documents
array
Array of document versions
[
  {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "title": "React Component",
    "content": "function Button() { return <button>Click</button>; }",
    "kind": "code",
    "userId": "123e4567-e89b-12d3-a456-426614174000",
    "createdAt": "2024-01-15T10:30:00Z"
  },
  {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "title": "React Component",
    "content": "function Button({ label }) { return <button>{label}</button>; }",
    "kind": "code",
    "userId": "123e4567-e89b-12d3-a456-426614174000",
    "createdAt": "2024-01-15T10:35:00Z"
  }
]

Document properties

id
string
Document UUID
title
string
Document title
content
string
Document content (can be null)
kind
string
Document type: "text", "code", "image", or "sheet"
userId
string
Owner user ID
createdAt
string
ISO 8601 timestamp

Error responses

400
Bad Request
Missing document ID parameter
401
Unauthorized
Not authenticated
403
Forbidden
Document belongs to different user
404
Not Found
Document does not exist
From app/(chat)/api/document/route.ts:10-40

POST /api/document

Create a new version of a document or save a new document.

Authentication

Requires authenticated session. Returns 401 if not authenticated.

Query parameters

id
string
required
The document ID (UUID format)

Request body

title
string
required
Document title
content
string
required
Document content
kind
string
required
Document type: "text", "code", "image", or "sheet"

Request example

{
  "title": "React Button Component",
  "content": "function Button({ label, onClick }) {\n  return <button onClick={onClick}>{label}</button>;\n}",
  "kind": "code"
}

URL example

POST /api/document?id=550e8400-e29b-41d4-a716-446655440000

Response

Returns the created document.
{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "title": "React Button Component",
  "content": "function Button({ label, onClick }) { return <button onClick={onClick}>{label}</button>; }",
  "kind": "code",
  "userId": "123e4567-e89b-12d3-a456-426614174000",
  "createdAt": "2024-01-15T10:30:00Z"
}

Error responses

400
Bad Request
Missing required parameters
401
Unauthorized
Not authenticated
403
Forbidden
Attempting to update document owned by different user

Document versioning

Documents use composite primary key (id, createdAt), allowing multiple versions:
  • Each save creates a new version with same ID but different timestamp
  • Retrieve all versions with GET endpoint
  • Delete versions after specific timestamp with DELETE endpoint
From app/(chat)/api/document/route.ts:42-85

DELETE /api/document

Delete document versions created after a specific timestamp.

Authentication

Requires authenticated session. Returns 401 if not authenticated.

Query parameters

id
string
required
The document ID
timestamp
string
required
ISO 8601 timestamp. Versions created after this time will be deleted.

Request example

DELETE /api/document?id=550e8400-e29b-41d4-a716-446655440000&timestamp=2024-01-15T10:35:00Z

Response

Returns array of deleted document versions.
[
  {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "title": "React Component",
    "content": "...",
    "kind": "code",
    "userId": "123e4567-e89b-12d3-a456-426614174000",
    "createdAt": "2024-01-15T10:40:00Z"
  }
]

Error responses

400
Bad Request
Missing required parameters (id or timestamp)
401
Unauthorized
Not authenticated
403
Forbidden
Document belongs to different user

Cascade behavior

Deleting documents also removes associated suggestions created after the timestamp. From app/(chat)/api/document/route.ts:87-126
Use this endpoint to implement “undo” functionality by deleting versions created after a user’s last accepted edit.

Build docs developers (and LLMs) love