Call another service with a platform token
Sometimes a function must call another first-party application that accepts FlowCatalyst access tokens. The function holds no client id or secret: it asks its host for a token and sends it as a bearer.
Whose identity it is
Section titled “Whose identity it is”The token belongs to the service account linked to the function’s application. Always that one:
there is no per-function service account and no override, and the function’s optional webhook-verifier
account (see Declare endpoints, auth, subscriptions and
schedules) is not used here. The token is what that account’s
client_credentials grant would give: the same claims, the account’s flattened permissions as scope,
and the same lifetime as an ordinary access token. Give the application’s service account the roles the
target needs and nothing more. See Tokens.
The call is refused when the application has no linked service account, when the account or its principal is deactivated, or when the function did not declare the capability.
Declare and use it
Section titled “Declare and use it”func init() { fn.UsesPlatformToken() fn.HTTPAllow("other-app.example.com") fn.Platform("POST /call-other", onCallOther)}
func onCallOther(w http.ResponseWriter, r *http.Request) { token, err := fn.PlatformToken(r.Context()) // fn.Refresh() skips the cache if err != nil { if errors.Is(err, fn.ErrRetryable) { fn.Retry(w, 5*time.Second) return } fn.Reject(w, "platform token refused") return } req, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://other-app.example.com/api/ping", nil) req.Header.Set("Authorization", "Bearer "+token) resp, err := fn.HTTPClient().Do(req) // ...}fn.usesPlatformToken();fn.httpAllow("other-app.example.com");
fn.platform("POST /call-other", async () => { try { const token = await fn.platformToken(); // { refresh: true } skips the cache const res = await fn.fetch("https://other-app.example.com/api/ping", { headers: { authorization: `Bearer ${token}` }, }); return fn.status(res.status); } catch (err) { if (isRetryable(err)) return fn.retry(5); return fn.reject("platform token refused"); }});flowcatalyst_fn::export!(|app: &mut App| { app.platform("POST /call-other", on_call_other) .platform_token() .http_allow("other-app.example.com");});
fn on_call_other(_req: &Request, ctx: &Context) -> Result<Response, Error> { match ctx.platform_token() { // ctx.refresh_platform_token() skips the cache Ok(token) => { /* send as a bearer; never log it */ Ok(Response::new(200)) } Err(e) if e.is_retryable() => Ok(Response::new(429).with_header("Retry-After", "5")), Err(_) => Ok(Response::new(422).with_header("FlowCatalyst-Outcome", "reject")), }}Declare "platformToken": true in function.json (and the target’s host in httpAllow), then:
try { String token = ctx.platformToken(); // ctx.platformToken(true) skips the cache // send as "Authorization: Bearer " + token using ctx.http(); never log it return Result.ack();} catch (PlatformTokenException e) { return e.retryable() ? Result.retry(Duration.ofSeconds(5)) : Result.reject("platform token refused");}The target host must also be in HTTPAllow: the token does not widen what the function may reach.
Caching and refresh
Section titled “Caching and refresh”The host caches the token per function and renews it shortly before it expires (it serves from cache while more than the larger of 60 seconds and 10% of the lifetime remains). Concurrent calls for the same function share one fetch, and a failed fetch is not cached. So call it per request rather than keeping the token yourself.
Force a fresh token (Refresh(), { refresh: true }, refresh_platform_token(),
platformToken(true)) only when the target answered 401 with the token you were given, for example
because the account’s roles changed.
Errors
Section titled “Errors”| Code | Meaning | Retryable |
|---|---|---|
NOT_DECLARED |
the version does not declare the capability | no |
NOT_ALLOWED |
the platform refused: not declared there, the application has no linked service account, or the account is inactive. The message carries the platform’s code | no |
UNAVAILABLE |
the platform was unreachable, answered 5xx, 429 or 408 | yes |
DEADLINE |
the call’s deadline passed | no |
A runner that predates the capability answers UNKNOWN_OP; the SDKs turn that into an error saying the
runner does not support platform tokens, not a crash.
- Never log the token, at any level. Do not return it to a caller either.
- Every mint is audited against the service account (operation
TOKEN_MINTED_FOR_FUNCTION); the token itself is never recorded. - The token is not narrowed by audience or scope per target. It carries the account’s full permissions.
- The target application must accept FlowCatalyst tokens, verified against the platform’s signing keys.