> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thig.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat & PRD Generation

> AI conversation and PRD document generation endpoints

# Chat & PRD Generation API

## General AI Chat

```http theme={null}
POST /api/chat
```

Conversational AI endpoint that resolves the best available provider (BYOK → org key → platform key).

**Body:**

| Field       | Type   | Required | Description                                                 |
| ----------- | ------ | -------- | ----------------------------------------------------------- |
| `messages`  | array  | Yes      | Array of `{role, content}` message objects                  |
| `provider`  | string | No       | Force a specific provider (`openai`, `anthropic`, `gemini`) |
| `projectId` | string | No       | Associate with a project for context                        |

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://app.thig.ai/api/chat \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "messages": [
        {"role": "user", "content": "What sections should a good PRD include?"}
      ],
      "provider": "anthropic"
    }'
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch('https://app.thig.ai/api/chat', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      messages: [
        { role: 'user', content: 'What sections should a good PRD include?' }
      ],
      provider: 'anthropic'
    })
  });
  const data = await res.json();
  ```
</CodeGroup>

**Response:**

```json theme={null}
{
  "role": "assistant",
  "content": "Based on your requirements, I'd recommend..."
}
```

## Project Chat

```http theme={null}
POST /api/projects/{projectId}/chat
```

Non-streaming chat within a project context. Saves QA pairs to the project.

**Body:**

| Field                 | Type   | Required | Description                   |
| --------------------- | ------ | -------- | ----------------------------- |
| `message`             | string | Yes      | User message                  |
| `conversationHistory` | array  | No       | Previous messages for context |

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://app.thig.ai/api/projects/proj_123/chat \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"message": "The app should support offline mode and push notifications"}'
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch('https://app.thig.ai/api/projects/proj_123/chat', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      message: 'The app should support offline mode and push notifications'
    })
  });
  const data = await res.json();
  ```
</CodeGroup>

**Response:**

```json theme={null}
{
  "response": "Great, let me ask about your target users...",
  "qaPairId": "qa_123"
}
```

## Streaming Conversation

```http theme={null}
POST /api/projects/{projectId}/conversation/stream
```

SSE streaming endpoint for the conversational PRD builder.

**Body:** Same as Project Chat.

**SSE Events:**

```
data: {"type": "progress", "phase": "Analyzing your requirements...", "percent": 15}
data: {"type": "content", "content": "Based on what you've described..."}
data: {"type": "done", "fullContent": "Complete response text..."}
```

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://app.thig.ai/api/projects/proj_123/conversation/stream \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Accept: text/event-stream" \
    -d '{"message": "Users need to collaborate in real-time"}'
  ```

  ```javascript JavaScript (EventSource) theme={null}
  // Note: EventSource only supports GET; use fetch for POST SSE streams
  const res = await fetch('https://app.thig.ai/api/projects/proj_123/conversation/stream', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ message: 'Users need to collaborate in real-time' })
  });

  const reader = res.body.getReader();
  const decoder = new TextDecoder();

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    const chunk = decoder.decode(value);
    const lines = chunk.split('\n').filter(l => l.startsWith('data: '));
    for (const line of lines) {
      const data = JSON.parse(line.slice(6));
      if (data.type === 'content') process.stdout.write(data.content);
      if (data.type === 'done') console.log('\n--- Complete ---');
    }
  }
  ```
</CodeGroup>

## Generate PRD (Non-Streaming)

```http theme={null}
POST /api/generate-prd
```

Generate a complete PRD from the project's QA pairs.

**Body:**

| Field        | Type    | Required | Description                                                 |
| ------------ | ------- | -------- | ----------------------------------------------------------- |
| `projectId`  | string  | Yes      | Project to generate PRD for                                 |
| `style`      | string  | No       | PRD style (comprehensive, technical, business, lean, agile) |
| `regenerate` | boolean | No       | Force regeneration                                          |

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://app.thig.ai/api/generate-prd \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"projectId": "proj_123", "style": "comprehensive"}'
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch('https://app.thig.ai/api/generate-prd', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ projectId: 'proj_123', style: 'comprehensive' })
  });
  const data = await res.json();
  ```
</CodeGroup>

## Generate PRD (Streaming)

```http theme={null}
POST /api/generate-prd/stream
```

SSE streaming PRD generation. Same body as non-streaming.

**SSE Events:**

```
data: {"type": "progress", "phase": "Generating Executive Summary...", "percent": 10}
data: {"type": "content", "content": "# Executive Summary\n\n..."}
data: {"type": "done", "fullContent": "Complete PRD content..."}
data: {"type": "error", "message": "Generation failed", "code": "GENERATION_ERROR"}
```

<CodeGroup>
  ```bash curl theme={null}
  curl -N -X POST https://app.thig.ai/api/generate-prd/stream \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "Accept: text/event-stream" \
    -d '{"projectId": "proj_123", "style": "technical"}'
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch('https://app.thig.ai/api/generate-prd/stream', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ projectId: 'proj_123', style: 'technical' })
  });

  const reader = res.body.getReader();
  const decoder = new TextDecoder();
  let fullContent = '';

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    const chunk = decoder.decode(value);
    const lines = chunk.split('\n').filter(l => l.startsWith('data: '));
    for (const line of lines) {
      const data = JSON.parse(line.slice(6));
      if (data.type === 'progress') console.log(`${data.percent}% — ${data.phase}`);
      if (data.type === 'content') fullContent += data.content;
      if (data.type === 'done') console.log('PRD generated:', data.fullContent.length, 'chars');
    }
  }
  ```
</CodeGroup>

## Chat Export

```http theme={null}
GET /api/projects/{projectId}/chat-export
```

Export the conversation history.

**Query Parameters:**

| Parameter | Type   | Description             |
| --------- | ------ | ----------------------- |
| `format`  | string | `pdf`, `csv`, or `json` |

<CodeGroup>
  ```bash curl theme={null}
  curl -G https://app.thig.ai/api/projects/proj_123/chat-export \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -d "format=json" \
    -o chat-history.json
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch('https://app.thig.ai/api/projects/proj_123/chat-export?format=json', {
    headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
  });
  const blob = await res.blob();
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'chat-history.json';
  a.click();
  ```
</CodeGroup>

Returns the file directly as a download.
