# Organise clients: tags and ownerPath

> Label instances by client, plan or project with tags and an owner path, and find them again with one API call.

**Ask your management agent:**

- "List my agents tagged client=acme."
- "Add the tag plan=pro to the acme agent."


When you run agents for many clients or users, you need to know which agent belongs to whom. molted.net gives
every instance two kinds of labels.

## Tags

**Tags** are `key` / `value` pairs. Use them for anything you want to sort or filter by:

| Key | Example values |
|---|---|
| `client` | `acme`, `dupont-sarl` |
| `plan` | `starter`, `pro` |
| `role` | `sales-agent`, `support-agent`, `management-agent` |
| `env` | `production`, `staging` |
| `user.email` | the email of the end user |

Rules:

- up to 50 tags per instance;
- keys up to 128 characters, values up to 256;
- keys starting with `molted:` are reserved;
- filters are exact and case-sensitive: `Acme` is not `acme`.

The tag `role=management-agent` has a meaning of its own: it marks the instance the dashboard opens as your
[management agent](management-agent.md).

## ownerPath

The **ownerPath** is a path that places an instance in a tree, like folders:

```
/clients/acme
/clients/acme/sales
/clients/dupont
/internal/tests
```

Templates can carry an ownerPath too, so you can keep each client's templates next to its agents.

Neither tags nor ownerPath change who can access what. They are labels, not permissions.

## Setting them

**At creation**, in the dashboard or with the API:

```bash
curl -X POST "https://api.<region>.molted.net/v2/instances" \
  -H "Authorization: Bearer $MOLTED_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "handle": "acme-sales",
    "tenantId": "<your account id>",
    "instanceType": "small",
    "templates": [{ "name": "sales-agent" }],
    "ownerPath": "/clients/acme",
    "tags": [
      { "key": "client", "value": "acme" },
      { "key": "plan", "value": "pro" }
    ]
  }'
```

**Later**, from the dashboard (**Rename → Tags** on the instance), by asking the management agent ("Add the
tag plan=pro to the acme agent"), or with the API:

```bash
curl -X PATCH "https://api.<region>.molted.net/v2/instances/<instance id>/tags" \
  -H "Authorization: Bearer $MOLTED_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "set": [{ "key": "plan", "value": "pro" }], "remove": ["trial"] }'
```

Changing tags adds, replaces or removes only the keys you name. The others stay.

## Finding instances

List the instances that carry a tag:

```bash
curl "https://api.<region>.molted.net/v2/instances?tagKey=client&tagValue=acme" \
  -H "Authorization: Bearer $MOLTED_API_KEY"
```

Or those under a path:

```bash
curl "https://api.<region>.molted.net/v2/instances?ownerPath=/clients/acme" \
  -H "Authorization: Bearer $MOLTED_API_KEY"
```

- `tagKey` alone lists every instance that has the key, whatever its value.
- `tagValue` needs `tagKey`.
- Combining a tag filter and `ownerPath` returns the instances that match both.
- Results are paginated: pass `limit` (up to 100) and the `cursor` returned as `nextCursor`.

## Ideas

- One tag `client` per client, and per-client costs with [Resell and re-bill](resell-and-rebill.md).
- A tag `plan` to apply different [usage caps](usage-caps.md) by plan.
- A tag `user.email` to find a user's agent from your own product's backend.
