# Build your first Workspace

A Workspace is a folder of files that your server code can read and change. It tracks your changes as a diff, a list of what changed. A Source is read-only content that you put inside a Workspace, such as a folder of Markdown files.

In this tutorial, you list Markdown files from a local folder. Then you write a draft into the Workspace and see the change. The original files do not change.

> [!NOTE] Before you start
> Start with the app from [Installation](https://vitehub.dev/docs/getting-started/installation) and run commands from the app root. Everything saves on your computer, with no account or API key needed. Before deploying, read [Hosts](https://vitehub.dev/docs/workspace/hosts) to choose storage.

::tutorial-step{title="Install"}
## Install

Install the Workspace package:

```bash pm [commands/install]
pnpm add @vite-hub/workspace valibot
```

::

::tutorial-step{title="Define the Workspace"}
## Define the Workspace

Create `server/workspaces/docs.ts`. The file name is the Workspace name: `docs`. This file turns Workspace on, so you do not add a `workspace` option.

```ts [server/workspaces/docs.ts]
import { defineWorkspace, glob } from 'vite-hub/workspace'

export default defineWorkspace({
  sources: {
    docs: glob({ cwd: 'docs', include: '**/*.md' }),
  },
})
```

- `sources.docs` adds a Source named `docs` to the Workspace.
- `glob({ cwd: 'docs', include: '**/*.md' })` reads every Markdown file in your app's `docs` folder. In the Workspace, those files are under `docs/`.

Create `docs/intro.md` so the read route has a file to find:

```md [docs/intro.md]
# Workspace sample

This document comes from a read-only Source.
```

::

::tutorial-step{title="Add read and write routes"}
## Add read and write routes

Create a route that lists the Markdown files in the Workspace:

```ts [server/api/docs.get.ts]
import { defineEventHandler } from 'h3'
import { useWorkspace } from 'vite-hub/workspace'

export default defineEventHandler(async () => {
  const workspace = useWorkspace('docs')
  return workspace.fs.glob('**/*.md')
})
```

- `useWorkspace('docs')` opens the Workspace in read-only mode by default.
- `workspace.fs.glob('**/*.md')` lists the Markdown files, including files from the Source.

Create a second route that writes a draft:

```ts [server/api/drafts.post.ts]
import { defineEventHandler, readValidatedBody } from 'h3'
import * as v from 'valibot'
import { useWorkspace } from 'vite-hub/workspace'

export default defineEventHandler(async (event) => {
  const workspace = useWorkspace('docs', { mode: 'write' })
  const body = await readValidatedBody(event, body => v.parse(v.object({ text: v.string() }), body))

  await workspace.fs.writeFile('drafts/summary.md', body.text, {
    mediaType: 'text/markdown',
  })

  return workspace.diff()
})
```

- The route responds with `400` when the body lacks a `text` string.
- `{ mode: 'write' }` asks for write access. In read mode, `workspace.fs` has no `writeFile()`.
- `writeFile()` saves the draft in the Workspace. It does not change the files in your `docs` folder.
- `diff()` returns pending changes in the Workspace, here the new draft.

::

::tutorial-step{title="Read and write the tree"}
## Read and write the tree

Start the dev server:

```bash [commands/start]
pnpm dev
```

Keep the server running. Open a second terminal and call the read route:

```bash [commands/request]
curl http://localhost:3000/api/docs
```

The response lists `docs/intro.md`. Now send a draft to the write route:

```bash [commands/write]
curl -X POST http://localhost:3000/api/drafts \
  -H 'content-type: application/json' \
  -d '{"text":"# Draft"}'
```

The response is the diff. It shows the new file `drafts/summary.md`. Your `from` and `to` values are different:

```json [output/diff.json]
{
  "from": "361e4d3ccd99a3c02491bb7bea076549e9886868e5cd701c3a3ec10938534a09",
  "to": "b39f89281fd2971be9b8949b030c6382186e6df7347c52c1648c22ca9f060b96",
  "entries": [
    { "path": "drafts", "type": "added", "after": { "type": "directory" } },
    {
      "path": "drafts/summary.md",
      "type": "added",
      "after": {
        "type": "file",
        "digest": "2f8aa1f6d9602d7214a36d962d142cffa28dd28a8adf24bcf5c4589db06d52ff",
        "size": 7
      }
    }
  ]
}
```

Call `/api/docs` again and the list includes the draft. The file `docs/intro.md` in your app does not change.

::

## Next steps

::card-group
:::card{title="Workspaces and Sources" icon="i-lucide-folder-git-2" to="/docs/workspace/concepts"}
Decide when to use a Workspace, a Source, or both.
:::

:::card{title="Server API" icon="i-lucide-code-2" to="/docs/workspace/server-api"}
Take snapshots, make commits, and sync Sources.
:::

:::card{title="Hosts" icon="i-lucide-cloud-cog" to="/docs/workspace/hosts"}
Choose where your deployed app stores its files.
:::
::

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/workspace.md)
- [Get started](https://vitehub.dev/raw/docs/workspace/get-started.md) (this page)
- [Configure](https://vitehub.dev/raw/docs/workspace/configure.md)
- [Server API](https://vitehub.dev/raw/docs/workspace/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/workspace/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/workspace/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/workspace/limits-and-errors.md)
- [Workspaces and Sources](https://vitehub.dev/raw/docs/workspace/concepts.md)

Next: [Configure](https://vitehub.dev/raw/docs/workspace/configure.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
