# Try an API and preview

## Try a public API

This complete page uses [GitHub’s Get a repository endpoint](https://docs.github.com/en/rest/repos/repos#get-a-repository) to show stars and forks for a public repository. It needs no API key.

1. Open Legato. Each time it starts, it installs the page schema in its Pages folder for editor suggestions.
2. [Download github-starter.json](https://legatobar.com/examples/github-starter.json), or copy the complete JSON below. Save it as `~/Library/Application Support/Legato/Pages/github-starter.json`, using plain text with the `.json` extension. Or, from Terminal: `/Applications/Legato.app/Contents/Helpers/legato set github-starter github-starter.json`.
3. Within a second, Legato finds the file and the **GitHub** page appears. The first fetch fills both cards.

```json
{
  "$schema": "./.schemas/page.schema.json",
  "title": "GitHub",
  "sources": {
    "repo": {
      "url": "https://api.github.com/repos/octocat/Hello-World",
      "auth": { "type": "none" },
      "headers": {
        "Accept": "application/vnd.github+json",
        "X-GitHub-Api-Version": "2026-03-10"
      },
      "every": "1h"
    }
  },
  "cards": [
    { "id": "stars", "type": "stat", "label": "Stars",
      "source": { "ref": "repo",
                  "map": { "value": "stargazers_count" } } },
    { "id": "forks", "type": "stat", "label": "Forks",
      "source": { "ref": "repo",
                  "map": { "value": "forks_count" } } }
  ]
}
```

The page then makes **one request per hour while your Mac is awake and unlocked**. Both cards use the same answer: `sources.repo` defines the connection, `ref` selects it, and each `map` takes a different response field. After sleep or unlock, a due source refreshes; opening the panel also checks sources that are due.

Change `octocat/Hello-World` to another public `owner/repository`, or follow the next section to connect a different API. [GitHub](https://legatobar.com/services/github) has the full pages, with a token. Save the file again and the page changes within a second. Right-click a page and choose **Show in Finder** to find its file, or **Delete Page…** to move the file to the Trash (put it back from there to restore it).

## Edit with suggestions

Start from a JSON page and let the editor guide the fields. Open the page in VS Code or another editor with JSON Schema support. The starter's `$schema` line points to `./.schemas/page.schema.json`, which Legato installs in the `.schemas` directory inside Pages. It enables property suggestions, hover descriptions and structural validation without needing an internet connection.

Keep that line when adapting an example. The schema covers sources, authentication, mappings, buttons and card fields. It can catch an invalid field or missing property; it cannot know an API's real response or prove that a mapping selects the right data. Use the preview below to check source references, mappings and rendered cards.

Both the schema and the app reject unknown top-level page keys, including `widgets` and `light`. They also reject unknown keys in sources, authentication, requests, conditions and buttons. The app still ignores unknown options inside mapping rules, such as `defualt`; the schema catches those too.

Keep the `$schema` line to catch typos while editing. If you keep pages elsewhere, [download the schema](https://legatobar.com/schemas/page.schema.json) and place it at the same relative path, or adjust `$schema` to its location.

## Preview before polling

Before a page polls, check its cards in the **Source Preview** window, with a sample response or one real fetch. Right-click a page in the panel's sidebar and choose **Preview Source…** to open it on that page's file.

For a file that isn't in the Pages folder yet, right-click an empty part of the sidebar, choose **Source Preview…**, then **Choose page…**.

Then select a named **Source**. Loading a file makes an in-memory preview; it does not change the file or add it to your active pages.

Paste a **Sample response**, or use **Import…** to load a UTF-8 response file, then click **Preview sample**. For the GitHub starter, this illustrative sample is enough:

```json
{ "stargazers_count": 100, "forks_count": 12 }
```

The preview displays the actual cards, mapping issues, **Mapped card JSON**, and **Response and derived values**. Sample previews require an active trial or license. They do not contact an API or read source secrets. They help catch missing response fields or values that do not fit a card, beyond what editor validation can check.

For a direct request to the source’s `url`, paste its raw JSON, text or Prometheus answer. If the source uses named `requests`, even just one, paste a JSON object keyed by request name; encode text and Prometheus answers as JSON strings. Samples and imported pages must be no larger than 1 MB.

To try the real service, click **Fetch Once**; it works during the trial or with a [license](https://legatobar.com/docs/license.md). It uses the selected source and its saved Keychain secret through the same fetcher as normal polling, including shared rate-limit waits and conditional GETs. Preview cards do not run buttons. **Cancel** stops a pending preview fetch; changing the source or sample clears the old result.

An agent gets the same sample preview with the MCP tool [`preview_page`](https://legatobar.com/docs/mcp-tools.md#checking-a-pages-sources), or `legato preview` from a script, and sees each source's last fetch in `get_page`'s `status.sources`.

Edit configuration in your external editor, then click **Reload page**. Reloading clears the old output and keeps your selected source and sample when that source still exists. To use it in the menu bar, save it in the Pages folder. **Open Pages Folder** in the editor-support section opens that location and installs the bundled schema if needed.

## Adapt a request you already know

Start with a working API request and a sample response. For the starter above, the equivalent request is:

```sh
curl 'https://api.github.com/repos/octocat/Hello-World' \
  -H 'Accept: application/vnd.github+json' \
  -H 'X-GitHub-Api-Version: 2026-03-10'
```

A polling source should read data. Use POST polling only for read operations, such as a template request; put commands that change something in [Buttons](https://legatobar.com/docs/buttons.md#buttons), which run when clicked.

Translate the parts you need into a named source:

| Request part | Page setting |
| --- | --- |
| URL | `sources.<name>.url` |
| GET or POST | `method`; GET is the default |
| JSON sent with POST | `body`, as a JSON value, not a quoted curl command |
| Accept, API version, other non-secret headers | `headers`, an object of header names and string values |
| API token or password | `secret` names an item you save in **Settings → Secrets** (or with [`Legato secret set`](https://legatobar.com/docs/secrets.md#from-the-command-line-or-an-agent)); `auth` describes how to send it |
| Fields in the response | Card `source.map`, such as `"value": "stargazers_count"` |
| How often it should refresh | Source `every`; start conservatively, such as `1h` |

For a bearer token, set `"secret": "my-api"` and `"auth": { "type": "bearer" }`, then save `my-api` for the API's host in Settings → Secrets. Keep the credential itself out of the file. Use `auth: { "type": "none" }` for an unauthenticated endpoint.

Every request sends `Accept: application/json` and `User-Agent: Legato`, unless the source's headers set their own. Source `headers` apply to its data requests and buttons. A named request or button can override a common header; authentication fills the credential header afterward. A login's own headers belong in `auth.request.headers` and are separate from these common headers. See [Signing in](https://legatobar.com/docs/source-requests.md#signing-in) for API-key headers, Basic authentication and token logins.

Start with one card and one field. Add more cards referencing the same source after that works. The [card reference](https://legatobar.com/docs/cards.md) lists the fields each card accepts, and [Mapping](https://legatobar.com/docs/mapping.md#mapping) explains nested objects, arrays and small transformations.

