# Self-host the Minekube API and relay

> Run your own Minekube API and relay - the API as a Cloudflare Worker or locally in workerd, the relay as a container that gets its own certificate - from the open source repository.

> **Preview: Connect in the Minekube plugin:**
> This page describes the Minekube API and relay behind networks in the Minekube plugin, the next generation of Connect. They are open source under the MIT license, in the [Minekube repository](https://github.com/minekube/minekube).

Minekube runs the Minekube API and relays for every network. You can also run your own. Each part is in the open source repository, and Minekube deploys the same code.

## The Minekube API

The Minekube API is an Effect Cloudflare Worker in `apps/api`. It keeps its state in Durable Objects: the registry, where `Directory` maps every key to its network and `Network` holds one network's instances, labels, versions and deployments, and `Accounts`, which holds accounts and their sessions. Deployed bundles go to the R2 bucket `Bundles`.

**Locally:** in a checkout, `mk api` runs it in workerd on `http://localhost:4776` (`--port` for another), with local state and no Cloudflare account. It is the API every `mk` command and every project uses unless `--api`, `MINEKUBE_API` or `api` in `vite.config.ts` names another. `mk server --network` starts it for the dev network.

**On Cloudflare:** `infra/network.run.ts` deploys it with [Alchemy](https://alchemy.run), at `api.minekube.com` (staging: `api.staging.minekube.com`), with DNS for `*.<domain>`, where players reach networks. Your own deployment sets its own host names. The Worker reads three values:

| Variable | What |
| --- | --- |
| `MINEKUBE_NETWORK_DOMAIN` | Where networks are reached: `<network>.<domain>` |
| `MINEKUBE_API_DOMAIN` | The API's own host name, such as `api.example.com` |
| `MINEKUBE_OPERATORS` | The issuers whose short-lived tokens may act as a relay or an admin |
| `MINEKUBE_LOGIN_HUB` | Where players get a login code, such as `play.example.com`, for `mk login` to say. Optional. |

There are no static tokens. A relay or an admin shows a short-lived token of an issuer in `MINEKUBE_OPERATORS`, such as a Kubernetes service account token.

## The relay

The relay is a Rust program in `apps/relay`, with a Dockerfile. CI publishes its image as `ghcr.io/minekube/minekube-relay:main`. Each option is a flag and an environment variable:

| Variable | Default | What |
| --- | --- | --- |
| `MINEKUBE_RELAY_LISTEN` | `0.0.0.0:25565` | Where Java players connect |
| `MINEKUBE_API` | | The Minekube API to look up routes in and announce the relay to |
| `MINEKUBE_API_TOKEN_FILE` | | A file with a short-lived token the API trusts for relays, read before each request |
| `MINEKUBE_NETWORK_DOMAIN` | `localhost` | Networks are reached at `<network>.<domain>`; other hosts are refused |
| `MINEKUBE_RELAY_REGION` | `local` | Where the relay runs, a label on the instances homed here |
| `MINEKUBE_RELAY_KEY` | `relay.key` | The relay's key, made on first start. Connectors recognize the relay by it. |
| `MINEKUBE_RELAY_TLS_DOMAIN` | | Serve the iroh relay over HTTPS for this name, with a certificate from Let's Encrypt that the relay renews itself. Needs port 443. |
| `MINEKUBE_RELAY_TLS_CACHE` | `acme` | Where the certificate is kept across restarts |

Locally, `mk relay` runs one against the local API. Point your servers' `connector.json` at your relay in `relays` and at your API in `directory`.

## Related

- [Connector, relay and API](https://minekube.com/guide/networks/connectivity): what each part does and why.
- [Open source](https://minekube.com/games/platform/open-source): the license and what is in the repository.
