# Connect a coding agent / CLI

Let a coding agent or a command-line script read your tickets, comment on them and keep its notes in the project's memory — with a personal token.

> **Two ways to connect an agent**
>
> This guide uses a **personal access token**: the agent acts as you, narrowed by the token's abilities. An agent can also have its own identity, with a `kanso_agt_` token on the agent API and the MCP server. Read [Coding agents & MCP](https://docs.youniton.com/api/tools/coding-agents-mcp.md).

1. **Create a token for the agent**

   In **Account › Sessions & apps › API tokens**, create a token named after the agent. Select only what it needs, for example:

   - `projects:read` and `tickets:read` — read projects, tickets, boards and backlogs;
   - `comments:write` — post a comment on a ticket;
   - `memory:read` and `memory:write` — keep notes in the project's memory.

   Select a short expiry. Give each agent its own token, so that you can revoke one without the others.

2. **Give the agent the token**

   *Shell profile or the agent's secret store*

   ```bash
   export YOUNITON_API="https://app.youniton.example/api/v1"
   export YOUNITON_TOKEN="kanso_pat_…"    # never commit this value
   ```

3. **Paste the setup prompt**

   **Prompt for your coding agent: Set up Youniton in your agent**

   ```text
   You can use the Youniton REST API. Follow these rules.

   Connection
   - Base URL: $YOUNITON_API (for example https://app.youniton.example/api/v1).
   - Send "Authorization: Bearer $YOUNITON_TOKEN" and "Accept: application/json" on every call.
   - Never print, log or commit the token.
   - Responses are JSON: {data, meta, links}. Errors are application/problem+json; branch on "code".
   - On 429, wait for the Retry-After seconds. On 412 stale-version, read the record again before you write.

   What you may do
   - GET /projects — find the project key (for example ORB).
   - GET /projects/{projectKey}/tickets and GET /tickets/{ticketKey} — read the work.
   - GET /tickets/{ticketKey}/comments — read the discussion.
   - POST /tickets/{ticketKey}/comments with an Idempotency-Key header (a new UUID per comment) — report progress.
   - PUT /projects/{projectKey}/memory/entries/by-path?path=agents/<your-name>/<topic> with
     {"body": "<Markdown>", "source": {"kind": "agent", "name": "<your-name>"}} — keep your notes.
     The same body again changes nothing; read meta.outcome.
   - GET /projects/{projectKey}/memory/entries?q=<words> — find notes before you start a task.

   Rules
   - Only do what the user asked. Do not change tickets unless the user asks you to.
   - After each write, read the record back to confirm it.
   ```

4. **Try it**

   *A smoke test*

   ```bash
   curl -s "$YOUNITON_API/projects" -H "Authorization: Bearer $YOUNITON_TOKEN" | head -c 400
   ```

   A `401` means that the token is wrong, expired or revoked. A `404 route.not_found` on a call means that a personal token cannot call that operation. A `403 access.forbidden` means that you can see the record but your role may not do the action.

## The operations the prompt uses

- [`GET /projects`](https://docs.youniton.com/api/reference/projects.md#listProjects)The project catalogue — the member's visible projects, filtered, segmented and paged with totals
- [`GET /projects/{projectKey}/tickets`](https://docs.youniton.com/api/reference/work.md#listProjectTickets)The generic ticket list of a project — filters, q, whitelisted sort, cursor (the Calendar uses the due-date range)
- [`GET /tickets/{ticketKey}`](https://docs.youniton.com/api/reference/work.md#getTicket)The ticket through the caller's mask, with can (the side panel's document)
- [`GET /tickets/{ticketKey}/comments`](https://docs.youniton.com/api/reference/work.md#listTicketComments)Comments of both threads (filter\[thread\]) oldest first
- [`POST /tickets/{ticketKey}/comments`](https://docs.youniton.com/api/reference/work.md#postTicketComment)Post a comment in the internal or the client thread, with files that follow the thread
- [`PUT /projects/{projectKey}/memory/entries/by-path`](https://docs.youniton.com/api/reference/memory.md#upsertMemoryEntryByPath)Create the entry at the path, or replace its body (for automations and agents)
- [`GET /projects/{projectKey}/memory/entries`](https://docs.youniton.com/api/reference/memory.md#listMemoryEntries)List or search the project's memory entries

## A small CLI

*youniton-note*

**Bash**

```bash
#!/usr/bin/env bash
# youniton-note <projectKey> <path> < notes.md  — write stdin as a memory entry
set -euo pipefail
body=$(jq -Rs . )
curl -sf -X PUT "$YOUNITON_API/projects/$1/memory/entries/by-path?path=$(jq -rn --arg p "$2" '$p|@uri')" \
  -H "Authorization: Bearer $YOUNITON_TOKEN" -H "Content-Type: application/json" \
  --data "{\"body\": $body, \"source\": {\"kind\": \"agent\", \"name\": \"youniton-note CLI\"}}" \
  | jq -r '.meta.outcome + " " + .data.path'
```

**Node**

```js
#!/usr/bin/env node
// youniton-note <projectKey> <path> < notes.md
const [projectKey, path] = process.argv.slice(2);
const body = await new Response(process.stdin).text();
const url = new URL(`${process.env.YOUNITON_API}/projects/${projectKey}/memory/entries/by-path`);
url.searchParams.set("path", path);
const res = await fetch(url, {
  method: "PUT",
  headers: { Authorization: `Bearer ${process.env.YOUNITON_TOKEN}`, "Content-Type": "application/json" },
  body: JSON.stringify({ body, source: { kind: "agent", name: "youniton-note CLI" } }),
});
const json = await res.json();
if (!res.ok) { console.error(json.code); process.exit(1); }
console.log(json.meta.outcome, json.data.path);
```
