# Agents API

> Configure AI agents, knowledge bases and sources, web-chat links and custom fields, and chat with an agent over text.

Configure AI agents (personas), their knowledge bases, scored custom fields and
web-chat share links, and chat with an agent over text, all with an API key.
The same API is also reachable over
Connect/gRPC at `/pingx.agents.v1.AgentsService/<Method>`.

## Auth and scopes

Send `Authorization: Bearer pk_live_...` (create keys under Settings > API Keys in the app).
The organization always comes from the key; there is no organization field in
any request, and ids of another organization answer `404` exactly like unknown
ids.

| Key scope | Allows |
|---|---|
| `agents:read` | `Get*` / `List*` RPCs (all `GET` endpoints) |
| `agents:write` | everything, including reads |
| `agents` | same as `agents:write` |

Classification is by RPC name (`Get`, `List`, `Search`, `Count`, `Export`,
`Download` prefixes are reads). `GenerateSystemPrompt` and `SendChatMessage`
are writes on purpose: they spend AI quota / append conversation turns.

## Conventions

- JSON field names are camelCase over REST (`systemPrompt`); `int64` ids are JSON strings.
- Lists take `pageSize` (default 50, max 200) and `pageToken`, and return `nextPageToken` (empty = last page).
- Errors: `400` invalid argument / failed precondition, `401` bad key, `403` missing scope, `404` unknown id, `409` aborted (script changed concurrently), `503` assistant unavailable.
- `PATCH` endpoints are partial: only the fields you send change.

## Endpoints

| Method | Path | Scope | Purpose |
|---|---|---|---|
| GET | `/agents/v1/agents` | read | List agents |
| POST | `/agents/v1/agents` | write | Create agent (name, systemPrompt, knowledgeBaseIds) |
| GET | `/agents/v1/agents/{agentId}` | read | Get agent |
| PATCH | `/agents/v1/agents/{agentId}` | write | Update agent (prompt, playbooks, script, voice, toggles, knowledge bases) |
| DELETE | `/agents/v1/agents/{agentId}` | write | Delete agent |
| POST | `/agents/v1/system-prompts:generate` | write | Draft a system prompt from a brief (AI) |
| GET | `/agents/v1/knowledge-bases` | read | List knowledge bases |
| POST | `/agents/v1/knowledge-bases` | write | Create knowledge base |
| GET | `/agents/v1/knowledge-bases/{knowledgeBaseId}` | read | Get knowledge base with sources |
| PATCH | `/agents/v1/knowledge-bases/{knowledgeBaseId}` | write | Rename |
| DELETE | `/agents/v1/knowledge-bases/{knowledgeBaseId}` | write | Delete with its sources |
| GET | `/agents/v1/knowledge-bases/{knowledgeBaseId}/sources` | read | List sources |
| POST | `/agents/v1/knowledge-bases/{knowledgeBaseId}/sources` | write | Add a text / url / file / site source (async ingestion) |
| GET | `/agents/v1/knowledge-sources/{sourceId}` | read | Get source: **the ingestion poll** |
| PATCH | `/agents/v1/knowledge-sources/{sourceId}` | write | Edit a text source (re-ingests) |
| DELETE | `/agents/v1/knowledge-sources/{sourceId}` | write | Delete source |
| POST | `/agents/v1/knowledge-sources/{sourceId}:recrawl` | write | Re-crawl a site source |
| POST | `/agents/v1/agents/{agentId}/web-chat:enable` | write | Enable share link (idempotent) |
| POST | `/agents/v1/agents/{agentId}/web-chat:disable` | write | Revoke share link |
| GET | `/agents/v1/agents/{agentId}/web-chat` | read | Share-link state and token |
| GET | `/agents/v1/custom-fields` | read | List custom fields (`scopeType`, `agentId`) |
| POST | `/agents/v1/custom-fields` | write | Create custom field |
| PATCH | `/agents/v1/custom-fields/{customFieldId}` | write | Update label/description/range/position |
| DELETE | `/agents/v1/custom-fields/{customFieldId}` | write | Delete custom field |
| POST | `/agents/v1/chat/conversations` | write | Start a text conversation |
| POST | `/agents/v1/chat/conversations/{conversationId}/messages` | write | Send a message, get the full reply |

## Examples

```bash
export H='Authorization: Bearer pk_live_xxx'
export API=https://<gateway-host>

# Create a knowledge base, add a site, poll until ingestion finishes
curl -s -H "$H" -H 'Content-Type: application/json' -d '{"name":"Help center"}' $API/agents/v1/knowledge-bases
curl -s -H "$H" -H 'Content-Type: application/json' \
  -d '{"title":"Docs","site":{"url":"https://example.com/docs","maxPages":50}}' \
  $API/agents/v1/knowledge-bases/12/sources
curl -s -H "$H" $API/agents/v1/knowledge-sources/34      # repeat until "ingestionFinished": true
# status: pending | crawling | ready | partial | failed; pagesDiscovered/Ingested/Failed show crawl progress

# Upload a file source (data is base64, max 10 MB, .txt/.md/.pdf)
curl -s -H "$H" -H 'Content-Type: application/json' \
  -d "{\"file\":{\"filename\":\"faq.pdf\",\"data\":\"$(base64 -w0 faq.pdf)\"}}" \
  $API/agents/v1/knowledge-bases/12/sources

# Draft a prompt, create the agent with the knowledge base, tweak it
curl -s -H "$H" -H 'Content-Type: application/json' \
  -d '{"brief":"Friendly support agent for a bike shop"}' $API/agents/v1/system-prompts:generate
curl -s -H "$H" -H 'Content-Type: application/json' \
  -d '{"name":"Bike support","systemPrompt":"You are ...","knowledgeBaseIds":["12"]}' $API/agents/v1/agents
curl -s -X PATCH -H "$H" -H 'Content-Type: application/json' \
  -d '{"voiceGender":"male","fillerWordsEnabled":true,"knowledgeBaseIds":{"ids":["12"]}}' $API/agents/v1/agents/7

# Web chat share link
curl -s -X POST -H "$H" $API/agents/v1/agents/7/web-chat:enable
curl -s -X POST -H "$H" $API/agents/v1/agents/7/web-chat:disable

# Custom field scored per conversation
curl -s -H "$H" -H 'Content-Type: application/json' \
  -d '{"scopeType":"agent","agentId":"7","key":"nps","label":"NPS","description":"0-10 likelihood to recommend","minValue":0,"maxValue":10}' \
  $API/agents/v1/custom-fields

# Text chat: start, then send turns (the reply is returned whole)
curl -s -H "$H" -H 'Content-Type: application/json' -d '{"agentId":"7"}' $API/agents/v1/chat/conversations
curl -s -H "$H" -H 'Content-Type: application/json' -d '{"text":"What are your hours?"}' \
  $API/agents/v1/chat/conversations/99/messages
```

## Notes and exclusions

- **Updating `script`** runs an AI normalization pass first (up to a few minutes); the processed text is stored. `409` means another save changed the script meanwhile: re-read and retry.
- **`supportEscalationEnabled`** needs JIRA and Google Chat connected in the app; otherwise `400`. The integrations themselves (and their tokens) are not exposed.
- **Create** only takes name, system prompt and knowledge bases; set everything else with `PATCH`.
- **Chat** replies are assembled server-side. `segments` are the separate bubbles; `text` joins them. Conversations are recorded as app ("studio") chats.
- **Web chat token** is a bearer capability to chat publicly with the agent; treat it as sensitive. Re-enabling after disabling mints a new token.
- **Not in this API**: test calls and browser WebRTC calls (voice API), `TranscribeBrief` (needs browser-recorded audio upload; use `GenerateSystemPrompt` with a text brief), JIRA / Google Chat integrations.
- Agents here have no language or tools/flows setting: those do not exist on the platform's agent model today.

