Skip to content

Scripting the CLI

A script uses the unspar CLI the same way you do in a terminal. It needs output it can read, it can't answer questions, and it logs in through an environment variable.

All docs pages

Everything on the other CLI pages works in a script, too. Here, a script means any program that runs the CLI without a person at the keyboard, including shell scripts, CI jobs on services like GitHub Actions, and AI agents.

Running the CLI unattended changes three things. Your script needs output it can parse, so it uses --json or a template. It can’t answer yes-or-no prompts, so it passes --force. And without a browser for unspar login, it reads its key from an environment variable. This page covers those options, along with unspar api, which calls the REST API directly for the few tasks that don’t have a dedicated command.

Log in without a browser

In a CI job, set the UNSPAR_API_KEY environment variable to a team API key so the CLI doesn’t need a stored login. Create the key under API tokens in Settings and save it in your CI provider’s secret store. See API keys and connections for what a key is allowed to do.

UNSPAR_API_KEY overrides a stored login for every command. On a machine that has both, unspar auth status tells you which one is active. unspar auth env shows the profile and addresses a job would use, without reading a key or contacting Unspar.

On a server that keeps a stored login, unspar setup configures a profile and is safe to run more than once. If the stored key still works, it leaves it alone. Otherwise, it reads a new key from stdin:

unspar setup --profile ci --no-prompt < api-key.txt

If Unspar rejects the stored key, setup won’t replace it and exits with code 3. To replace the key on purpose, run:

unspar login --api-key --no-prompt --force < api-key.txt

Which environment variables are there?

Environment variables change a setting for a single shell or job without editing the CLI’s files. A variable that is set to an empty string counts as unset.

Variable What it does
UNSPAR_API_KEY The team API key. It overrides the stored login for every command.
UNSPAR_PROFILE The profile to use, matching --profile.
UNSPAR_LOCALE The language for Unspar’s messages, en or de.
UNSPAR_CONFIG A custom path for config.yml.
UNSPAR_AUTH_FILE A custom path for auth.json.
UNSPAR_PAGER, then PAGER The program for long output. Defaults to less -FRX if neither is set.
NO_COLOR Turns off colors, matching --no-color.

When a setting is defined in more than one place, a command-line option overrides an environment variable. The environment variable overrides the profile in config.yml, and the profile overrides the built-in default. For the profile itself, the CLI uses the first of these that is set:

  1. --profile in the command
  2. UNSPAR_PROFILE
  3. current_profile in config.yml
  4. the profile called default

Get output a script can read

By default, the CLI prints human-readable tables and summaries, and opens long output in a pager on a terminal. These options give a script output it can parse:

Option What you get
--json The result as JSON, with no wrapper around it.
--plain Tab-separated rows without colors.
--jq '<expression>' The JSON filtered through a built-in jq expression. Text values keep their quotes.
--template '<template>' The JSON rendered with a Go template like '{{.id}}'. Text values come out without quotes.
--include-meta With --json, --jq or --template, wraps the result under data and adds meta with the request ID.
--no-pager, --no-color Turns off the pager or colors.
--debug Writes request details to stderr, with keys hidden.

To capture a value in a shell variable, use --template, since --jq leaves quotes around strings.

id=$(unspar articles create --project <project-id> --brief "How DNS caching works" --force --template '{{.id}}')
unspar articles status "$id" --jq .status

--template only formats CLI output. The template used to write an article is --content-template. To prevent mix-ups, the CLI rejects any --template value that doesn’t contain {{.

Some commands don’t support JSON output. login, logout, setup, profile create, profile use, profile delete and config set always print plain text for people. mcp serve ignores --json because its output is the protocol for the AI tool. Errors also bypass --jq and --template, so check the exit code before parsing stdout.

Why does a script need –force?

Commands that spend credits or publish an article outside Unspar ask for confirmation first. Prompts require a terminal, which doesn’t exist in a CI job, a pipe or an AI agent. Without one, the command stops with exit code 1 and sends nothing:

Error: this action needs confirmation; re-run with --force to proceed without a prompt

These commands prompt for confirmation:

  • articles create, unless you add --no-queue
  • articles queue
  • articles retry
  • articles derive
  • articles revise
  • articles restore-revision
  • articles publish
  • profile delete

--force answers yes to every prompt in a command, and --yes is an older alias for it. --no-prompt does the opposite, turning every prompt into an error even when you’re in a terminal.

articles cancel and articles approve never prompt, and neither do changes to projects, templates or unspar api. Because an AI agent has no terminal, it always passes --force, which means prompts won’t stop it. Control what an agent can do through its API key instead. A Read only login can’t spend credits or publish.

What do the exit codes mean?

Every command finishes with an exit code. Check it before your script tries to read the output.

Code What it means Examples
0 Success. status --wait finishes with completed or published.
1 Invalid command, or stopped before sending. A missing option, an unanswered prompt, or --wait ending in failed, cancelled, action_required or a timeout.
2 Unspar or the network returned an error. No connection, or not enough credits (insufficient_credits).
3 Login is missing or invalid. No key, or an expired or revoked key.
4 The key doesn’t have permission for this action. A missing permission, or a project the key can’t access.
5 Unspar couldn’t find the resource. A wrong ID, or an ID from another team.
6 Too many requests. You hit the rate limit.

Error messages from Unspar include a request ID. Include that ID if you contact Support.

Wait for an article in a script

Adding --wait to articles create or articles status pauses until the article reaches a final status. Progress updates go to stderr, and the final result prints to stdout once. Use --interval to set how often the CLI checks (10 seconds by default) and --timeout to set how long it waits (30 minutes by default). Durations look like 30s, 5m or 1h.

Exit code 1 can mean the article failed or the wait timed out. Timing out doesn’t cancel the article. To tell the two apart, check the article’s status after --wait exits:

unspar articles status "$id" --wait --timeout 1h
code=$?
if [ "$code" -eq 1 ]; then
  status=$(unspar articles status "$id" --template '{{.status}}')
  echo "Wait ended with exit code 1. Current status: $status"
elif [ "$code" -ne 0 ]; then
  echo "unspar failed with exit code $code"
fi

articles revisions <article-id> --wait works the same way while waiting for a round of changes.

Run a command twice without paying twice

Every write request sends an idempotency key, which is a unique label for that request. If Unspar receives the same label twice, it returns the original response instead of running the action again. By default, the CLI generates a fresh key on every run, so running articles create twice creates two articles and spends credits twice.

To make retries safe, pass the same --idempotency-key on both runs, using the same login and options. Unspar remembers a key for 24 hours. Keys can be 1 to 255 visible characters, and the CLI never prints them.

unspar articles create --project <project-id> --brief "How DNS caching works" \
  --force --idempotency-key dns-caching-2026-11-25

If you aren’t sure whether a create command went through, check recent articles before running it again:

unspar articles list --project <project-id> --sort -created_at --limit 5

To inspect what a write command would send, add --dry-run. It prints the HTTP method, path and body without needing a login or contacting Unspar.

Fetch long lists

List commands return a single page unless you ask for more. Pick one of these options per command:

Option What it fetches
--limit 100 Up to 100 rows, across as many pages as needed.
--all All pages.
--page 2 --per-page 50 That specific page. Each page holds 1 to 100 rows.

Without these options, projects list and articles list return 30 rows. Other list commands return the first page Unspar sends.

A GitHub Actions example

This workflow generates an article from a topic you enter and saves the Markdown file as an artifact so someone can review it before it goes live. Make sure the project won’t publish automatically by turning Auto-publish completed articles off, or turning Require human review before auto-publish on. See Scheduling and auto-publish.

Store your API key in a repository secret named UNSPAR_API_KEY, and your project ID in a repository variable named UNSPAR_PROJECT_ID. Save the workflow as .github/workflows/write-article.yml, then trigger it from the Actions tab with Run workflow.

name: Write an article

on:
  workflow_dispatch:
    inputs:
      topic:
        description: Topic of the article
        required: true

jobs:
  write:
    runs-on: ubuntu-latest
    env:
      UNSPAR_API_KEY: ${{ secrets.UNSPAR_API_KEY }}
      PROJECT_ID: ${{ vars.UNSPAR_PROJECT_ID }}
      TOPIC: ${{ inputs.topic }}
    steps:
      - name: Install the unspar CLI
        run: |
          curl -fsSL https://app.unspar.com/install.sh | sh -s -- --dir "$HOME/.local/bin"
          echo "$HOME/.local/bin" >> "$GITHUB_PATH"

- name: Write the article
        run: |
          id=$(unspar articles create --project "$PROJECT_ID" --brief "$TOPIC" --force --template '{{.id}}')
          echo "Article $id started"
          unspar articles status "$id" --wait --timeout 1h
          unspar articles content "$id" --with-images --output article.md

- name: Keep the article
        uses: actions/upload-artifact@v4
        with:
          name: article
          path: article.md

Passing the topic through an environment variable keeps quotes in the topic from breaking the shell command. If the article fails, status --wait exits with 1 and stops the job. Each run costs whatever the article costs, normally 1 credit.

Call the API directly

unspar api calls the REST API using the login and settings from your current profile. Use it for actions that don’t have their own CLI command. Pass the HTTP method and the path, where the path is everything after https://app.unspar.com/api/v1.

unspar api get /whoami
unspar api get /projects --query status=active
unspar api post /articles/estimate --body '{"project":"<project-id>","brief":"How DNS caching works","fact_check":true}'

The supported methods are get, post, patch and delete. By default, the CLI prints the data field of the response. Use these options to build your request:

Option What it does
--query key=value Adds a query parameter. Can be repeated.
--field key=value Adds a text field to the request body. Can be repeated.
--body '<json>' Sends a JSON body. Use this for numbers, true or false, and nested objects.
--body-file <file> Reads the JSON body from a file, or from stdin when set to -.
--raw Prints the full response, including meta and the request ID.
--secret-field <name> Hides that field’s value in previews, debug output and error messages.
--all, --limit With get, fetches multiple pages, just like the list commands.

Use only one body option per command. Put tokens and passwords in a file with --body-file so they stay out of your shell history. The CLI rejects full URLs, paths containing ? or #, and paths ending with a slash.

unspar api post, patch and delete never prompt for confirmation and don’t need --force. Because some calls spend credits and others permanently delete data, you can add --dry-run first to see what the command will send.

Here are the calls people use most often:

What you want Command
See the price of an article unspar api post /articles/estimate
Change a draft or a failed article unspar api patch /articles/<article-id>
Delete an article unspar api delete /articles/<article-id>
See each attempt to publish an article unspar api get /articles/<article-id>/publishing-attempts
List the image styles unspar api get /image-presets
Find ideas for a project. This costs 1 credit. unspar api post /projects/<project-id>/idea-runs
Connect or test an outlet unspar api post /outlets, unspar api post /outlets/<outlet-id>/test
Delete a template copy unspar api delete /templates/<template-id>
Delete a project unspar api delete /projects/<project-id>
Turn Project Auto off unspar api delete /projects/<project-id>/auto

When you delete an article, Unspar replies with an empty body. The CLI prints api protocol error: empty response body (status 204, ...) and exits with code 2, even though the article was deleted.

unspar api doesn’t support PUT requests. Turning Project Auto on requires one, so do that in the web app or with your own HTTP client. The API reference lists all endpoints and their fields.

Last updated 25 November 2026. This page as Markdown: scripting.md

Try Unspar for free

Create a project, give it a topic and read the article before anything goes live.