Skip to main content

Call the API as a service account

An integration that calls the BeaconTower API on its own, with no person signing in, uses a service account: a client id and secret it trades for a token with the client-credentials grant.

You need a tenant that btk3s up brought up with btk3s 0.7.6 or later; earlier tenants do not route /api/v1/service-accounts. On an older one, run btk3s update and then btk3s up (see Upgrade). The commands below run as the tenant's default user, which may manage users in its organization.

1. Create the service account​

Find the id of the organization the integration works in:

btk3s api /organizations

Create the account in it:

btk3s api -X POST /service-accounts \
-d '{"organizationId":"<organization id>","name":"my-integration"}'

The answer holds id, clientId and clientSecret. The API shows the secret only in this answer. Store it where your integration reads its secrets; if you lose it, rotate it (below).

2. Get a token​

Keep the secret off the command line, where it would land in your shell history and show in ps: put client_secret=<clientSecret> in a file only you can read, here sa.secret, and let curl read it.

curl -s https://auth.<domain>/realms/beacontower/protocol/openid-connect/token \
-d grant_type=client_credentials \
-d client_id=<clientId> \
-d @sa.secret

access_token in the answer is the bearer token for https://<domain>/api/v1/.... On a local tenant, curl needs the tenant's CA: add --cacert ~/.local/state/btk3s/ca-bundle.pem, or install it into your system's trust the way btk3s hosts trust prints.

3. Give it a role​

A new service account is a member of its organization: it reads what members read. To let it change things, give it a role the way you would a user, with its id as the subject. The role replaces its membership; a manager role such as asset_manager can still read the organization. For example, to let it manage the organization's assets:

btk3s api -X POST /relations -d '[{"subjectType":"user","subjectId":"<id>",
"relation":"asset_manager","resourceType":"organization","resourceId":"<organization id>"}]'

Rotate or remove it​

btk3s api -X POST /service-accounts/<id>/secret   # a new secret; the old one stops working
btk3s api -X DELETE /service-accounts/<id>
btk3s api /service-accounts # the ones you may see, never a secret

See also​