> For the complete documentation index, see [llms.txt](https://docs.graphlit.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.graphlit.dev/api-guides/use-cases/conversations/conversation-message-history.md).

# Get Message History

## User Intent

"I want to retrieve the complete message history from a conversation"

## Operation

* **SDK Method**: `graphlit.getConversation()`
* **GraphQL**: `getConversation` query
* **Entity Type**: Conversation
* **Common Use Cases**: View chat history, export conversations, display message thread

## TypeScript (Canonical)

```typescript
import { Graphlit } from 'graphlit-client';
import { Types } from 'graphlit-client/dist/generated/graphql-types';

const graphlit = new Graphlit();

const conversationId = 'conversation-id-here';

// Get conversation with full message history
const conversation = await graphlit.getConversation(conversationId);

console.log(`\nConversation: ${conversation.conversation.name}`);
console.log(`Total messages: ${conversation.conversation.messages?.length || 0}\n`);

// Display message history
conversation.conversation.messages?.forEach((msg, index) => {
  console.log(`${index + 1}. [${msg.role}] ${msg.timestamp}`);
  console.log(msg.message);
  
  // Show citations if available
  if (msg.citations && msg.citations.length > 0) {
    console.log(`\nCitations:`);
    msg.citations.forEach((citation, citIndex) => {
      console.log(`  [${citIndex + 1}] ${citation.content?.name}`);
    });
  }
  
  console.log('---\n');
});
```

## Parameters

* **`id`** (string): Conversation ID

## Response

```typescript
{
  conversation: {
    id: string;
    name: string;
    messages: Message[];
  }
}

interface Message {
  role: MessageRole;  // USER, ASSISTANT, SYSTEM
  message: string;
  timestamp: Date;
  citations?: Citation[];
}
```

## Developer Hints

### Filter by Role

```typescript
const conversation = await graphlit.getConversation(conversationId);

// Get only user messages
const userMessages = conversation.conversation.messages?.filter(
  m => m.role === MessageRole.User
);

// Get only assistant responses
const assistantMessages = conversation.conversation.messages?.filter(
  m => m.role === MessageRole.Assistant
);

console.log(`User messages: ${userMessages?.length || 0}`);
console.log(`Assistant responses: ${assistantMessages?.length || 0}`);
```

### Export Conversation

```typescript
async function exportConversation(conversationId: string) {
  const conv = await graphlit.getConversation(conversationId);
  
  const exported = {
    name: conv.conversation.name,
    created: conv.conversation.creationDate,
    messageCount: conv.conversation.messages?.length || 0,
    messages: conv.conversation.messages?.map(msg => ({
      role: msg.role,
      timestamp: msg.timestamp,
      message: msg.message,
      citations: msg.citations?.map(c => c.content?.name)
    }))
  };
  
  return JSON.stringify(exported, null, 2);
}

const json = await exportConversation(conversationId);
console.log(json);
```
