---
title: Resources
description: Declare a named entity, its fields, and its Backlit endpoints.
---

A resource is a named entity such as an order, customer, or product. Its name
is the first part of each view, action, and lookup address.

A resource declares reusable fields and holds its registered endpoints. It does
not fetch data and does not create a protocol node. Views, zones, actions, and
lookups fetch data when they resolve.

## API

```ts
defineResource(name: string, fields: FieldBuilderContract[]): Resource
```

| Member      | Input                             | Result                                      |
| ----------- | --------------------------------- | ------------------------------------------- |
| `name`      |                                   | Returns the resource name.                  |
| `pick`      | `...fieldNames`                   | Returns fields in the requested order.      |
| `get`       | `fieldName`                       | Returns one field builder.                  |
| `view`      | `name`, params or state, resolver | Registers and returns a view definition.    |
| `getView`   | `name`                            | Returns a registered view or `undefined`.   |
| `action`    | `name`, params, schema, handler   | Registers and returns an action definition. |
| `getAction` | `name`                            | Returns a registered action or `undefined`. |
| `lookup`    | `name`, params or state, resolver | Registers and returns a lookup definition.  |
| `getLookup` | `name`                            | Returns a registered lookup or `undefined`. |

Field, view, action, and lookup names must be unique in their own group on one
resource.

## Basic resource

Pass the address name and the field declarations to `defineResource`.

<BacklitExample path="resources/basic" view="overview" />

## Resource without fields

A resource can have no fields. This is useful when its screens show computed
blocks instead of record data.

<BacklitExample path="resources/empty" view="overview" />

## Name

The `name` property keeps its literal TypeScript type. Backlit also uses this
value in endpoint addresses.

<BacklitExample path="resources/name" view="overview" />

## Fields

Declare fields once on the resource. Tables, datalists, forms, and lookups can
reuse the same builders.

<BacklitExample path="resources/fields" view="list" />

See [Fields](/reference/fields) for every field type and option.

## Pick fields

Use `pick` when a block needs several resource fields. The returned tuple keeps
the requested order. TypeScript rejects a name that the resource does not have.

<BacklitExample path="resources/pick" view="list" />

## Get one field

Use `get` when code needs one field builder and its field-specific methods. The
returned builder keeps its exact type.

<BacklitExample path="resources/get" view="overview" />

## Registered views

Calling `view` registers the definition. Use `getView` when a host must find a
definition by name. An unknown name returns `undefined`.

<BacklitExample path="resources/views" view="overview" />

See [Views](/reference/views/view) for view registration and resolver options.

## Registered actions

Calling `action` registers the definition. Use `getAction` to find it by name.
The action schema can use any Standard Schema validator.

<BacklitExample path="resources/actions" view="overview" />

## Registered lookups

Calling `lookup` registers the definition. Use `getLookup` to find it by name.

<BacklitExample path="resources/lookups" view="overview" />

A lookup can declare params, the same as a view. The resolver reads their
values from `ctx.paramValues`. The search query stays on the context and can be
empty.

```ts
const statePicker = states.lookup(
  'byCountry',
  { params: ['country'] },
  (ctx, lookup) =>
    lookup
      .setFields(states.pick('code', 'name'))
      .withData(statesOf(ctx.paramValues.country))
)
```

See [Lookup target](/reference/targets/lookup) for how an input supplies the
args.

## All members

One resource can declare fields and register views, actions, and lookups. Each
endpoint group has its own name registry.

<BacklitExample path="resources/all_options" view="overview" />

## Groups and endpoint discovery

`resource.group(fields)` creates an anonymous group for a relation display.
It checks field callbacks against the target resource record type.
`views()`, `actions()`, and `lookups()` return registered definitions.
`toAgentTools()` collects opted-in definitions. See [Agent tools](/reference/agents).
