# Automating Swarmfile

Swarmfile is built to run without a person at a keyboard: a render-farm node, a CI job, a coding agent, or a seed box can mount, sync, and report on a repository on its own. This page is the map: which credential to reach for, the surfaces that are meant to be scripted, and the REST API contract.

## The credentials

| You need to | Use | Scope |
|---|---|---|
| Run an engine where nobody can complete a browser sign-in (render node, CI runner, agent fleet, seed box) | **Repository API key** (`sf_key_…`) | Exactly one repository |
| Run an untrusted agent or a least-privilege job on one branch (optionally only some paths) | **Branch- or path-scoped repository API key** (`sf_key_…`) | One branch; optional allow/deny globs (path-scoped keys are engine/API-only) |
| Act as yourself from a script - provisioning, webhook management, anything your own role can reach | **Personal access token** (`sf_pat_…`) | Whatever your account can do, live |
| Push and pull large objects with the stock `git-lfs` client | A **git-LFS credential** - a repository API key minted from the repository's git-LFS tab | That repository's LFS traffic |
| Work at a keyboard | Normal sign-in | - |

**Repository API keys** are org-owned machine identities. A key is walled off to one repository: it cannot read or write anything else in the org, and it is never treated as an owner or admin, no matter who minted it.

Mint, list, revoke, and set a per-key request ceiling with `swarmfile api-key` (admin or owner), or from the dashboard's **API keys** page. The dashboard page also gives a key its own monthly **spend budget** - a dollar ceiling below the org's [spend cap](https://swarmfile.com/docs/admin/billing-and-plans#spend-cap); enforcement uses the lower of the two, and a key over its budget has only its own writes paused. See [CLI: api-key](https://swarmfile.com/docs/cli/swarmfile#api-key).

A key can be **scoped** before it leaves the dashboard. `--branch-scope <branch>` binds it to one non-default branch and keeps git, history and merge requests on that branch; adding repeatable `--path-glob`/`--deny-glob` makes it **path-scoped**, an engine/API credential with no git, history, commit walk or merge requests; it can use the content-addressed batch checkpoint and the mount surface, not the branch archive or inline batch content.

The scope is set at mint time - to change it, revoke the key and mint a new one - and each allow/deny list holds at most 64 globs of at most 1,024 characters.

The branch must already exist and be non-default; minting does not validate it, and the hub refuses a dead or default branch on first use. The dashboard's **API keys** page and the tray's API keys panel use the same fields. See [Branch- and path-scoped keys](https://swarmfile.com/docs/guides/coding-agents#71-branch--and-path-scoped-keys-and-read-only-delegations).

**Personal access tokens** are not a separate identity - a PAT resolves to you, and every access check runs against your own current role, so it can never do more than you can right now and it dies with your access, with nothing separate to clean up. Mint one under **Settings → Access Tokens** (self-serve for any member; up to 20 active per member, per org, optionally expiring). See [Personal access tokens](https://swarmfile.com/docs/admin/identity#personal-access-tokens).

A **Starter+** service delegation can mint a short-lived branch-scoped key from a service user's PAT; a glob-free delegation also hydrates a workspace through the branch archive. See [Service-minted agent tokens and the branch archive](https://swarmfile.com/docs/guides/coding-agents#10-service-minted-agent-tokens).

Both are shown in full exactly once at creation, both are revocable with immediate effect, and both are audited. Two more rules worth knowing:

- On a machine with both an API key and a leftover `SWARMFILE_OIDC_REFRESH_TOKEN`, the API key wins - and the engine logs the choice rather than picking quietly.
- If your org enforces an [authentication policy](https://swarmfile.com/docs/admin/identity#authentication-policy-require-mfa-andor-a-verified-email), repository API keys are exempt (they're org-provisioned machine identities), while a PAT snapshots the factors and email assertion of the session that minted it - a token minted before you enrolled stops satisfying the policy when enforcement begins.

## Running an engine headlessly

Mint a key for the repository it serves, then hand it to the engine as `SWARMFILE_API_KEY`:

```bash
# On a machine where you're an org admin/owner:
swarmfile api-key create render-farm-04 --repository proj_9f2a

# A scoped key for an agent that should touch only one branch:
swarmfile api-key create agent-04 --repository proj_9f2a --branch-scope agent/worker-04 --path-glob 'src/**'

# On the headless machine:
export SWARMFILE_API_KEY=sf_key_...
export SWARMFILE_ORG_ID=...
export SWARMFILE_REPOSITORY_ID=proj_9f2a
swarmfile-engine
```

Key minting is a hard plan allowance, not a soft one: Free 1, Starter 2 per seat (floor 5), Pro 5 per seat (floor 10), Studio 10 per seat (floor 20), plus additional headless seats at $2/month each. A mint past the cap is refused with `keys_cap`; revoking frees the slot immediately, and a downgrade blocks new mints without revoking keys you already have. See [Billing & Plans](https://swarmfile.com/docs/admin/billing-and-plans).

Pin a key's own rate limit when a farm node should burst above the plan default:

```bash
swarmfile api-key rate-limit <key-id> 1000
```

For the full per-agent recipe - a labeled mount per agent and a shared dependency cache - see [Coding Agents](https://swarmfile.com/docs/guides/coding-agents).

### Scripting the CLI

- **`--json`** (global) makes the CLI print the engine's raw response - the intended path for scripts - and a usage error comes back as `{"code":"usage_error",…}` instead of prose.
- **Exit codes**: `0` success, `1` error, `2` a refusal a script should act on (permission, quota, a conflict that needs a human), `3` a long operation detached. See the [CLI reference](https://swarmfile.com/docs/cli/swarmfile).
- **Long operations** (`merge`, `commit`, `checkout`, `materialize`, `repository delete`, and more) wait and print their result by default. `--no-wait`, or Ctrl-C, returns the operation id (exit `3`) and the work carries on in the engine; collect the outcome later with [`swarmfile op`](https://swarmfile.com/docs/cli/swarmfile#op) - even after an engine restart.

## CI

There are two shapes, and they compose:

- **Swarmfile's own runner** - a headless engine that watches a branch and runs a job on every matching commit or tag, from an in-place checkout with no clone and no FUSE. See [Runner (Headless CI)](https://swarmfile.com/docs/guides/runner-as-ci).
- **Your existing CI reporting a check** - any system that can make two authenticated HTTP calls can gate a protected branch's **require checks to pass** rule through `/runner-runs`, using the same repository API key a runner uses. The runner guide has the exact two-call contract: [Reporting checks from your own CI system](https://swarmfile.com/docs/guides/runner-as-ci#reporting-checks-from-your-own-ci-system).

A headless engine authenticating with an API key also serves `git clone` and `git push` through the remote helper, once git access is enabled for the repository - see [Cloning a Repository with Git](https://swarmfile.com/docs/guides/git-clone#turning-on-git-access).

## Webhooks

Webhooks deliver a signed HTTP `POST` to a URL you control when something happens in the org - no polling. An **org-wide webhook** is owner-managed, retried, logged, and auto-disabled after repeated failure; a **personal webhook** is a fire-and-forget notification channel for your own account. Deliveries are dispatched when the event is recorded - typically within seconds; the hourly tick is a backstop for a missed wake.

The full envelope, signature verification, event kinds, and limits live in [Webhooks](https://swarmfile.com/docs/admin/webhooks). The automation-relevant part: an API key cannot manage webhooks or any other org-wide admin surface - that's deliberate. Manage them from a script with a personal access token instead, which acts as you.

## Rate limits

Request budgets are per-user and per-org, counted in requests rather than bytes, and content streaming isn't counted. A PAT shares its owner's per-user window; an API key gets its own window at the plan's per-user default, adjustable per key.

### How the org window scales

The org window scales with the fleet: it is the larger of the plan's per-org default and (active keys × the plan's per-key default), capped by the plan's per-org maximum - 10 Pro keys give 5,000/min, not 1,000 + 10 × 500. An owner/admin org override replaces the scaled value (scaling is off while it is set).

Enterprise automatic scaling stops at 50,000/min and has no key-allowance cap; on the other plans the headless-seat allowance (seats plus purchased seats) caps how many active keys count, so over-cap keys retained from a larger plan still work individually but do not enlarge the org window. A per-key rate limit changes that key's own bucket, not its contribution to the org window.

The engine batches and paces bursts against the budget the hub reports - burst behavior is covered in [Coding Agents](https://swarmfile.com/docs/guides/coding-agents#how-fast-can-one-user-create-files).

## The REST API contract

The hub's REST API is published as OpenAPI in the open-source [`swarmfile-api`](https://swarmfile.com/public/swarmfile/swarmfile-api) repository (MIT/Apache-2.0):

- `hub-openapi.json`: the hosted hub's routes that have authored request and response schemas.
- `hub-core-profile-openapi.json`: the core-profile contract the self-hosted [community server](https://swarmfile.com/docs/guides/self-hosting) answers for. A few community routes are deliberately **outside** the core profile - the branch archive and the batch checkpoint - so they are absent from this file (and from its generated client) even though the server serves them.
- `hub-core-profile-client.ts`: a dependency-free TypeScript client generated from that profile, built on `fetch`.
- `webhooks.md`: the webhook envelope, signature verification and event kinds.

![The community core profile is a contract: generated OpenAPI and a typed, dependency-free client ship in the open swarmfile-api repository. The cast clones that repository with the open git helper, shows the whole demo program, and runs it against a real community server - fetching the live profile and listing the repository's root over the HTTP API.](https://swarmfile.com/demo/oss-sdk.cast)

On hosted and self-hosted Swarmfile, the branch archive is a streamed tar (`GET /orgs/:org/repositories/:repository/files/archive?branch=…`, with `paths`, `format=tar.gz`, `ETag`/304 and a manifest sibling; the folder form too); see [Hydrating without a mount](https://swarmfile.com/docs/guides/coding-agents#11-hydrating-without-a-mount-the-branch-archive). Both scan at most 20,000 rows; the community server caps output at 256 MiB (hosted: 1 GiB) and limits archive starts to 30/minute per repository. A sandbox can write its side-effects back in one call with [the batch checkpoint](https://swarmfile.com/docs/guides/coding-agents#12-committing-a-sandboxs-changes-in-one-request); the community server's batch tree cap is documented there.

### Authentication and versioning

Requests to the hosted hub authenticate with `Authorization: Bearer` and a personal access token or an API key; a community server uses its own keys, minted by its admin command or with `swarmfile api-key create`. Every response carries `x-swarmfile-api-version: 1`, which changes only with a deliberate breaking change; a tagged release of `swarmfile-api` is the exact contract at that version.

### What to script against

There's no multi-language SDK. Routes that aren't in the published OpenAPI and aren't named in these docs are internal and can change without notice, so script against the documented routes, the `swarmfile` CLI (with `--json`), headless engines authenticated by API keys, the `/runner-runs` check API, and webhooks.

The branch archive and its manifest are the documented streaming/metadata exception: they are in the hosted `hub-openapi.json` but deliberately **outside** the community core profile (`hub-core-profile-openapi.json`), so the core-profile client does not cover them; their contract is also the route manifest, the agent SDK and the branch-archive runbook. The batch checkpoint is the same kind of documented exception.

On the community server, an archive's `X-Swarmfile-Archive-Head` may be a provisional member claim rather than a confirmed commit hash; read `X-Swarmfile-Archive-Claim-Status` (`confirmed`/`provisional`/`unknown`) before treating the value as repository history.

## Handling the credentials

- Copy a secret once and store it in a secret manager or your CI's secret store - never in the repo, an image layer, or shared shell history.
- Prefer an API key over a personal refresh token on shared machines; revoke a key the moment a node is retired.
- Keys are org-owned: deleting a repository revokes its keys with it, and every mint, revoke, and rate-limit change lands in the org's [Activity feed](https://swarmfile.com/docs/admin/operations#audit-log).

## Where to go next

- [CLI: `api-key`](https://swarmfile.com/docs/cli/swarmfile#api-key) - mint, list, revoke, and cap keys.
- [Personal access tokens](https://swarmfile.com/docs/admin/identity#personal-access-tokens) - the script credential that acts as you.
- [Runner (Headless CI)](https://swarmfile.com/docs/guides/runner-as-ci) - the runner, external check reporting, and ref materialization.
- [Coding Agents](https://swarmfile.com/docs/guides/coding-agents) - one mount per agent, a shared cache, service-minted tokens, the branch archive and the batch checkpoint.
- [Webhooks](https://swarmfile.com/docs/admin/webhooks) - event kinds and signature verification.
- [Billing & Plans](https://swarmfile.com/docs/admin/billing-and-plans) - headless-seat allowances.
