# Quick start for a developer

Call a machine tool from your own program: one HTTPS request per call, an agent token per program.

**Preview.** The `/api/v1` routes with agent tokens ship with the next server release. The TypeScript and Python SDKs are not published yet.

**In short:** `POST https://<contour>.cmdop.ai/api/v1/call` with `Authorization: Bearer $CMDOP_TOKEN` and
`{"machine": "<machine>", "tool": "sys_info", "args": {}}`.

## Before you start

- A machine online on your page, and an agent token for your program from the Agents tab, in the
  environment as `CMDOP_TOKEN`. Never put the token in a URL or in code you commit.

## Steps

1. **List the machines:**

   ```bash
   curl -sS "https://<contour>.cmdop.ai/api/v1/machines" \
     -H "Authorization: Bearer $CMDOP_TOKEN"
   ```

2. **Call a tool.** The answer is `{"ok": <result>}` or `{"error": {"code", "message"}}`:

   ```bash
   curl -sS -X POST "https://<contour>.cmdop.ai/api/v1/call" \
     -H "Authorization: Bearer $CMDOP_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"machine":"<machine>","tool":"sys_info","args":{}}'
   ```

3. **From TypeScript**, the same call with `fetch`:

   ```ts
   const res = await fetch(`https://${contour}.cmdop.ai/api/v1/call`, {
     method: "POST",
     headers: { Authorization: `Bearer ${process.env.CMDOP_TOKEN}`, "Content-Type": "application/json" },
     body: JSON.stringify({ machine, tool: "sys_info", args: {} }),
   });
   const answer = (await res.json()) as { ok?: unknown; error?: { code: string; message: string } };
   if (answer.error) throw new Error(`${answer.error.code}: ${answer.error.message}`);
   ```

4. **Stream a command's output** with [`POST /api/v1/call/stream`](https://docs.cmdop.ai/reference/http/post-call-stream.md):
   one JSON line per piece of output, then the answer as the last line.

## Check

Step 2 answers `{"ok": {...}}` with the machine's operating system and user. On an error, act on
`error.code`: [Errors](https://docs.cmdop.ai/reference/errors.md).

## Next

- Every route: [HTTP API](https://docs.cmdop.ai/reference/http.md). Every tool: [Tools](https://docs.cmdop.ai/reference/tools.md).
- Using CMDOP inside your product: [embed CMDOP](https://docs.cmdop.ai/guides/embed.md).
