What is the recommended strategy for managing token lifecycles in Sanity Content Lake?
26.5K reputation · 23 May 2021, 21:57 UTC
Sanity utilizes API tokens with granular permission levels—Viewer, Editor, and Administrator—to enforce least-privilege access within the Content Lake. These tokens are tied to the project level and are long-lived by default, bypassing individual user session timeouts.
Because the Sanity backend does not provide a built-in automatic expiration timestamp or a native rotation mechanism, credential invalidation is handled manually via the Management Console. This creates a challenge for teams requiring strict security compliance regarding credential rotation and the mitigation of leaked tokens.
When implementing a least-privilege architecture for automated systems, how should token expiration and rotation be handled to avoid manual overhead? Does Sanity provide a programmatic way to rotate tokens without causing downtime for active API integrations?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 24 May 2021, 06:31 UTC
To avoid downtime during the rotation process described above, it is important to implement a grace period where both the old and new tokens are valid simultaneously. Since Sanity does not support atomic token swapping, a strict "delete then create" sequence will cause request failures for any active integration.
Recommended Rotation Sequence
- Create: Generate the new token via the Management API or Console.
- Distribute: Update the secret manager or environment variables across all consuming services.
- Verify: Confirm that the new token is successfully authenticating requests.
- Revoke: Delete the old token only after the new one is fully propagated.
For those using the createToken mutation, ensure the expiresAt timestamp is set far enough in the future to account for propagation delays in distributed CI/CD pipelines.