Run background jobs
A job that runs with no user signed in, such as a nightly import or one that builds a shared word list, signs in as an app identity: your application's own identity. App identities and sign-in explains how it differs from a user's sign-in. Run the commands in your app's directory, after multipaas init, which Build on the generated API covers.
Create the identity
The app-identity commands in this guide need CLI 0.9.0 or later, and CLI 1.2.0 or later when the identity belongs outside the default environment. That includes a project whose test app-tenant is outside it: on CLI 1.2.0 the identity then defaults to environmentId in multipaas.json, and an older CLI creates it in the default one instead. Upgrading adds no environmentId to a multipaas.json an older init wrote, so without --env the app-identity commands, and the role commands with --app-wide, use the default environment. To use another, run init again as Environments describes, adding --api-version API_VERSION with the file's apiVersion to keep the pin (a bare init pins the newest version), or pass --env ENV_ID on those commands. --env on these commands, and its default from multipaas.json, need CLI 1.2.0 or later. Check multipaas --version first.
multipaas app-identity create --name NAME
It prints the identity's client ID and its secret. The secret is shown once. Store it as a secret in the job's environment, never in your repository.
You need permission to manage the application's sign-in, held in the environment the identity belongs to: the one you pass as --env, else environmentId in multipaas.json when that file is for the same application, else the default. With a CLI older than 1.2.0, that is the default environment.
Give it a role
An app identity can do only what its roles allow, like a person. These commands need CLI 0.11.0 or later, and --env on app-identity role needs CLI 1.2.0 or later.
List the application's roles with
multipaas roles list. Add--tenant APP_TENANT_IDto include that app-tenant's own roles.If none fits, list the permissions a role can carry with
multipaas roles permissions, then declare a role:multipaas roles create --key KEY --name NAME --permission PERMISSIONRepeat
--permissionfor each permission. Write a permission for one entity asPERMISSION=ENTITY_ID.To change a role that already exists, use
multipaas roles update --role KEY --add-permission PERMISSION --remove-permission PERMISSION, repeating either flag. It changes application roles only and needs CLI 1.1.0 or later.Give the role to the identity:
multipaas app-identity role add CLIENT_ID --role KEYThat gives it in the app-tenant in
multipaas.json. Add--tenant APP_TENANT_IDfor another app-tenant, or--app-widefor the whole application. An app identity belongs to one environment, the one it was created in (multipaas app-identity create --env ENV_ID), and it holds roles only there. Without--app-wide, the command works at an app-tenant, and only an app-tenant in the identity's environment finds the identity; for an identity in another environment, the test app-tenant inmultipaas.jsonor a--tenantoutside that environment is refused as not found. Use--tenantwith an app-tenant of the identity's environment, or--app-wide --env ENV_IDto give the role across the application in that environment.--envworks only with--app-wide, and so does its default: without--env,--app-wideusesenvironmentIdinmultipaas.jsonwhen that file is for the same application, or the default environment otherwise.
multipaas app-identity role list CLIENT_ID lists the roles the identity holds in a scope, and multipaas app-identity role remove CLIENT_ID --role KEY takes one back. Both work at an app-tenant like role add, so the same limits apply: use --tenant of an app-tenant in the identity's environment, or --app-wide --env ENV_ID.
Which role a token needs:
- A token for one app-tenant needs a role in that app-tenant, or across the application in the identity's environment. The app-tenant must be in that environment.
- An app-wide token works in the identity's environment and needs a role across the application there.
- Writing app-wide records takes an app-wide token and a role across the application in the identity's environment with
record.app_wide.createorrecord.app_wide.edit. - Reading app-wide records with an app-tenant token takes a role with
record.app_wide.viewheld in that app-tenant or across the application in its environment, or a role withrecord.app_wide.editorrecord.app_wide.manageheld across the application in its environment.
Get a token
POST, form-encoded, to the token endpoint that multipaas init --json printed as endpoints.token, with grant_type=client_credentials. Identify the identity one way, not both:
- the form fields
client_idandclient_secret, or - HTTP Basic, with the client ID as the user name and the secret as the password.
Add app_tenant=APP_TENANT_ID for a token that acts in one app-tenant. Leave it out for an app-wide token.
printf 'user = "%s:%s"\n' "$CLIENT_ID" "$CLIENT_SECRET" | curl -s -K - -X POST "$TOKEN_ENDPOINT" -d grant_type=client_credentials -d app_tenant="$APP_TENANT_ID"
This passes the ID and secret on standard input, so they stay off the command line. In the job's own code, send them from its HTTP client.
The answer holds access_token, token_type and expires_in. The token lasts 3600 seconds. There is no refresh token: when the token expires, ask for a new one the same way.
| Error | Status | Meaning |
|---|---|---|
invalid_client |
401 | The client ID or secret is wrong, the secret was revoked, the identity was deleted, or its environment was deleted and erased. It doesn't say which. |
invalid_target |
400 | The identity holds no role in the scope asked for, or app_tenant isn't an active app-tenant of this application in the identity's environment, or its environment was deleted and not yet erased. |
invalid_request |
400 | The request is malformed, for example it sent HTTP Basic and a client_secret form field together. |
Call the API
Send the token as Authorization: Bearer <access_token>, with X-MultiPaaS-App-Api-Version set to the apiVersion in multipaas.json, as in Build on the generated API.
The token decides what the job reaches:
- An app-tenant token reads and writes that app-tenant's records, as its roles allow, and reads the app-wide records in its app-tenant's environment that its roles let it view.
- An app-wide token reads and writes its environment's app-wide records only, as its roles allow, and sees no app-tenant's records. It needs an API version built after app-wide records shipped. An older version answers
403 insufficient_scope.
What an app identity can't do
An app identity's token works only on your application's generated API: its records and the me query. It can't:
- create, change or archive app-tenants, or manage their members or invitations
- declare roles, or give them to anyone, itself included
- register OAuth clients, or manage app identities and their secrets
- change the model
The multipaas CLI and the MultiPaaS web app don't accept its token. A builder signed in to MultiPaaS does these instead.
Rotate the secret
- Add a secret with
multipaas app-identity secret add CLIENT_ID. It prints the new secret once. - Deploy the job with the new secret.
- Find the old secret's ID with
multipaas app-identity secret list CLIENT_ID, and revoke it withmultipaas app-identity secret revoke SECRET_ID.
These commands find the identity only in its own environment, so pass the identity's --env ENV_ID unless multipaas.json already names that environment. So does app-identity delete, which needs CLI 1.9.0 or later; app-identity retire is the deprecated name and still works.
A revoked secret gets no new tokens. Tokens already issued with it keep working until they expire. To stop an identity at once, delete it with multipaas app-identity delete CLIENT_ID. Its tokens stop working immediately.
More
multipaas app-identity --helpmultipaas roles --help- Using Claude Code? The MultiPaaS plugin does these steps for you.