> ## Documentation Index
> Fetch the complete documentation index at: https://docs.widgetic.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripts and CI

> Use the CLI from scripts and CI jobs, with JSON output, exit codes, retries, a way to wait for a build and a GitHub Actions recipe.

Every command is made to be run by a program as well as by a person. This page covers what a script needs to know: what a command prints, what its exit code means, what the CLI retries by itself and how to wait for a build.

## Output for programs

These flags work on every command:

| Flag | What it does |
| - | - |
| `--json` | Prints the result, and only the result, as one line of JSON on standard output. An error is one line of JSON on standard error. It never asks anything. |
| `--dry-run` | Prints the exact request, with the key left out, and sends nothing. It needs no key. |
| `--yes` | Goes ahead without asking. A command that deletes or changes something asks first at a terminal, and without a terminal it needs `--yes`. |
| `--timeout <seconds>` | Gives up on a call after this long. Default: 60. |
| `--profile <name>` | The saved key to use. See [Profiles](https://docs.widgetic.com/docs/cli/configuration.md#profiles). |
| `--data` or `--input` | The whole request as JSON, as `@file.json`, or as `-` for standard input. `--data` is for REST commands and `--input` for MCP commands. The flags of the command override its keys. |

With `--json`, a REST command prints the body of the response as the server sent it, and an MCP command prints the `structuredContent` of the tool's answer. An error looks like this:

```json theme={"theme":"css-variables"}
{"error":{"code":"WORKSPACE_LOCKED","message":"…","status":403,"requestId":"…"}}
```

`code` is the server's own code when it sent one (the [error codes](https://docs.widgetic.com/api-reference/errors.md) of the API) and the CLI's otherwise, such as `USAGE_ERROR`. `status`, `requestId`, `retryAfter`, `details` and `hint` are there when there is something to say. Quote the `requestId` to support.

## Exit codes

A script reads the exit code before it reads anything else.

| Code | Meaning | What to do |
| - | - | - |
| `0` | It worked. | |
| `1` | Anything else: a bug, an unexpected server error, or a build that did not finish in time. | Run `widgetic doctor`. |
| `2` | The command line or a JSON document is wrong. Nothing was sent. | Read `widgetic schema` for the command and fix the call. |
| `3` | Authentication or plan: no key, or a `401`, `402` or `403`. | Without a key, run `widgetic auth login` or set `WIDGETIC_API_KEY`. Otherwise the key or the plan does not allow the command. |
| `4` | Not found (`404` or `410`). | Check the id with a `get-` or `list-` command. |
| `5` | The server refused the request (`400`, `409`, `422` or another `4xx`), a tool answered with an error, or a build failed or was cancelled. | Read the message in the error. Do not repeat the call unchanged. |
| `6` | Rate limited or unavailable (`429`, `502`, `503`, `504`), a timeout, or no network. | Wait, then try once more. |
| `7` | This CLI is too old for the server (`426`). | Run `widgetic update`, or `npm install -g @widgetic/cli@latest`. |

A workspace without a plan is refused with `3` over REST and with `5` from an MCP tool: see [When the workspace has no plan](https://docs.widgetic.com/docs/cli/keys-and-workspaces.md#when-the-workspace-has-no-plan).

## Retries and idempotency

The CLI tries a call at most three times, waiting half a second and then a second and a half between tries. What it repeats depends on what the first try could have done.

* **A `429` is always waited out.** The server refused the call before it did anything. A `Retry-After` of up to 30 seconds is honoured. A longer one is reported at once, with exit code `6`.
* **A lost answer is repeated only when repeating cannot do anything twice.** A dropped connection, a timeout, a `502`, a `503` or a `504` may have been carried out and lost on the way, so the CLI repeats the call only when it reads, or when it carries an idempotency key. An MCP tool is repeated when it only reads or when its contract says that repeating it changes nothing more. A tool that starts a build is not repeated.
* **Everything else is returned at once.** A `400`, a `401` or a `404` would answer the same again.

A REST command that creates something takes `--idempotency-key`. The CLI makes a key when you leave it out and sends the same one on every try. Give your own, a UUID is ideal, to repeat the call by hand later, for instance after a script was cut off:

```bash theme={"theme":"css-variables"}
KEY=$(uuidgen)
widgetic api widgets create-widget --name "Launch countdown" --type countdown \
  --data '{"content":{}}' --idempotency-key "$KEY" --json

# Not sure it went through? Send the same call with the same key: it is carried out once.
widgetic api widgets create-widget --name "Launch countdown" --type countdown \
  --data '{"content":{}}' --idempotency-key "$KEY" --json
```

The API keeps the answer to a call for 24 hours, and a repeat with the same key gets that answer back. See [Idempotency](https://docs.widgetic.com/docs/reference/idempotency.md).

While the CLI waits to try again it says so on standard error, unless you passed `--json`.

## Wait for a build

`create-widget` and `revise-widget` answer at once with the widget's `widget_id` and the phase it is in, and the build goes on in the background. (`replicate-widget` builds nothing: it offers the copy the browser extension stored, so there is nothing to wait for.) Add `--wait` and the command keeps asking `get_widget` every 7 seconds until the build has ended, then prints the finished widget:

```bash theme={"theme":"css-variables"}
widgetic mcp create-widget \
  --title "Launch countdown" \
  --brief "A countdown to 14 March 2027" \
  --wait
```

| The build ends as | The command |
| - | - |
| `ready` | Exits with `0` and prints the widget. |
| `failed` or `cancelled` | Exits with `5`, with the code `BUILD_FAILED` and the widget in the error's `details`. |
| Still running after 15 minutes | Exits with `1`, with the code `WAIT_TIMED_OUT`. The build keeps going on the server: look again later with `widgetic mcp get-widget --widget-id <widget-id>`. |

Without `--json`, the CLI prints each new phase as `Widget <widget-id>: <phase>` on standard error.

A build that starts is charged a credit, so do not run a build command again just because it failed or timed out. Run `widgetic mcp list-widgets` first: the widget may exist already.

## GitHub Actions

This workflow builds a widget from a brief that you type in when you start it, publishes it and prints the embed code. The key comes from a secret, and the workflow holds every change to the workspace you mean.

```yaml theme={"theme":"css-variables"}
name: Build a widget
on:
  workflow_dispatch:
    inputs:
      brief:
        description: What the widget should do and show
        required: true

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      WIDGETIC_API_KEY: ${{ secrets.WIDGETIC_API_KEY }}
      WIDGETIC_WORKSPACE: Acme Studio
      BRIEF: ${{ inputs.brief }}
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: 22

      # Pin the version you tested.
      - run: npm install -g @widgetic/cli@0.1.17

      # Fails when the key is refused or a server does not answer.
      - run: widgetic doctor

      - name: Build and publish the widget
        run: |
          widgetic mcp create-widget \
            --title "Launch countdown" \
            --brief "$BRIEF" \
            --publish --wait --json > widget.json

          compositionId=$(jq -r '.composition_id // empty' widget.json)
          if [ -z "$compositionId" ]; then
            jq -r '.note // "The widget was built but not published."' widget.json >&2
            exit 1
          fi
          echo "COMPOSITION_ID=$compositionId" >> "$GITHUB_ENV"

      - name: Print the embed code
        run: widgetic mcp get-embed-code --composition-id "$COMPOSITION_ID" --format html --json | jq -r .code
```

* `WIDGETIC_API_KEY` is the **full access** key that `widgetic mcp` needs. Make it in the workspace you mean and keep it in the repository's secrets.
* `WIDGETIC_WORKSPACE` makes `create-widget` refuse the call, and change nothing, if the key works in another workspace. See [Which workspace a change goes to](https://docs.widgetic.com/docs/cli/keys-and-workspaces.md#which-workspace-a-change-goes-to).
* The brief goes in through an environment variable (`$BRIEF`) and not into the script text, so nothing a person types can run as a command.
* GitHub Actions sets `CI`, so the CLI never looks for a newer version or asks about one.
* A build that finishes but cannot publish, because the plan does not include publishing or because the publish did not finish, has no `composition_id`. The `note` says why, and the job fails with it.

Any other CI works the same way: store the key as a secret and expose it as `WIDGETIC_API_KEY`, install the CLI with npm at a version you pinned, and read the exit code.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.