> ## Documentation Index
>
> Fetch the complete documentation index at: https://www.cosmicjs.com/llms.txt
> Use this file to discover all available pages before exploring further.
>
> For a question these docs do not answer, such as which plan fits a team, read https://www.cosmicjs.com/agent.txt and ask Cosmic's concierge.
>
> Machine-readable API spec: https://www.cosmicjs.com/openapi.json

# Agent messages

Send messages to an agent you already created, and read its conversations. This is the HTTP channel shown on the agent detail page under API tools.

To provision a new project for an agent that does not yet have a Cosmic account, see [Agent signup](/docs/api/agents). To configure channels, capabilities, and Slack, see [Agents](/docs/dashboard/ai/agents).

### Base URL
```
https://dapi.cosmicjs.com

```
These endpoints live on the dashboard API, not the bucket API. Authenticate `POST /messages` and the conversation reads with a [Personal Access Token](/docs/api/authentication#personal-access-tokens) (`cos_` prefix) or a session JWT. The inbound webhook uses a per-agent secret instead of a Bearer token.

---

## Send a message (`POST` `/v3/ai/agents/:agentId/messages`)

After you create an agent, the endpoint URL is shown on the agent detail page. Create a token at [Account Settings > API Tokens](https://app.cosmicjs.com/account/api-tokens).

### Required body

- **`message`** (`string`): The message text.

### Optional body

- **`conversation_id`** (`string`): Continue an existing conversation. Omit to start a new one.

- **`channel_metadata`** (`object`): Additional context passed to the agent.

- **`model`** (`string`): Override the agent's default AI model.

- **`context`** (`object`): Extra context for this message.

- **`stream`** (`boolean`): Set to `true` to receive a streaming SSE response instead of JSON. See [Streaming](#streaming).
```bash
curl -X POST https://dapi.cosmicjs.com/v3/ai/agents/AGENT_ID/messages \
  -H "Authorization: Bearer cos_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message": "Summarize our latest blog posts"}'

```
```js
const res = await fetch(
  'https://dapi.cosmicjs.com/v3/ai/agents/AGENT_ID/messages',
  {
    method: 'POST',
    headers: {
      Authorization: 'Bearer cos_YOUR_TOKEN',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      message: 'Summarize our latest blog posts',
    }),
  },
);
const data = await res.json();
```

If the agent is still working on the previous message in that conversation, the request returns `202` with `status: "queued"` and the new message is processed next.

---

## Streaming

Pass `stream: true` in the request body to receive a streaming response via [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) (`Content-Type: text/event-stream`). Each event is a `data:` line containing JSON.

### Event types

| Event type | Description |
|---|---|
| `info` | Sent immediately with `conversation_id` and `model` |
| `text_delta` | Incremental text chunk with `text` and accumulated `fullText` |
| `tool_progress` | Status label while the agent executes a tool |
| `done` | Final event with `conversation_id`, complete `message`, `tokens_used`, and `model` |
| `awaiting_approval` | Agent is waiting for approval, includes `pending_approval` details |
| `error` | An error occurred, includes `message` |

```bash
curl -N -X POST https://dapi.cosmicjs.com/v3/ai/agents/AGENT_ID/messages \
  -H "Authorization: Bearer cos_YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message": "Summarize our latest blog posts", "stream": true}'
```
```
data: {"type":"info","conversation_id":"abc123","model":"claude-opus-5-5"}

data: {"type":"text_delta","text":"Here","fullText":"Here"}

data: {"type":"tool_progress","label":"Fetching content..."}

data: {"type":"done","conversation_id":"abc123","message":"The latest posts cover...","tokens_used":1234,"model":"claude-opus-5-5"}
```

---

## Inbound webhook (`POST` `/v3/ai/agents/:agentId/webhook`)

Receive messages from external systems without a Personal Access Token. Enable the webhook channel on the agent. A webhook endpoint and secret are shown on the agent detail page. Use **Reveal** to view the secret and **Copy** to copy it.

If a webhook secret is compromised, click **Regenerate** on the agent detail page. The old secret stops working immediately.

Authenticate with one of these methods:

| Method | How to use |
|---|---|
| Query parameter | `?secret=YOUR_WEBHOOK_SECRET` |
| Header | `X-Webhook-Secret: YOUR_WEBHOOK_SECRET` |
| Header (alternate) | `X-Cosmic-Webhook-Secret: YOUR_WEBHOOK_SECRET` |
| HMAC signature | `X-Webhook-Signature: sha256=HMAC_HEX` (HMAC-SHA256 of the raw request body using the secret as the key) |

### Required body

- **`message`** (`string`): The message text.

### Optional body

- **`conversation_id`** (`string`): Continue an existing conversation.

- **`metadata`** (`object`): Additional context passed to the agent.

```bash
curl -X POST https://dapi.cosmicjs.com/v3/ai/agents/AGENT_ID/webhook?secret=YOUR_WEBHOOK_SECRET \
  -H "Content-Type: application/json" \
  -d '{"message": "New deployment completed successfully"}'

```
Use this for Zapier, GitHub Actions, or any automation that should not hold a Personal Access Token.

---

## List conversations (`GET` `/v3/ai/agents/:agentId/conversations`)

**Authentication**: Personal Access Token or session JWT.

Returns the agent's conversations. Conversations are scoped to the agent, not to a bucket.

---

## Get conversation history (`GET` `/v3/ai/agents/:agentId/conversations/:conversationId`)

**Authentication**: Personal Access Token or session JWT.

Returns one conversation and its messages. Optional query: `messagesLimit`, `messagesOffset`.

### Related

- [Agents](/docs/dashboard/ai/agents) - channels, capabilities, Slack, `/reset`, and plan limits.
- [Personal Access Tokens](/docs/api/authentication#personal-access-tokens) - `cos_` auth for `/messages` and conversation reads.

- [Agent signup](/docs/api/agents) - provision a project for an agent that does not yet have a Cosmic account.

## FAQ

### How much does Cosmic cost?

Project plans are Free ($0), Builder ($99/mo), Team ($299/mo), Business ($499/mo), Enterprise (custom pricing). Yearly billing saves 10%. Workspace plans are Small Workspace ($999/mo) and Large Workspace ($2,999/mo), and they start with a 30-day free trial. Allowances, add-on rates, and the rest of the pricing FAQ are plain text at https://www.cosmicjs.com/pricing.md.

### Are API keys required, and is content private?

Each Bucket has a read key and a write key in Bucket Settings > API Access. GET requests use the read key. POST, PATCH, and DELETE use the write key as Authorization: Bearer. Do not put the write key in a browser bundle, a mobile app, or a NEXT_PUBLIC_ variable. SAML single sign-on is on Large Workspace and Enterprise. The audit log is append-only. Text versions: https://www.cosmicjs.com/security.md and https://www.cosmicjs.com/docs/dashboard/audit-log.md.

### Can an agent create a Cosmic project without an existing account?

Yes. POST https://dapi.cosmicjs.com/v3/agents/sign-up provisions a free-tier project and bucket tied to a human email, with no existing Cosmic account. The human claims it with the emailed one-time code. The playbook is https://www.cosmicjs.com/docs/ai-onboarding.md.
