# Subnet services (/docs/guides/services)

A validator (a hotkey with a validator permit on a subnet) or the subnet owner,
for its owner hotkey, can publish a service with [`set-service`](/docs/tx/set-service), so that
apps and agents can find what the subnet serves and how it is paid for. A
service is a url to a document of its services, which customers pay for by
spending [gamma](/docs/guides/gamma) at its hotkey. The coldkey that owns the
hotkey signs it and pays a normal fee. Publishing an empty url removes the
service.

```bash
btcli tx set-service --netuid 1 --url https://example.com/services.json -w my_coldkey -H my_hotkey
btcli query services --netuid 1
```

```python
await client.execute(bt.SetService(netuid=1, url="https://example.com/services.json"), wallet)
services = await client.subnets.services(netuid=1)  # {hotkey: {url, owner}}
```

The permit is checked when the service is published. A service follows a hotkey
swap, but what was spent at it stays under the old hotkey, so list the new hotkey
in `publishers` before swapping. It is removed when its neuron is deregistered.
`owner` marks the service of the subnet owner's hotkey while the owner owns it.
A leased subnet's beneficiary publishes it through its `SubnetLeaseBeneficiary`
proxy. Customers spend gamma at a service with `btcli gamma spend` (see
[Gamma](/docs/guides/gamma#spending)).

## The document [#the-document]

The chain stores the url. The document says what the services
are, how to reach them, and how they are paid for:

```json
{
  "version": 1,
  "name": "Example Inference",
  "description": "Pay-per-call inference for open models over an OpenAI-compatible API.",
  "publishers": [{ "netuid": 1, "hotkey": "5…" }],
  "services": [
    { "type": "openapi", "endpoint": "https://…", "spec": "https://…/openapi.json" },
    { "type": "mcp", "endpoint": "https://…/mcp" }
  ],
  "payments": [
    { "type": "gamma", "signature": "btauth/1", "balance": "https://…/balance" },
    { "type": "x402" }
  ]
}
```

* `name` and `description` say what is sold. Who sells it is the subnet's or
  the coldkey's on-chain identity.
* `publishers` lists, by netuid and hotkey, every published service whose url
  may point to the document, including hotkeys it was published under before.
  Readers ignore one that is not listed, and customers spend only at listed
  hotkeys, since anyone can publish the url.
  Without `publishers`, any may point to it.
* An `http` service is an HTTP API at `endpoint`.
* A `gamma` payment accepts the subnet's [gamma](/docs/guides/gamma), spent at
  the service's hotkey. `balance` is the url where a hotkey reads its balance
  at the service.
* An [`x402`](https://www.x402.org) payment states its price and payee in each
  HTTP 402 response.
* Readers skip service and payment types they do not know.
* `name`, `description` and `signature` are optional, and so is `publishers`
  without a `gamma` payment. Readers skip fields they do not know.

Serve the document with `Access-Control-Allow-Origin: *`, so that web apps can
read it.

A `gamma` payment with `"signature": "btauth/1"` accepts
[signed requests](/docs/guides/signed-requests). The hotkey that spent signs
each request, with the service's hotkey as the receiver, and the service
charges what that hotkey spent at it. Without `signature`, the service's own
interface says how a request names its hotkey.

## Accepting gamma [#accepting-gamma]

A service keeps each hotkey's credit itself and sends no transaction. At a
finalized block it reads [`gamma-spent`](/docs/query/gamma-spent) for the hotkey
at each of its hotkeys in `publishers`, and credits what each total has grown by
since its last read. Dissolving the subnet clears the totals and the gamma, so a
service stores the subnet's `RegisteredSubnetCounter` with each total and starts
from zero when the counter has grown. The totals can be cleared in the block
that removes the subnet, which emits `NetworkRemoved`, so a service takes the
subnet's final totals as those of the block before plus that block's
`GammaSpent` events at its hotkeys.
