# Manage a workspace as code (/workflows/code/workspace)

A whole workspace can be read and written as a directory: one folder per kind of resource, with each workflow written exactly as [workflows as code](/workflows/code) writes it. One apply creates the resources before the workflows that use them, so a coding agent can build a working setup from scratch.

Note:

Workspace as code is in preview, behind the [same setting](/workflows/code) as workflows as code. It adds the `sim workspaces source` commands to the [CLI](/cli/workspaces#workspace-as-code).

## The directory \[#the-directory]

The layout of every kind and each file's role is in [Directory layout](/workflows/code/directory). A resource's key is its file or folder name, and a workflow names it by that key:

```ts title="workflows/support/workflow.ts"
const policy = knowledge.search('Policy Search', {
  knowledgeBaseId: knowledgeBase('policies'),
  query: start.question,
})
const answer = agent('Answer', {
  model: 'claude-sonnet-4-6',
  messages: [{ role: 'user', content: t`${policy.results}\n\n${start.question}` }],
  tools: [customTool('lookup-order')],
  skills: [skill('tone')],
})
const log = table.insertRow('Log Question', {
  tableId: table('orders'),
  data: { orderId: start.orderId, status: 'answered' },
})
```

`get` writes a short primer for coding agents atop `.sim/AGENTS.md`, the guide, with deletes in depth, Git and CI, and promoting to another workspace in `.sim/GUIDE-advanced.md`. It also writes `.sim/examples/workspace/`, a complete example of every kind and the workflow that uses them.

## Edit a workspace \[#edit-a-workspace]

```bash
sim workspaces source get --dir ./my-workspace
sim workspaces source check --dir ./my-workspace
sim workspaces source status --dir ./my-workspace
sim workspaces source apply --dir ./my-workspace --dry
sim workspaces source apply --dir ./my-workspace --yes
```

`check` compiles every workflow on your machine against what the directory declares: an unknown table, credential, knowledge base, skill, custom tool, MCP server or workflow key, a table row write naming a column the table does not declare, and a child workflow call whose `inputMapping` names an input the child's Start trigger does not declare, are reported, and an env name `secrets.ts` lacks is a warning. It checks each `tables/<key>.rows.jsonl` line against its table the way an apply writes it (a required cell, a unique value, a column's type and select options), so the dry run's `E_TABLE_ROWS` shows up offline at its line. It exits 1 on errors and, as `sim workflows source check` does, on a broken block you added.

Unlike a workflow directory's `check`, it needs the block catalog but fetches it for you: on a fresh clone with none cached it downloads the catalog once with the profile's credentials, and without credentials it exits `1` and tells you to run `get`. It also regenerates `.sim/sdk/workspace.d.ts` from the declarations and then type-checks every file, so a table, workflow or other key the directory declares since the get is typed in the same check. Run in a `workflows/<key>` folder, `sim workflows source check --dir .` checks that one workflow the same way.

The dry run prints the plan (`+` create, `~` update, `=` adopt an existing resource with the same name, `-` delete) and every error with its file and line. `--only tables,workflows` limits a get or an apply to some kinds.

## Test workflows on apply \[#test-workflows-on-apply]

```bash
sim workspaces source check --dir ./my-workspace && sim workspaces source apply --dir ./my-workspace --yes --run lead-intake='{"email":"a@b.co"}'
```

`--run <key>='<input>'` dry-runs the saved draft of `workflows/<key>/` with that input after the apply, as `sim workflows run <workflowId> --dry --input` does: writes return placeholders and are listed under each run's `sideEffects`. The input is JSON or `@file`; repeat `--run` to test several workflows. The result lists each run under `runs` with its workflow key and id, status and output. A run that does not complete exits `1` and prints the failing block's file and line in `workflows/<key>/`; the applied drafts stay in place, so fix the source and run the same command again.

With `--run`, `deployments/` is applied only after every run completed, in a second write, so a deployment with `live: 'draft'` publishes only a draft that passed its test. Without `--run` it is applied with everything else. `--run` is refused with `--dry` (nothing is written to test), with `--to` (test in this workspace, then promote), and with an `--only` that leaves out `workflows`.

An apply adopts a resource it never read (one made in Sim under the name a new declaration gives) only when the declaration matches it, or only adds to it: a column, rows into an empty table, a document. An adopt that would change or remove anything the resource holds is refused with `E_APPLY_NO_BASE`, even with `--allow-delete`. Run `get` into a new directory to read the resource, then make your edit there, or give the declaration another name.

`status` shows what would happen if you applied, without writing anything: the files you created, edited, or deleted, grouped by resource; what changed in Sim since the `get` (a resource edited, added, or deleted there, a workflow edited in the workflow builder included); the conflicts, where a resource changed on both sides; and the one command to run next. It exits `0` when nothing changed, `11` when only your edits are unapplied, and `12` when something changed in Sim since the `get`, so a script or an agent can branch on it. Its JSON `plan` counts errors and warnings as the dry run of the apply it names next would, a conflict as an error. Without a connection it shows your edits only and says so; `--offline` skips the server. `diff [paths or kind:key…]` prints the edits as unified diffs (a binary file by its sizes), and `diff --server` compares the directory with what Sim holds now. Both take `--json` (or `--output json`).

## Workspace API keys \[#workspace-api-keys]

A personal API key or an OAuth login can apply every kind its workspace role allows. A workspace API key cannot create or change workflows, skills, secrets or deployments. An apply with one that would is refused with `E_APPLY_FORBIDDEN`, naming each resource, before anything is written. A `get` with a workspace key leaves out the kinds it cannot read, such as `secrets.ts`, and names them with the same code.

To apply the rest with a workspace key, such as in CI, narrow the apply to the kinds it can write: `sim workspaces source apply --dir ./my-workspace --only tables,knowledge,files,tools,mcp,credentials.ts --yes`. Deployments need an admin's personal key.

## Deletes \[#deletes]

- A kind is managed only when its folder exists. A directory without `skills/` never touches a skill.
- Removing the file of a resource the directory manages deletes it only with `--allow-delete`; without the flag the apply is refused. The flag also covers a removed table column, managed table row, knowledge base document, workspace file, workflow block or workflow variable.
- A resource the directory never managed is kept, and the apply names it in a warning.
- Something deleted in Sim is not re-created by an unchanged directory, and an edit to it is refused as stale.
- Table rows are part of the directory only when the table's `rows` opts in (`tables/<key>.rows.jsonl`). Connector-synced documents and run history never are.
- Seed rows (`rows: { mode: 'seed' }`) belong to the directory: `get` keeps the declaration and the rows file beside it, but Sim does not store them, so a get into a new directory has the declaration without the file.
- A delete cannot be undone by applying an older copy of the directory, such as a reverted commit. The resource stays deleted, and its rows, documents, run history, webhook URLs and schedules are gone. The dry run marks each change that deletes data and counts them as `destructive`, a workflow update that removes blocks or variables included.

## Good to know \[#good-to-know]

- Secret values never appear. Workflows read environment variables as `env.NAME`, tool code as `$env.NAME`, and credentials by key.
- Renaming a skill does not rewrite the workflows that use it: each agent keeps the name it stored until its skills list is next edited.
- Hidden files (a name starting with a dot, such as `files/.env`) and names with a character no workspace path holds (such as `:` or `?`) are never sent. An apply names each one in a `W_PATH_NOT_SENT` warning.
- A knowledge base's `embeddingModel` is fixed when it is created. An apply that changes it is refused.
- An apply that stops part-way exits `8`, prints the plan again with each change marked written or not written (`notWritten` in JSON), and saves what it wrote into the directory. Run the same apply again until it exits `0`. The dry run checks an MCP server's URL as the write does, so an unresolvable host is refused before anything is written.
- An apply exits `5` when a resource changed in Sim after you read it. Run `get` into a new directory and reapply your edit.
- Size caps, and what is left out of a get, are in [Limits](/workflows/code/limits).

## Related docs \[#related-docs]

- [Promote to another workspace](/workflows/code/promote)
- [Run in CI](/workflows/code/ci)
- [Directory layout](/workflows/code/directory)
- [CLI workspace commands](/cli/workspaces#workspace-as-code)
