# Quickstart

Make your first call in five minutes: create a token, read your projects, write a note into a project's memory and read it back.

You need an internal member account in a Youniton workspace with the right "Memory write" on one project. Client guests cannot create tokens.

1. **Create a personal access token**

   In Youniton, open **Account › Sessions & apps › API tokens** and select **Create token**. Youniton asks you to confirm who you are first.

   - Give the token a name, for example *Quickstart*.
   - Select the abilities `projects:read`, `memory:read` and `memory:write`.
   - Select an expiry: 30, 90 or 365 days, never above your workspace's maximum.

   Youniton shows the secret **one time**. Copy it now. It starts with `kanso_pat_`.

   > **Keep the secret safe**
   >
   > Never put a token in a document, a log, a commit or a chat. Keep it in an environment variable or a secret store.

2. **Keep the token in an environment variable**

   *Terminal*

   ```bash
   export YOUNITON_TOKEN="kanso_pat_…"   # the secret from step 1
   export YOUNITON_API="https://app.youniton.example/api/v1"   # your workspace's app host + /api/v1
   ```

3. **Make your first call**

   Read the projects that you can see. The token names its tenant, so you do not send a tenant header.

   *GET /projects*

   **curl**

   ```bash
   curl -s "$YOUNITON_API/projects" \
     -H "Authorization: Bearer $YOUNITON_TOKEN" \
     -H "Accept: application/json"
   ```

   **JavaScript**

   ```js
   const res = await fetch(`${process.env.YOUNITON_API}/projects`, {
     headers: { Authorization: `Bearer ${process.env.YOUNITON_TOKEN}`, Accept: "application/json" },
   });
   const { data, meta } = await res.json();
   console.log(data.map((p) => p.key), meta.page);
   ```

   **Python**

   ```python
   import os, requests

   res = requests.get(
       f"{os.environ['YOUNITON_API']}/projects",
       headers={"Authorization": f"Bearer {os.environ['YOUNITON_TOKEN']}", "Accept": "application/json"},
   )
   res.raise_for_status()
   print([p["key"] for p in res.json()["data"]])
   ```

   **PHP**

   ```php
   <?php
   $client = new GuzzleHttp\Client(['base_uri' => getenv('YOUNITON_API') . '/']);
   $res = $client->get('projects', ['headers' => [
       'Authorization' => 'Bearer ' . getenv('YOUNITON_TOKEN'),
       'Accept' => 'application/json',
   ]]);
   $projects = json_decode((string) $res->getBody(), true)['data'];
   ```

   The answer is one page of projects in `data`, with the page totals in `meta`. Note the `key` of a project, for example `ORB`. Reference: [`GET /projects`](https://docs.youniton.com/api/reference/projects.md#listProjects)

4. **Write a note into the project's memory**

   Write a Markdown entry by its path. The first call creates it (`201`), with the missing directories. Youniton adds `.md` to the name.

   *PUT /projects/{projectKey}/memory/entries/by-path*

   **curl**

   ```bash
   curl -s -X PUT "$YOUNITON_API/projects/ORB/memory/entries/by-path?path=quickstart/hello" \
     -H "Authorization: Bearer $YOUNITON_TOKEN" \
     -H "Content-Type: application/json" \
     --data '{"body": "# Hello\n\nMy first entry from the API.", "source": {"kind": "agent", "name": "Quickstart script"}}'
   ```

   **JavaScript**

   ```js
   const url = new URL(`${process.env.YOUNITON_API}/projects/ORB/memory/entries/by-path`);
   url.searchParams.set("path", "quickstart/hello");
   const res = await fetch(url, {
     method: "PUT",
     headers: { Authorization: `Bearer ${process.env.YOUNITON_TOKEN}`, "Content-Type": "application/json" },
     body: JSON.stringify({ body: "# Hello\n\nMy first entry from the API.", source: { kind: "agent", name: "Quickstart script" } }),
   });
   const { data, meta } = await res.json();
   console.log(res.status, data.path, meta.outcome); // 201 "quickstart/hello.md" "created"
   ```

   **Python**

   ```python
   res = requests.put(
       f"{os.environ['YOUNITON_API']}/projects/ORB/memory/entries/by-path",
       params={"path": "quickstart/hello"},
       headers={"Authorization": f"Bearer {os.environ['YOUNITON_TOKEN']}"},
       json={"body": "# Hello\n\nMy first entry from the API.", "source": {"kind": "agent", "name": "Quickstart script"}},
   )
   print(res.status_code, res.json()["meta"]["outcome"])
   ```

   **PHP**

   ```php
   $res = $client->put('projects/ORB/memory/entries/by-path', [
       'query' => ['path' => 'quickstart/hello'],
       'headers' => ['Authorization' => 'Bearer ' . getenv('YOUNITON_TOKEN')],
       'json' => ['body' => "# Hello\n\nMy first entry from the API.", 'source' => ['kind' => 'agent', 'name' => 'Quickstart script']],
   ]);
   $outcome = json_decode((string) $res->getBody(), true)['meta']['outcome'];
   ```

   Send the same call again: the answer is `200` with `meta.outcome` `unchanged`. A new body gives `updated`. Reference: [`PUT /projects/{projectKey}/memory/entries/by-path`](https://docs.youniton.com/api/reference/memory.md#upsertMemoryEntryByPath)

5. **Read it back**

   Read the entry by its path. This proves that the write is saved — never trust a "saved" message alone.

   *GET /projects/{projectKey}/memory/entries/by-path*

   ```bash
   curl -s "$YOUNITON_API/projects/ORB/memory/entries/by-path?path=quickstart/hello.md" \
     -H "Authorization: Bearer $YOUNITON_TOKEN"
   ```

   The answer holds the body, the path, the source and the `version`. The `ETag` header carries the same version for [If-Match](https://docs.youniton.com/api/versioning.md). Reference: [`GET /projects/{projectKey}/memory/entries/by-path`](https://docs.youniton.com/api/reference/memory.md#getMemoryEntryByPath)

## Next steps

- [Authentication](https://docs.youniton.com/api/authentication.md): Abilities, expiry, rotation and what a revoked token answers.
- [Errors](https://docs.youniton.com/api/errors.md): The problem shape and the codes to branch on.
- [Help-desk requests](https://docs.youniton.com/api/guides/help-desk-requests.md): Connect a client's own system to a project's help desk.
- [API reference](https://docs.youniton.com/api/reference/index.md): Every operation, its parameters, schemas and examples.
