Server integration

Service tokens

Mint an appcenter:service token with client_credentials, cache it properly and understand the two refusals.

Your backend holds a confidential_service client. It mints short-lived tokens with the client_credentials grant and calls the Application API. There is no user involved: a service token carries no sub and cannot be used for anything a person sees.

Mint one

token.sh
curl -X POST https://apps-api.versified.com.gh/oidc/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "client_id=ledgerly-backend" \
  -d "client_secret=$LEDGERLY_CLIENT_SECRET" \
  -d "scope=appcenter:service" \
  -d "resource=urn:versified:api:appcenter"

# {
#   "access_token": "eyJ...",
#   "token_type": "Bearer",
#   "expires_in": 900,
#   "scope": "appcenter:service"
# }

Practical notes

  • Cache the token until 60 seconds before expiry. One request per token is wasteful and needlessly loads the provider.
  • The token is RS256. When you are the client, treat it as opaque — App Center verifies it, not you.
  • A user token is refused. /app/* is service-only: a browser session token gets 403 service_token_required. Never proxy a person's token into the Application API.
  • The client must be bound to an application. Register it under an application; an unbound client gets 403 app_not_bound.
token-cache.ts
let cached: { token: string; expiresAt: number } | null = null;

export async function serviceToken(): Promise<string> {
  if (cached && cached.expiresAt - 60_000 > Date.now()) return cached.token;

  const response = await fetch(`${ISSUER}/token`, {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: new URLSearchParams({
      grant_type: "client_credentials",
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      scope: "appcenter:service",
      resource: "urn:versified:api:appcenter",
    }),
  });

  if (!response.ok) {
    throw new Error(`token request failed: ${response.status}`);
  }

  const body = await response.json();
  cached = {
    token: body.access_token,
    expiresAt: Date.now() + body.expires_in * 1000,
  };
  return cached.token;
}

Prove the credential at boot

Call GET /appcenter/v3/app/me once on startup. It answers with the application your client is bound to, which turns a misconfigured environment variable into an obvious log line instead of a mystery 403 an hour later.

Service tokens in the other direction

App Center also mints service tokens — for retill:service and onekitchen:service — when it calls *your* capability endpoints. The difference is who holds the secret, not the shape of the token. Verify it the same way: signature, issuer, then audience.

Something here out of date or unclear?

The platform team answers integration questions directly.

support@versified.com.gh
32 pages in this documentation