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

# Chat with Agent

> Send a message to an agent using API key authentication.

<Note>
  **Authentication**: This endpoint accepts either key type:

  * **Agent API key** (`ak_*`) — scoped to a single agent. Recommended for embedded widgets and other client-side use where a leaked key should not expose your full account. Generate from the agent's settings.
  * **Platform API key** (`thns_sk_*`) — full account access. Use from trusted server-side code. Generate from **Dashboard → Settings → API Keys**.

  In both cases, pass the key as a Bearer token: `Authorization: Bearer <key>`.
</Note>

For external integrations, use the V1 agent chat endpoint with either an Agent API Key or a Platform API Key. Create keys from the dashboard.

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.thinnest.ai/v1/agents/{agent_id}/chat \
    -H "Authorization: Bearer $THINNESTAI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "message": "What are your business hours?",
    "session_id": "sess_customer_jane_001"
  }'
  ```

  ```python Python theme={null}
  import requests

  url = "https://api.thinnest.ai/v1/agents/{agent_id}/chat"
  headers = {
      "Authorization": "Bearer " + "YOUR_THINNESTAI_API_KEY",
      "Content-Type": "application/json",
  }

  payload = {
    "message": "What are your business hours?",
    "session_id": "sess_customer_jane_001"
  }

  response = requests.post(url, headers=headers, json=payload)
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.thinnest.ai/v1/agents/{agent_id}/chat", {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + "YOUR_THINNESTAI_API_KEY",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
    "message": "What are your business hours?",
    "session_id": "sess_customer_jane_001"
  }),
  });

  const data = await response.json();
  console.log(data);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "fmt"
      "io"
      "net/http"
  )

  func main() {
      payload := []byte(`{
    "message": "What are your business hours?",
    "session_id": "sess_customer_jane_001"
  }`)
      req, _ := http.NewRequest("POST", "https://api.thinnest.ai/v1/agents/{agent_id}/chat", bytes.NewBuffer(payload))
      req.Header.Set("Authorization", "Bearer YOUR_THINNESTAI_API_KEY")
      req.Header.Set("Content-Type", "application/json")

      resp, err := http.DefaultClient.Do(req)
      if err != nil { panic(err) }
      defer resp.Body.Close()
      body, _ := io.ReadAll(resp.Body)
      fmt.Println(string(body))
  }
  ```
</RequestExample>

***

## Path Parameters

<ParamField path="agent_id" type="string" required>
  Target agent's public ID (ag\_\*)
</ParamField>

***

## Request Body

<ParamField body="message" type="string" required>
  User message text
</ParamField>

<ParamField body="session_id" type="string">
  Session ID for conversation memory. Reuse the same ID to continue a conversation
</ParamField>

### Session Behavior

* **With `session_id`**: The agent remembers previous messages in this session. Use for multi-turn conversations.
* **Without `session_id`**: A new session is created automatically. Each request is treated as independent.

***

## Response `200`

<ResponseExample>
  ```json 200 theme={null}
  {
    "response": "Our business hours are Monday through Friday, 9 AM to 6 PM EST. We're also available on Saturdays from 10 AM to 2 PM.",
    "session_id": "sess_customer_jane_001",
    "usage": {
      "input_tokens": 45,
      "output_tokens": 32,
      "total_tokens": 77
    }
  }
  ```
</ResponseExample>

### Response Fields

<ResponseField name="response" type="string">
  Agent's text response
</ResponseField>

<ResponseField name="session_id" type="string">
  Session ID (reuse for follow-up messages)
</ResponseField>

<ResponseField name="usage.input_tokens" type="integer">
  Tokens in the prompt
</ResponseField>

<ResponseField name="usage.output_tokens" type="integer">
  Tokens in the response
</ResponseField>

<ResponseField name="usage.total_tokens" type="integer">
  Total tokens consumed
</ResponseField>

***

## Example — Multi-Turn Conversation

**Turn 1:**

```json theme={null}
// Request
{ "message": "What products do you sell?", "session_id": "sess_001" }

// Response
{ "response": "We sell three main product lines: ...", "session_id": "sess_001" }
```

**Turn 2:**

```json theme={null}
// Request (same session_id continues the conversation)
{ "message": "How much does the premium plan cost?", "session_id": "sess_001" }

// Response (agent remembers context from Turn 1)
{ "response": "The premium plan for our Enterprise product is $99/month...", "session_id": "sess_001" }
```

***

## Errors

| Code  | Description                |
| ----- | -------------------------- |
| `401` | Missing or invalid API key |
| `402` | Insufficient balance       |
| `404` | Agent not found            |
| `429` | Rate limit exceeded        |
