# Expansion and field selection

> Ask for related objects with ?expand= and trim responses with ?fields=. Both work on every resource.

Source: https://docs.wpwaterfall.com/developers/expansion-and-fields

Every resource route accepts two query parameters on top of its own. Both take a comma separated list of names. Both work on a single object and on a list, where they apply to each row of `data`.

## How does expand work?

`?expand=` adds related objects to the response under the relation's name. WP Waterfall loads them with your key's own permissions, so an expansion never shows you something you could not read on its own route.

```bash
curl -s "https://api.wpwaterfall.com/v1/sites/01J9K2Q7M4Z8X0T6Y3B5N1C2D4?expand=node,domains" \
  -H "Authorization: Bearer wpw_live_YOUR_SECRET"
```

```json
{
  "id": "01J9K2Q7M4Z8X0T6Y3B5N1C2D4",
  "name": "Shop",
  "live_environment": { "id": "...", "node_id": "..." },
  "node": { "id": "...", "name": "us-east4-a1", "region": "us-east4", "status": "online" },
  "domains": [{ "id": "...", "hostname": "shop.example.com", "status": "active" }]
}
```

| Resource | Relations | Routes |
|---|---|---|
| Site | `node`, `domains`, `environments` | `GET /v1/sites`, `GET /v1/sites/{id}`, `PATCH /v1/sites/{id}` |
| Environment | `node`, `domains`, `site` | `GET /v1/environments/{id}`, `GET /v1/sites/{id}/environments`, `PATCH /v1/environments/{id}` |
| Domain | `environment`, `site` | `GET /v1/domains/{id}`, `GET /v1/environments/{id}/domains`, `PATCH /v1/domains/{id}` |
| Job | `site`, `environment` | `GET /v1/jobs`, `GET /v1/jobs/{id}`, and every `202` answer that returns a Job |
| Node | none | `GET /v1/nodes`, `GET /v1/nodes/{id}` |

A relation that ends in `s` returns a list. A relation that points at nothing returns `null`, for example `node` on a site that is not placed on a server yet.

A name that is not in the table answers `422` with a sentence that lists the relations you can expand:

```json
{
  "status": 422,
  "title": "Unprocessable Entity",
  "detail": "Unknown expansion owner on a site. Relations you can expand: node, domains, environments.",
  "errors": [{ "field": "expand", "message": "..." }]
}
```

Expansion works on `POST` answers too. `POST /v1/sites?expand=site` returns the Job with the new site inside it, and a repeated `Idempotency-Key` replays the same expanded answer.

## How does fields work?

`?fields=` keeps only the top-level fields you name. Use it to make list calls small.

```bash
curl -s "https://api.wpwaterfall.com/v1/sites?fields=id,name,status" \
  -H "Authorization: Bearer wpw_live_YOUR_SECRET"
```

```json
{
  "data": [{ "id": "01J9K2Q7M4Z8X0T6Y3B5N1C2D4", "name": "Shop", "status": "active" }],
  "next_cursor": null
}
```

Rules:

- Names come from the resource's schema in the API reference. `id` is always allowed.
- A relation you expand in the same call counts as a field: `?expand=node&fields=id,node` returns exactly `id` and `node`.
- Field selection is top level only. Nested objects such as `live_environment` come back whole.
- `next_cursor` stays on list answers.
- An unknown name answers `422` with a sentence that lists the fields you can request.

On a route whose body is not one of the resources above, `fields` still works and the allowed names are the keys of the body itself.

## In what order does the API check a request?

Authentication comes first. A call with no key answers `401` whatever the query string says. Then the query string is checked, before the route does any work. A bad `expand` or `fields` on a `POST` never creates anything.
