Integrate with MCID

API keys and server access

How a project authenticates with its API key, how an admin grants access per server, and what each 401 and 403 means.

A project talks to MCID with an API key. The key says which project is calling. What the project may do is decided separately, per Discord server, by an admin. This page explains both halves and what each error means.

The key

A key looks like this:

pk_66db96db.3aebaa1aac557ba0f597955dfc2e706a05b9dedc79ad26b8b3859417bfebe91d
  • pk_66db96db is the prefix, pk_ and 8 hex characters. MCID uses it to find your project, and the admin panel shows it so a key can be recognised.
  • The part after the dot is the secret, 64 hex characters.

MCID stores the prefix and a SHA-256 hash of the secret, never the secret itself. Nobody can read a key back, including admins. The full key is shown once, when an admin creates the project or regenerates the key, so store it straight away.

Send the key

Put it in the X-API-Key header:

curl -s "$MCID_URL/servers/$MCID_SERVER_ID/members?limit=1" \
  -H "X-API-Key: $MCID_API_KEY"

The guard also accepts the key as Authorization: Bearer <key>, which is what the Swagger page uses. It looks in this order and uses the first it finds:

  1. Authorization: Bearer <key>
  2. X-API-Key: <key>
  3. an apiKey query parameter

Prefer X-API-Key. The query form is accepted by the guard, but most endpoints reject unknown query parameters, so ?apiKey= returns a 400 property apiKey should not exist on, for example, the members list. A key in a URL also ends up in logs. If your HTTP client already sets an Authorization header, MCID reads that as the key and ignores X-API-Key.

Send the key from your backend only. It must never reach a browser. Browsers use the member's session token or the SSO cookie, as described in Login with Micro Club. Cross-origin browser calls can carry the X-API-Key header, but that does not make it safe to ship the key to one.

A key without a redirect URI

The admin panel creates a project in two steps: the project and its key, then a redirect URI. The second step is optional (Skip for now). A project that only calls the API with its key, for example a server-side read such as GET /api/identities, never signs members in, so it has no use for a redirect URI.

A project with no redirect URI works for every key-authenticated call. The login endpoints (/auth/authorize, /auth/sso/authorize) answer invalid_redirect_uri for it, so no member can sign in through that project until an admin adds an address with PATCH /admin/projects/:id/redirect-uri.

Rotate and revoke

An admin can act on a key in the admin panel:

ActionEffect
RegenerateCreates a new key and invalidates the old one at once. MCID also clears the cached copy of the old key, so it stops working immediately instead of when a cache expires. The new key is shown once.
RevokeMarks the project inactive. Its key is rejected with Invalid API key until an admin restores it.
RestoreReverses a revoke.

The new key exists only after the regeneration, and the old one stops at that moment. Plan the swap: regenerate, copy the key, update your service's configuration, and expect a short gap until it runs with the new value.

Access: servers, operations and scopes

A valid key proves who you are. It grants nothing on its own. An admin gives a project access to each Discord server separately. Every project and server pair has:

  • Operations, what the project may do on that server:
OperationDefaultAllows
READallowedReading members, channels and permissions of the server.
SEND_MESSAGESnot allowedPosting messages to channels.
MANAGE_WEBHOOKSnot allowedManaging Discord webhooks on the server.
  • Scopes, which kinds of member data it may read:
ScopeUsed by
read_membersThe member endpoints under /api/servers/{serverId}/members.
check_permissionsThe permission endpoints under /api/permissions: POST /check, POST /check-batch and GET /{serverId}/{discordId}.
manage_inbound_webhooksPOST /api/project/inbound-webhooks, PATCH /api/project/inbound-webhooks/{id} and POST /api/project/inbound-webhooks/{id}/rotate-secret, with the server named in the serverId query parameter.
read_inbound_submissionsGET /api/project/inbound-webhooks/{id}/submissions, with the server named in the serverId query parameter.
manage_rolesPOST /api/servers/{serverId}/roles, and PUT and DELETE /api/servers/{serverId}/members/{discordId}/roles/{roleId}. A project grants or removes only the roles it created, never a root role or one at or above the bot's highest role; a refused attempt answers 403 and is audited. Creating a role under a name the project already used on that server returns that role, so a retry never creates a second one.
manage_channelsPOST /api/servers/{serverId}/categories, POST /api/servers/{serverId}/channels and POST /api/servers/{serverId}/channels/{channelId}/invites. A project creates categories, text or voice channels (optionally under a category of the same server, otherwise 404), and invites (optional maxAgeSeconds, maxUses) for a channel of a server it may access. The category and channel routes require an Idempotency-Key header and answer 404 for a server the project is not mapped to; the invite route takes the header as an option. A retry with the same key gets the same category, channel or invite back. Each creation is audited. When the bot lacks the Discord permission, the API answers 502.
manage_eventsPOST /api/servers/{serverId}/scheduled-events, and PATCH and DELETE /api/servers/{serverId}/scheduled-events/{eventId}. A project creates scheduled events (external location or voice/stage channel) and can update or delete only the events it created; attempts to modify other events answer 404.

When an admin grants access without choosing scopes, the project gets all of them except the opt-in scopes (manage_roles, manage_channels, manage_events), which an admin has to choose explicitly.

MCID enforces access when the request names a server in its path, in its query string or, for POST /api/permissions/check and POST /api/permissions/check-batch, in its JSON body. It checks, in order, that the server exists and is active, that the project may perform the operation the endpoint needs, and that the project holds the scope the endpoint needs. A server ID must be 17 to 20 digits.

Idempotency keys

These project writes accept an Idempotency-Key header of 1 to 128 characters, so a retry never repeats the Discord action:

  • POST /api/servers/{serverId}/roles
  • PUT and DELETE /api/servers/{serverId}/members/{discordId}/roles/{roleId}
  • POST /api/servers/{serverId}/channels/{channelId}/webhooks
  • POST /api/webhooks/{webhookId}/execute
  • POST /api/servers/{serverId}/channels/{channelId}/invites
  • POST /api/servers/{serverId}/categories and POST /api/servers/{serverId}/channels, which require it
POST /api/servers/900000000000000001/roles
X-API-Key: mc_live_...
Idempotency-Key: 9a7b52f1-6782-4112-9c3f-7231459a43a1
Content-Type: application/json

{
  "name": "Hackathon Staff"
}
  • A request without the header runs as usual, except on the routes that require it, where it answers 400. An empty header or one longer than 128 characters answers 400.
  • A repeat of the same request with the same key does not run again. It answers the stored status and body with the header Idempotent-Replayed: true.
  • The same key with a different method, path, query or body answers 409.
  • A request that arrives while the first one with its key is still running answers 409 and does not run. A key that stays in progress for more than 5 minutes belongs to a request that died, so the next request with that key takes it over and runs.
  • When the first request fails with an error, MCID forgets the key, so the client can fix the request and retry with the same key.
  • Keys belong to one project, so two projects can use the same key. MCID keeps a key for 24 hours, and an hourly job deletes expired ones.

MCID stores the keys in PostgreSQL rather than Redis. A key must survive a Redis outage, because the API falls back to the database when Redis is down, and a lost key would let a retry repeat a Discord write. The unique index on the project and the key also makes the reservation atomic across API instances.

401, 403 and the others

401 means MCID does not know who you are. 403 means it does, and you are not allowed. These are the messages, each produced by running the request:

StatusmessageMeaning and fix
401API key is requiredNo key was sent. Add the X-API-Key header.
401Invalid API keyThe key matches no active project: wrong, regenerated, or the project is revoked.
403Project is not allowed to perform READ on server 900000000000000002The project has no access to that server, or lacks that operation. Ask an admin to grant it.
403Insufficient scope: 'read_members' is requiredThe project has the server but not this scope. Ask an admin to add the scope.
403Server not found or inactiveThe server is not known to MCID or an admin disabled it.
403Server is disabledA request that names a server in its body or query refers to one that is unknown or disabled.
404Server not foundOn POST /api/servers/{serverId}/categories and /channels, which never reveal whether a server exists: the server is unknown, disabled, or not mapped to the project.
400Invalid server ID formatThe server ID is not 17 to 20 digits.
400Server ID is required when scope validation is neededThe endpoint needs a scope but the request names no server.

All of them have the same JSON shape, described in Conventions.

Keeping keys safe

  • Keep the key in a secret manager or an environment variable. Never commit it.
  • Give each project its own key, so one leak can be revoked alone.
  • Ask for the smallest access: the servers, operations and scopes the project really uses.
  • If a key may have leaked, ask an admin to regenerate it. Revoking disables the whole project, regenerating only replaces the secret.

For every endpoint and its required access, see the Project API reference.

Source: apps/api/src/common/guards/api-key.guard.ts, apps/api/src/common/utils/auth.util.ts, apps/api/src/common/utils/api-key.util.ts, apps/api/src/modules/projects/projects.service.ts, apps/api/src/modules/servers/server.guard.ts, apps/api/src/modules/permissions/permissions.controller.ts, apps/api/src/modules/idempotency/idempotency.interceptor.ts.

On this page