> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Serve your first mock

Make `GET /users/123` return Ada. Once you see the response, add a second outcome and switch to it while the server is running.

## Install the CLI

For a project install, run the command in the directory containing your package manifest. In an empty directory, run `npm init -y` first. You will also need a POSIX shell and curl.


```sh [npm]
npm install -D @decoyhq/cli
```

```sh [pnpm]
pnpm add -D @decoyhq/cli
```

```sh [yarn]
yarn add -D @decoyhq/cli
```

```sh [bun]
bun add -d @decoyhq/cli
```

[Install a standalone binary](/docs/install/index.md).

## Define the user response

Create a folder for your mock files.

```sh title="Terminal: create the Route directory"
mkdir -p mocks/routes
```

Save the following [Route](/docs/terms/index.md#route) as `mocks/routes/users.yaml`. It handles `GET /users/{id}`. The `user-123` [Case](/docs/terms/index.md#case) picks requests for ID `123`, and its `success` [Behavior](/docs/terms/index.md#behavior) returns Ada.

```yaml title="mocks/routes/users.yaml"
id: users-by-id
transport: http
match: { method: GET, path: '/users/{id}' }

cases:
  user-123:
    match: { pathParams: { id: '123' } }

    behaviors:
      success:
        status: 200
        body: { id: '123', name: Ada }
```

Create a [Collection](/docs/terms/index.md#collection) named `happy-path` so Decoy starts with the `success` response.

```yaml title="mocks/collections.yaml"
- id: happy-path
  routes: [users-by-id:user-123:success]
```

## Request the user

Run the server from the directory containing `mocks/`. Leave this terminal open. The `--watch` flag lets you edit the files in the next step.


```sh [npm]
npm exec --package=@decoyhq/cli -- decoy serve --routes mocks/routes --collections mocks/collections.yaml --port 8080 --watch
```

```sh [pnpm]
pnpm exec decoy serve --routes mocks/routes --collections mocks/collections.yaml --port 8080 --watch
```

```sh [yarn]
yarn decoy serve --routes mocks/routes --collections mocks/collections.yaml --port 8080 --watch
```

```sh [bun]
bun run decoy serve --routes mocks/routes --collections mocks/collections.yaml --port 8080 --watch
```

If you installed the standalone binary, run the same command starting at `decoy serve`.

In a second terminal, request the user.

```sh title="Terminal 2: request the user"
curl -i http://localhost:8080/users/123
```

Look for **200 OK** and this response body:

```json title="200 OK response body"
{ "id": "123", "name": "Ada" }
```

## Switch to a 404

Replace the Route file with this version. It adds a second Behavior named `missing` beside `success`.

```yaml title="mocks/routes/users.yaml"
id: users-by-id
transport: http
match: { method: GET, path: '/users/{id}' }

cases:
  user-123:
    match: { pathParams: { id: '123' } }

    behaviors:
      success:
        status: 200
        body: { id: '123', name: Ada }

      missing:
        status: 404
        body: not found
```

Append a second Collection to `mocks/collections.yaml`. Its [Behavior address](/docs/terms/index.md#behavior-address) ends in `missing`, so the same request now has a different response available.

```yaml title="mocks/collections.yaml (append this entry)"
- id: user-missing
  routes: [users-by-id:user-123:missing]
```

After Decoy reloads the files, switch Collections from the second terminal.

```sh title="Terminal 2: switch to user-missing"
curl -X POST http://localhost:8080/__decoy__/control/useCollection \
  -H 'content-type: application/json' \
  -d '{"collection":"user-missing"}'
```

Repeat the original request. This time you'll see **404 Not Found** and the body `not found`.

```sh title="Terminal 2: request the same user again"
curl -i http://localhost:8080/users/123
```

Try a path you haven't mocked:

```sh title="Terminal 2: request an unmocked path"
curl -i http://localhost:8080/orders
```

Look for **501 Not Implemented** and `x-mock-miss: true`. Decoy tells you what failed to match instead of forwarding the request to a real service. See [How mock misses work](/docs/routes/index.md#misses-are-fail-closed).

Stop Decoy with Ctrl-C in the server terminal.

To keep these settings in your project, [put the flags in `decoy.yaml`](/docs/config/index.md). To match another user, add a [Case](/docs/routes/index.md#case-match). To switch this response from a browser test, follow the [Playwright](/docs/playwright/index.md) or [Testplane](/docs/testplane/index.md) setup.
