Query policies are managed through the API. You need an API key for the company that owns the access rule.
Prerequisites
- An access rule your company owns, and its ID
- An API key for your company
- Familiarity with access rules
Select the columns in the access rule
A policy can only name columns the access rule’s NQL selects. A Rosetta Stone attribute counts only when the NQL selects it from_rosetta_stone. Selecting a dataset column that is mapped to the attribute is not enough.
The examples in this guide use an access rule named customer_segments_ar, shared by a company with the slug acme:
Once an access rule has a policy, partners can read a Rosetta Stone attribute through it only if the NQL selects that attribute from
_rosetta_stone. Select every attribute partners need this way before you attach the policy.Write the policy
Start from what partners should be able to do, and open only that. Any usage the policy doesn’t set for a column falls back todefault, and any usage default doesn’t set is denied.
Pick the pattern closest to your use case:
- Join only
- Hashed values only
- Counts only
Partners can join on email addresses to enrich their own records, but never return one. The join-required rule means the access rule is usable only when joined to the partner’s own data on email.A partner query that satisfies this policy:
segment_id needs projection set to allow as well as group_by.
For every option, including nested columns and external functions such as AI_COMPLETE, see the Query Policies reference.
Attach the policy
Send the policy as the body ofPUT /v2/access-rules/{access_rule_id}/query-policy. This replaces any policy the access rule already has.
200 response returns the stored policy. The policy applies to every query compiled after that.
To create an access rule with a policy already attached, add the same object as query_policy in the body of POST /v2/access-rules.
Fix validation errors
Narrative checks the policy against the access rule’s NQL before storing it. An invalid policy returns400, leaves the current policy in place, and lists every problem in error_description:
The Query Policies reference lists every message.
Check the policy
Read the stored policy back:404 means the access rule has no policy. Partners the access rule is shared with can call the same endpoint, and the policy also appears as query_policy in every v2 access rule response.
You and partners the access rule is shared with can also see the policy on the access rule’s detail page, in the Query Policy section. A table shows, for each attribute and field the policy names and for everything else, whether a query may select it, aggregate it, group by it, or join on it. Below the table, the section says whether every query must join the access rule to the querying company’s own data, and on which keys. Show raw JSON displays the stored policy. An access rule without a policy shows No query policy.
Test a query against the policy
POST /v2/nql/simulate reports which access rules would deny a SELECT or MERGE statement, and why, without running it. No job is created, no data is read, and no credits are charged. The token needs read access to subscriptions.
List from 1 to 50 access rules in context.access_rules. Each entry is one of:
- A stored access rule, by
access_rule_id. Your company must own it or be able to query it. - A draft access rule, as
access_rulewith aname,nql, andquery_policy. It’s checked the wayPOST /v2/access-ruleschecks a new access rule, but isn’t saved. Its NQL must reference your datasets by ID, such ascompany_data."2001".
placeholder_owner_slug. For the length of the request, the access rule and the datasets it reads belong to a temporary company with that slug, and the statement reads the access rule as <placeholder_owner_slug>."<access_rule_name>" or through narrative.rosetta_stone. The slug can be a real company’s, such as acme, so you can test the exact query a partner will write. That company’s data is never involved.
The statement runs as your company, in the role of a buyer, and sees only the listed access rules and your own datasets. It reads your datasets only through company_data, by ID or by name.
This request tests a draft policy, which hides email and requires a join on it, against a query that returns email addresses without joining:
200 response lists each denied access rule, once for every table of the statement it was denied on. index is the access rule’s position in context.access_rules, and an entry carries access_rule_id for a stored access rule or access_rule_name for a draft:
type is field_usage (the statement uses a field in a way the policy doesn’t allow), join_required (the statement doesn’t join as the policy requires), or invalid_query_policy (the policy doesn’t compile, so it denies every statement). Branch on type, not reason: the text can change.
A listed access rule that isn’t in denied_access_rules wasn’t denied. Either its policy allowed the statement, or the statement didn’t read it. An empty list means no listed access rule denied the statement.
Change or remove the policy
To change the policy, send the complete new policy withPUT. There is no partial update: rules you leave out of the new policy are gone.
To remove the policy, send DELETE:
204 response means the access rule has no policy, whether or not it had one before. Partners can then use every column the access rule selects without restriction.
When a partner’s query returns fewer rows
A query that breaks a policy doesn’t fail. Narrative leaves that access rule out of the query, so it contributes no rows, and the query returns rows from any other access rules it reads. A partner who expects rows from your access rule and gets none should compare their query against the policy, which they can read on the access rule’s detail page:- Does the query return a column’s raw value where the policy only allows functions of it?
- Does every function on a restricted column appear in the matching list?
- If the policy has a join-required rule, does the query join the access rule directly to one of the partner’s own datasets on the required keys, in a join that keeps only the access rule’s matched rows?
Related content
Access Rules
Why query policies exist and how Narrative enforces them
Query Policies Reference
Every field, mode, and validation error
Joining Datasets
Write the joins a join-required rule expects
Set a Query Policy API
Request and response schemas

