Machine clients and M2M tokens
Credentials for Coderunners, servers and CI to call Groundfloor without a person signed in.
An M2M (machine-to-machine) token lets code call Groundfloor when no person is signed in: your Coderunners, a CI pipeline, a server of your own. You create a machine client with a secret, and the code exchanges that secret for a short-lived token.
What a machine client can do
A machine client acts like a Writer in its workspace:
- Read and write every Data Vault collection and every file. Collection grants on Site access don't apply to it.
- Read feature flags, but not change them.
- Read secrets only if you turn on Can read secrets. It can never change or delete secrets.
- Create, change, deploy, stop and delete Coderunners only if you turn on Can deploy services. Without it, those calls return
403withX-Groundfloor-Reason: m2m_scope_required.
It can't manage members, apps, app sign-in, access grants or managed databases, publish Shell app releases, or get a Coderunner developer token, whatever you turn on.
Create a client
Open Machine clients
In the Client Portal, go to Administer → Access and open the Machine clients tab, in the workspace you want. The old M2M tokens page takes you there.
Create it
Click New client and give it a name. Client ID is optional; it's made from the name when you leave it empty. Turn on Can read secrets only if its code needs secrets. Click Create, then copy the secret: you can't see it again. Rotate replaces it later.
Use it
- In a Coderunner: attach the client and deploy. A Coderunner can't use your data or files until a client is attached.
- In CI or on your own server: set
GROUNDFLOOR_M2M_CLIENT_IDandGROUNDFLOOR_M2M_CLIENT_SECRET, then rungf m2m token, or call the API.
Use one client per Coderunner, and a separate one for CI, so you can rotate and audit each one on its own. Each client's row shows its switches and, under Used by, the Coderunners it's attached to and their environments.
Give a Coderunner a machine client
Create a client for it
Create a client for this Coderunner alone, as above. Leave Can deploy services off: code running in the Coderunner gets everything the client can do.
Attach it
Open the Coderunner and, on the Machine client card, choose the client under Client and click Save. Only a workspace Admin or Owner can attach one. If the client can deploy services or read secrets, the card warns you and asks you to click Attach anyway. If the Coderunner's code came from a machine client with less access, you're asked to confirm the code first.
Deploy
Deploy the Coderunner again. The credentials reach it at deploy time, so a newly attached, changed or rotated client applies only after a deploy.
Read the values
Your code gets these environment variables:
| Variable | Kind | Value |
|---|---|---|
GROUNDFLOOR_M2M_CLIENT_ID | Secret, filled by the platform | The attached client's ID |
GROUNDFLOOR_M2M_CLIENT_SECRET | Secret, filled by the platform | The attached client's secret |
CONTROLPLANE_URL | Environment variable | The Groundfloor API's base URL |
GROUNDFLOOR_WORKSPACE_ID | Environment variable | Your workspace's ID |
You never set the GROUNDFLOOR_ variables yourself: Groundfloor fills them from the client attached to the Coderunner, and you can't use those names for your own variables. The Secrets card on the Coderunner's page lists them as Filled from the attached machine client, next to CONTROLPLANE_URL (Platform setting). It never shows values, and it's hidden while the Coderunner has none. The API lists the same names in secrets and platform_environment, with an empty value meaning filled by the platform. Both secrets reach the container without appearing in the deployment's configuration. Your other secrets are not passed to the container: read them at runtime with this client's token.
Exchange them for a token
Your code calls POST {CONTROLPLANE_URL}/v1/workspaces/{workspace_id}/m2m/token and uses the access_token it gets back. See Get a token.
Get a token
gf m2m token # prints a token; not the same as gf token
curl -H "Authorization: Bearer $(gf m2m token)" \
"https://platform.groundfloor.cloud/v1/workspaces/$GROUNDFLOOR_WORKSPACE_ID/vault/collections"From code, send client_id and client_secret to POST …/m2m/token. The response has access_token and expires_in, in seconds. Keep the token and reuse it:
const base = `${process.env.CONTROLPLANE_URL}/v1/workspaces/${process.env.GROUNDFLOOR_WORKSPACE_ID}`;
let cached; // { token, expiresAt }
async function m2mToken() {
if (cached && Date.now() < cached.expiresAt) return cached.token;
const res = await fetch(`${base}/m2m/token`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
client_id: process.env.GROUNDFLOOR_M2M_CLIENT_ID,
client_secret: process.env.GROUNDFLOOR_M2M_CLIENT_SECRET,
}),
});
if (!res.ok) throw new Error(`token request failed: ${res.status}`); // retry 429 and 401 later
const { access_token, expires_in } = await res.json();
cached = { token: access_token, expiresAt: Date.now() + expires_in * 800 }; // refresh at 80% of its life
return access_token;
}
// const res = await fetch(`${base}/vault/collections`, { headers: { authorization: `Bearer ${await m2mToken()}` } });
module.exports = { m2mToken };import json, os, time, urllib.request
BASE = f"{os.environ['CONTROLPLANE_URL']}/v1/workspaces/{os.environ['GROUNDFLOOR_WORKSPACE_ID']}"
_cached = {"token": None, "expires_at": 0.0}
def m2m_token() -> str:
if _cached["token"] and time.time() < _cached["expires_at"]:
return _cached["token"]
body = json.dumps({
"client_id": os.environ["GROUNDFLOOR_M2M_CLIENT_ID"],
"client_secret": os.environ["GROUNDFLOOR_M2M_CLIENT_SECRET"],
}).encode()
req = urllib.request.Request(f"{BASE}/m2m/token", data=body,
headers={"content-type": "application/json"}, method="POST")
with urllib.request.urlopen(req) as res: # raises on 429 and 401: retry later
data = json.load(res)
_cached["token"] = data["access_token"]
_cached["expires_at"] = time.time() + data["expires_in"] * 0.8 # refresh at 80% of its life
return _cached["token"]
# req = urllib.request.Request(f"{BASE}/vault/collections", headers={"authorization": f"Bearer {m2m_token()}"})Configuration and secrets uses the same helper to read secrets.
Reuse each token until shortly before it expires. Getting a token is limited to 20 a minute per client, and 20 a minute per IP address. Code that gets a new token for every request fails with 429 under real traffic. Treat a 429 or 401 from the token request as something to retry.
Read secrets
Turn on Can read secrets in the client's row on Machine clients, or when you create it. The client can then read every secret in the workspace, in every environment. The change applies to the next token it gets. A Coderunner can then read secrets at runtime; see Configuration and secrets. Deploying doesn't need it.
Deploy from CI
To let a CI pipeline deploy, turn on Can deploy services in its client's row, after creating it, and confirm with Allow deploying. Anyone holding that client's secret can then create, change, deploy, stop and delete every Coderunner in the workspace.
Give Can deploy services to CI pipelines only, never to a client attached to a Coderunner. Code running in that Coderunner could use it to replace its own code or another Coderunner's. The client's row shows a warning while it's both, and attaching such a client asks you to confirm. Turn the switch off, or attach a different client.
Even with Can deploy services, a client can't change the code of a Coderunner that has a workload identity bound, or whose attached client has a switch on that this one doesn't: 403 with X-Groundfloor-Reason: m2m_scope_escalation. The exception is the attached client itself.
Who supplied the code
Groundfloor records who supplied each version of a Coderunner's code: a workspace member, a machine client (with the switches it had then), or, for code uploaded before Groundfloor recorded this, Unknown. A Coderunner's page shows this on the Who supplied the code card, for the deployed and the latest version, with the name of the member who confirmed it, if anyone.
Code from a machine client can't run with more access than that client had when it supplied the code, unless a workspace member confirms it. More access means the Coderunner's attached client has a switch on that the supplier didn't have, or the Coderunner has a workload identity bound. Code supplied by the attached client itself always runs. Unknown code counts as code from a client with no switches on.
When you give a Coderunner more access
Attaching a client, binding a workload identity, or turning on a switch for a client that's attached to Coderunners asks Give this code more access? when the deployed or latest code, or the Git source, came from a machine client with less access. The message names the Coderunners affected. Confirm only if you trust the code they run now: you're vouching for it.
- Confirm and attach or Confirm and save goes ahead and records the deployed version as confirmed by you. Versions that aren't deployed stay unconfirmed.
- A Git source configured by such a client stops deploying on push. The Who supplied the code card and the Git source sheet say Push deploys are paused — a member needs to save the Git source again. Check its settings and click Save to turn push deploys back on.
- Turning a switch off, detaching a client, and changes that give nothing more never ask.
In the API this is 409 with X-Groundfloor-Reason: m2m_code_provenance_confirm_required; send the same request again with confirm_code_provenance: true.
When you deploy
Deploying, starting, promoting or deploying from Git code that can't run with the Coderunner's current access is refused, and nothing is deployed. In the Client Portal, Deploy anyway as me deploys it with you vouching for it; this is recorded in the activity log. You'll find it in the refusal dialog for a deploy or start, on the promote panel for each refused Coderunner, and in the Git source sheet after Deploy now is refused. Deploying from Git this way doesn't turn push deploys back on. Otherwise upload the code again as a workspace member. A machine client can't confirm code. Stopping a Coderunner is never refused.
In the API this is 403 with X-Groundfloor-Reason: m2m_code_provenance on deploy, promote and Git deploys; a member sends confirm_code_provenance: true to go ahead. See Code provenance.
Older code
Code uploaded before Groundfloor recorded who supplied it shows as Unknown. It deploys as before, except on a Coderunner whose attached client has a switch on or that has a workload identity bound. There, upload the code again as a workspace member, or have a member confirm it with Deploy anyway as me. A Git source saved before then needs a member to save it again before push deploys work on such a Coderunner.
Call a Function
A server or another Coderunner calls a Function with an M2M token. gf token is only for trying things out yourself.
Troubleshooting
| Problem | Likely cause | What to do |
|---|---|---|
403 m2m_scope_required when CI deploys | The client doesn't have Can deploy services | Turn it on for the CI client |
403 m2m_scope_escalation | The target Coderunner has a workload identity, or an attached client with more switches | Deploy as a member, or from that Coderunner's own client |
403 m2m_code_provenance on deploy | The code came from a client with less access than the Coderunner has now, or is Unknown | Upload again as a member, or Deploy anyway as me |
409 m2m_code_provenance_confirm_required | Attaching or widening would give existing code more access | Confirm if you trust the code |
| Push deploys stopped | Push deploys are paused after a confirmed change | A member opens Git source and clicks Save |
Next steps
- Configuration and secrets
- M2M API — clients, scopes and rotation from code.