On this page
1. Create a token#
- Open profile → API tokens → New token.
- Set Name, Role (Viewer or Admin) and Expires in, click Create token.
- Copy the token (shown once) and export:
bash
export PRINCIPAL_URL=https://principal.example.com export AUTH="Authorization: Bearer prn_pat_..."
Use an organization token for shared scripts (Authentication).
2. List targets#
bash
curl -sH "$AUTH" "$PRINCIPAL_URL/api/v2/targets?limit=5"Returns items, has_more, next_cursor (Lists). Copy warehouse-staging’s id.
3. Find group and template#
- Look up both:
bash
curl -sH "$AUTH" "$PRINCIPAL_URL/api/v2/identities?kind=group&search=analytics" curl -sH "$AUTH" "$PRINCIPAL_URL/api/v2/access-templates/warehouse-read-only" - On
404, create the template (Rules and templates):bashcurl -sX POST "$PRINCIPAL_URL/api/v2/access-templates" \ -H "$AUTH" -H "Content-Type: application/json" \ -d '{"name":"warehouse-read-only","engine":"postgres", "body":{"targeting":{"discovery":{"database":"app"}}, "grants":[{"target":"public.*","privileges":["SELECT"]}]}}'
4. Create, preview and apply#
- Create the rule:
bash
curl -sX POST "$PRINCIPAL_URL/api/v2/policies" \ -H "$AUTH" -H "Content-Type: application/json" \ -H "Idempotency-Key: $(uuidgen)" \ -d '{"subject_id":"<group id>","target_id":"<target id>", "template_name":"warehouse-read-only"}' - Preview, then apply:
bash
curl -sX POST -H "$AUTH" "$PRINCIPAL_URL/api/v2/policies/<rule id>/plan/diff" curl -sX POST -H "$AUTH" "$PRINCIPAL_URL/api/v2/policies/<rule id>/apply"
Returns 202 with job_id. Track via GET /apply-runs or GET /admin (Async).
5. Handle errors#
json
{"status":409,"code":"policy.access_held_by_group_rule","detail":"..."}Branch on code, not text (Errors).