Skip to main content
This guide shows how to create a derivation rule through the API, check that it produces the derived mappings you expect, and take it out of use. For what rules are and how queries use them, see Attribute Derivations.

Prerequisites

  • An API token with an admin grant on attribute_derivations. Reading rules needs only read. See Permissions.
  • The ids of the source and target attributes.
  • map access on the target attribute for your company.
The examples build a rule that computes sha256_hashed_email from normalized_email. The attribute ids, company id, and dataset id are placeholders. Replace them with your own.

Check what already exists

Only one of your company’s rules can connect a given source and target, and a rule shared with you may already do the job. List the rules between the two attributes before creating one:
An empty records array means no rule you can use connects them.

Write the transformation

The transformation is a mapping whose expressions read the source attribute through $source. Which form you write depends on the types of the two attributes. Fetch both to see their shapes:
A value_mapping for two string attributes:
Keep these rules in mind:
  • $source must come first in a reference. $source.value is valid; an identifier with $source anywhere after the first position is rejected.
  • An object_mapping must cover every required property of the target and must not name properties the target does not have.
  • cached_mapping is not accepted.
  • You can use any transformation function, including CURRENT_DATE and CURRENT_TIMESTAMP. The expression runs each time a query uses the rule.
A rule that reads $source.<property> only applies to datasets that map the source attribute with an object_mapping. A rule that reads bare $source only applies to datasets that map it with a value_mapping. Match the form that providers of the source attribute actually use.

Create the rule

Send the rule to POST /derivations. Set lossy when the transformation discards information and imprecise when it is approximate, and use fidelity_note to explain either flag to people deciding whether to accept derived data.
company_id must be your own company. A 201 response returns the rule with its id. The transformation is validated before the rule is written. A 400 lists every problem found, so you can fix them in one pass. A 409 means your company already has a rule with that name or between those two attributes. See the Derivations API reference for the full list. If the response includes a cyclic_derivation warning, the rule was still created. The warning tells you the new rule closes a loop with existing rules, which is allowed. See Why cycles are allowed.

Share the rule

A new rule with "type": "none" applies only to your own company’s queries. To let other companies use it, update collaborators with their ids:
Sharing with every company is reserved for rules owned by Narrative.

Confirm the rule works

Check that the rule appears where you expect in the derivation graph:
The target attribute should appear in nodes with "relationship": "descendant", and your rule should appear in edges. Then check that a dataset mapped to the source attribute gains a derived mapping. Ask for the dataset with only its derived mappings:
Look in mappings for a row whose attribute_id is the target attribute and your rule’s id in provenance.derivation_path. Its mapping.expression shows the dataset’s own expression substituted for $source. Finally, query the target attribute:

If no derived mapping appears

Change a rule

PATCH /derivations/{derivation_id} changes only the fields you send. A changed mapping is validated the same way as on create. You cannot change source_attribute_id or target_attribute_id. Create a new rule instead. Because derived mappings are computed when they are needed, a change applies to every query that runs after it. There are no stored mappings to rebuild.

Retire a rule

To stop a rule being applied while keeping it, set it inactive:
To remove it permanently, delete it:
Either action removes the derived mappings the rule produced, for your company and every company it was shared with. Queries for the target attribute stop returning rows from datasets that relied on the rule.

Attribute Derivations

How rules become mappings and which mapping wins

Derivations API

Every field, permission, and error response

Mapping Schemas

Create the stored mappings that rules build on

Transformation Functions

Functions available inside rule expressions