Skip to main content
An API Policy Token is a second kind of organization token, intended for autonomous agents and narrowly scoped automation. You attach an Open Policy Agent policy, written in rego, and Qovery evaluates that policy against every API request the token makes. A request the policy allows then runs with the token’s role, like a regular API Token: organization admin unless you pick a narrower one. What the token can do is what both the policy and the role allow.
Preview: this feature and the policy input contract described below are still evolving and are likely to change. Expect to revisit your policies as the feature matures.
The Console calls them policy tokens. They are managed in Agents → Settings → Policy tokens, by organization Owners, Admins and DevOps members. The old Organization settings page redirects there.

Why use one

A role answers “what kind of thing may this token do, across a scope”. A policy answers “may this exact request proceed”. That is what makes it a fit for an agent: you can hand out a token that reads everything in one environment, deploys that environment, may update one service, and can never delete anything - a shape no role expresses.

How it works

What Open Policy Agent does

Open Policy Agent (OPA) is an open source, general-purpose policy engine and a CNCF graduated project. Its model is deliberately narrow: it knows nothing about Qovery and holds no permission model of its own. You give it two things - a policy, and a JSON document called the input - and it answers a query against them. That is all it does. That separation is the point. The rules deciding what your agent may do live in the policy rather than in Qovery’s permission code, so you can express a constraint Qovery never anticipated without Qovery having to invent a role for it. Qovery runs OPA as a sidecar next to the API, reachable only over the pod’s loopback interface. Your policy and the requests it is evaluated against never leave Qovery’s infrastructure, and a decision costs an in-pod round trip rather than a call out to a network service.

How Qovery evaluates a request

The policy is evaluated at authentication time, before the request reaches the endpoint it targets. That is also why the policy sees the request’s raw path rather than a route template - the route has not been matched yet.
  1. The token is recognised by its sk-qov-01 prefix in the Authorization header, which is what tells it apart from a regular API token sent under the same Token scheme.
  2. The token is looked up. Only a hash of the token value is stored, so the presented value is hashed and matched against it. An unknown or expired token stops here.
  3. The target is resolved. Qovery takes the IDs appearing in the request path and asks its own database what they are - a service, an environment, a project - and what that resource’s ancestry is. This is what fills qovery_metadata, and it is why a policy can talk about environments while the request only names a service.
  4. The input document is built from the request and that resolved metadata. Its exact shape is documented below.
  5. The policy is loaded. The first time a pod handles a given token, it uploads that token’s policy to its OPA sidecar as a module, under a package unique to the token. Per-token packages are what stop one token’s rules from ever contributing to another token’s decision, and they are the reason your policy must not declare a package of its own. The module is compiled once and reused for subsequent requests.
  6. The policy is evaluated against the input, and OPA returns the value of allow: true, false, or undefined when no rule produced a value.
  7. The decision is applied. true lets the request continue to the endpoint. Anything else - including undefined, a non-boolean value, or an OPA that could not be reached - answers 401 Unauthorized.
  8. The role applies. An allowed request then runs with the token’s role, checked like any other caller’s permissions. If the role does not allow it, the answer is 403 Forbidden.

Rego, the policy language

Policies are written in rego, OPA’s language. It is declarative and derives from Datalog, extended to query nested JSON documents - which is exactly what an input document is. There are no statements to sequence and no control flow to trace: you declare the conditions under which something holds, and OPA searches for a way to satisfy them. Five ideas carry almost every Qovery policy. A rule has a head and a body, and the body is an AND. Every expression in the body must hold for the rule to produce its value.
Rules sharing a name are an OR. Defining allow twice is not a conflict, it adds an alternative. This is how a policy grants several unrelated things without collapsing into one tangled condition:
Anything missing is undefined, not an error. An expression referring to a value that is not there is itself undefined, and that propagates: the surrounding rule simply does not fire. input.request.body.key on a request that carried no body is undefined, so the rule using it does not fire, so the request is denied. Nothing throws, and fail-closed comes out as the natural default rather than something bolted on. default supplies a value when nothing fires. Without it, a request matching no rule leaves allow undefined; with it, the answer is explicitly false.
:= assigns, == compares, in tests membership. Membership needs no loop, which keeps a list of allowed verbs readable:
Rego also ships a large standard library - startswith, endswith, count, regex.match, sprintf and many more - listed in the policy reference.
The Rego Playground evaluates a policy against an input document in your browser, which is a fast way to get a feel for the language before writing a real policy. Paste one of the recipes and an input document from this page into it.

What this means in practice

  • The policy and the role constrain the token together. The role defaults to organization admin, and the policy is what narrows it. With the default role, a policy whose allow is unconditionally true grants full organization-admin access. Pick a narrower role, such as Viewer or a custom role, to cap what the token can ever do, whatever its policy says. The Owner role cannot be given to a policy token.
  • It fails closed. Anything other than an explicit true denies the request: no matching rule, a non-boolean allow, a request path that Qovery cannot resolve, or a policy engine that cannot be reached.
  • Who can create one. Owners and Admins can create a token with any role but Owner, and DevOps members can create one with the DevOps, Viewer or Billing role, or a custom role, in the Console or through the API. Other roles, custom roles included, cannot create one.
  • Revocation is immediate. The token is looked up on every request, with no caching.
  • Actions are attributed in the audit log as policy:<token-id>:<token-name>, so an agent’s activity is distinguishable from a regular API token’s.
With the default role, treat creating an API Policy Token with the same care as granting organization-admin. Start from a default allow := false policy, add only the rules you need, and pick the narrowest role that still lets the job run.

What the policy sees

Your policy is evaluated against this input document:
The public API path is internally prefixed with api, so POST https://api.qovery.com/environment/<id>/service/deploy reaches your policy as ["api", "environment", "<id>", "service", "deploy"]. Include that first segment when you match on a path.
A few behaviours worth knowing before you write rules against these fields:
  • The qovery_metadata keys are always present, set to null when nothing of that kind was resolved. Test input.qovery_metadata.service_id == null, not the absence of the key.
  • Qovery resolves one target per request, preferring a service over an environment over a project. For /api/environment/<env>/application/<app>, the target is the application: environment_id, project_id and cluster_id are then read from that service’s ancestry rather than from the path. So a rule on environment_id still holds for a request that only names the service.
  • IDs belonging to another organization are ignored, as if they were not in the path.
  • A request whose path cannot be resolved is denied, rather than shown with null fields. A rule phrased as service_id != "<id>" cannot be widened by a failed lookup.
  • Only JSON bodies are forwarded. A request with a non-JSON body, a malformed JSON body, or a body larger than 1 MiB is denied outright rather than presented to the policy with body set to null. Inside a policy, body == null therefore always means “there was no body”.
  • Query parameters, headers and the client IP are deliberately not exposed.

Writing the policy

Do not include a package declaration. Qovery prepends a per-token package so that one token’s rules can never authorize another’s, and a submitted package line is rejected. Submit rule definitions only.
  • The policy must define an allow rule, and the decision must be the boolean true. Anything else denies.
  • Start with default allow := false. Without it, a request that matches no rule leaves allow undefined, which Qovery also treats as a denial - the default just makes the intent explicit.
  • Maximum policy size: 65,536 characters.
  • The policy is compiled when you create the token, so a syntax error comes back immediately as a 400 with the compiler diagnostics.
  • The engine is OPA 1.19, so rego v1 syntax is available directly: if, in and contains need no import statement.

Starter policy

This is the policy prefilled in the Console when you create a token. It grants read-only access to one environment, write access to one service in it (but never a deletion), and the right to deploy that environment.

Policy recipes

For complete use cases, with the full policy, the prompt for the agent, and what the agent can and cannot do, see the agent policy examples: incident investigation, preview environments, stopping idle environments, right-sizing, scaling, rollbacks, tests on a copy of production and security patches. The recipes below are smaller building blocks.
Every GET and HEAD on the environment, its services and their sub-resources. Nothing else.
A token for a release pipeline: it can trigger deployments and cannot read or change anything else. Matching on the exact path is what keeps it that narrow.
Full read and write on a single service, with deletion excluded whatever the endpoint.
Because the parsed JSON body is part of the input, a rule can constrain the content of a change and not just its target. Here the agent may create environment variables on one application, but only those named FEATURE_*.
A request with no body leaves input.request.body.key undefined, so this rule denies it.

Create an API Policy Token

1

Open Policy tokens

In the Console, open the Agents tab of your organization, then Settings → Policy tokens (Owners, Admins and DevOps). Regular API tokens stay in Organization settings.
2

Click Create policy token

Press Create policy token. To start from a ready-made job, copy the policy of one of the agent policy examples.
3

Configure the token

Provide:
  • Token name: a descriptive name, unique within the organization
  • Description: what the token is for
  • Role: the role an allowed request runs with, organization admin by default
  • Policy (rego): your policy, prefilled with the starter policy above
4

Store the token value

The token is displayed once, in the form sk-qov-01-.... Copy it before closing the modal.
Important: Make sure you safely store the token returned by the UI. You won’t be able to retrieve it again (you will have to create a new one).

Use the token

An API Policy Token uses the same Authorization header scheme as a regular API token:
If the policy does not allow the request, the API answers 401 Unauthorized.

Inspect and revoke

In Agents → Settings → Policy tokens, each token shows its role, a bar of its allowed and denied calls over the last 7 days, Last used and Expires. A token with no expiry date gets a No expiry warning badge. Each token’s row menu offers:
  • See activity: opens Agents → Activity filtered on the token, with its allowed and refused calls. Activity is rolling out to organizations, so this entry and the 7-day bar may not appear for yours yet. See Activity.
  • View policy: shows the policy attached to the token, read-only. A policy can always be reviewed after creation. Only the token value is write-once.
  • Delete token: revokes the token. Type the token’s name to confirm, and the token stops working immediately.
Policies cannot be edited, and neither can the role or the expiry. To change one, delete the token and create a new one. Editing the custom role a token runs with changes what the token can do without rotating it, and deleting that role revokes the token. Deleted tokens that made calls in the last 30 days stay listed, greyed out, so you can still see what they did.

Manage tokens via the API

Creating a token, with the policy passed as a string in opa_policy:
The response contains the token value in its token field. This is the only response that ever carries it. The list endpoint returns each token with its opa_policy, never the token value. The create endpoint also accepts an optional role_id, the role an allowed request runs with, as returned by List organization custom roles (omit it for organization admin), and an optional expires_at (RFC 3339 date-time), after which the token stops authenticating. The Console form does not expose expires_at yet, so set an expiration through the API if you need one. Errors to expect when creating a token:

Test a policy before attaching it

A denied request tells you only that it was denied, so it is worth evaluating a policy locally first. Install OPA 1.19 - the same version Qovery runs - then:
1

Save the policy with a temporary package line

Qovery adds the package declaration for you, so add one locally and remove it before submitting.
2

Save the request you want to test

Write an input.json following the input document shown above.
3

Evaluate

true means the request would be allowed. false, or an empty result, means it would be denied.
Test the denials too, not just the allows. Flip the method to DELETE, point the request at another environment, and confirm the answer is false.

Current limitations

  • A denied request returns 401 with no explanation. Which rule failed is not surfaced to you today, so test policies locally before attaching them.
  • A token cannot be edited. Its policy, role and expiry are fixed at creation: delete the token and create a new one. Editing the permissions of the custom role a token runs with does change what the token can do, for every token with that role, and deleting that role revokes them.
  • Expiration is API-only. The Console form does not expose expires_at.
  • The input contract may change. Fields are added rather than renamed or removed, and policies may need to be updated.

Agent policy examples

Ready-to-use policies for autonomous agents, one use case per page.

API Token

Role-based tokens for CI/CD, Terraform and scripts.

Securing AI Agent Access

The full picture on giving an AI agent a safe footprint on your infrastructure.

Members & RBAC

The roles that back regular API tokens.