Skip to Content
MineDuels

Authentication

API keys, the three scopes, and how to send a key.

Every endpoint except GET / needs an API key. A key has one scope. A request with no key or a wrong key is refused.

Scopes

Scopes are ordered. A higher scope may call everything a lower one can.

ScopeMay call
readEvery GET except GET /config/{file}
writeEverything in read, plus POST /drain/start, POST /drain/stop, POST /reload
adminEverything in write, plus GET /config/{file}, PUT /config/{file}, PATCH /config

Give each tool the lowest scope it needs. A dashboard needs read. A deploy script that drains servers needs write. Only the tool that edits configs needs admin.

Creating and removing keys

In the console or in game, with mineduels.admin:

/md web key create dashboard read
/md web key create deploy write
/md web key create panel admin
/md web key list
/md web key remove dashboard
dashboard read
deploy write
panel admin

Rules for a name: 1 to 32 letters, digits, _ or -. Names are unique and not case sensitive.

The key is printed once. It looks like mdk_ followed by 43 random characters. Only its SHA-256 hash is saved in webapi.yml, so a lost key cannot be recovered: remove it and create a new one.

keys:
  - name: dashboard
    scope: read
    hash: 3b1f0e...c7d2

A removed key stops working at once. There is no restart.

Sending the key

Use the Authorization header:

curl -H "Authorization: Bearer mdk_Qm9k1s2yQeW3uYb0n8kz1V5lGq7cAa4m9H2xTt6RjLw" \
  http://127.0.0.1:8765/v1/kits

Or the X-API-Key header, for tools that cannot set Authorization:

curl -H "X-API-Key: mdk_Qm9k1s2yQeW3uYb0n8kz1V5lGq7cAa4m9H2xTt6RjLw" \
  http://127.0.0.1:8765/v1/kits

A key in the URL query is not accepted, so it never ends up in access logs.

What you get when it fails

No key:

curl -i http://127.0.0.1:8765/v1/status
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer
Content-Type: application/json; charset=utf-8

{"ok":false,"error":{"code":"unauthorized","message":"send the key as: Authorization: Bearer <key>"}}

A wrong key gives 401 with "the key is not valid". A key whose scope is too low gives 403:

curl -i -X POST -H "Authorization: Bearer $READ_KEY" http://127.0.0.1:8765/v1/drain/start
HTTP/1.1 403 Forbidden

{"ok":false,"error":{"code":"forbidden","message":"this key has the read scope; write is needed"}}

Wrong keys are throttled per address. After ten wrong keys in a row, further wrong keys get 429 with Retry-After: 5.

Keeping keys safe

  • Keep host: 127.0.0.1 unless you need remote access, and use HTTPS when you do.
  • Store keys in environment variables or a secret store, not in scripts or Git.
  • Use one key per tool, so you can remove one without breaking the others.
  • Keys give no access to the cross-server secret, the Redis password or the database password. Those are hidden and cannot be edited through the API.
  • webapi.yml is never listed or editable through /config.

Examples

Keep the key in an environment variable:

export MD_KEY="mdk_Qm9k1s2yQeW3uYb0n8kz1V5lGq7cAa4m9H2xTt6RjLw"
export MD_URL="http://127.0.0.1:8765/v1"
curl -s -H "Authorization: Bearer $MD_KEY" "$MD_URL/status"

Node.js:

const res = await fetch(`${process.env.MD_URL}/status`, {
  headers: { Authorization: `Bearer ${process.env.MD_KEY}` },
});
console.log(await res.json());

Python:

import os, requests

r = requests.get(f"{os.environ['MD_URL']}/status",
                 headers={"Authorization": f"Bearer {os.environ['MD_KEY']}"})
print(r.json())

On this page