> For the complete documentation index, see [llms.txt](https://he-said.tdreyno.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://he-said.tdreyno.com/package-guides/abac-api.md).

# ABAC API

## Imports

```ts
import {
  action,
  actionIs,
  actionIn,
  approve,
  deny,
  policy,
  enforcer,
  failure,
  failureMessage,
  all,
  userTerm,
  resourceTerm,
  environmentTerm,
  eq,
  ge,
  eqEnv,
} from "@tdreyno/he-said/abac"
```

## Action Tokens

### `action(label?)`

Create an action identity token.

```ts
const READ = action("read")
const UPDATE = action("update")
```

### `actionIs(...)`

Match by action token reference.

```ts
actionIs(READ)
```

### `actionIn(...tokens)`

Match any of the provided action tokens.

```ts
actionIn(READ, UPDATE)
```

## Rule Builders

### `approve(ruleOrRules, options?)`

Create an approve rule.

### `deny(ruleOrRules, options?)`

Create a deny rule.

`ruleOrRules` accepts:

* `Rule`
* `Rule[]` (normalized to `and(...rules)`)

`options`:

* `name?: string`
* `failure?: FailureToken`
* `priority?: number`

## Policy

### `policy(...rules)`

Create a policy from variadic rules.

```ts
const p = policy(ruleA, ruleB, ruleC)
```

## Enforcer

### `enforcer(policy, options?)`

Create an immutable enforcer instance.

`options`:

* `adapter?: EvaluatorAdapter<Record<PropertyKey, unknown>, Context>`
* `evaluatorContext?: Context`

### `authz.can(actionToken, context)`

Evaluate a single decision.

```ts
const result = await authz.can(READ, {
  user,
  resource,
  environment,
})
```

### `authz.policy()`

Return the policy used by the enforcer.

## Failure Tokens

### `failure(message?)`

Create a reusable failure token.

```ts
const RULE_DENY_SUSPENDED = failure("Suspended users cannot access documents.")
```

### `failureMessage(token)`

Read optional message metadata from a failure token.

## Comparators and Combinators

### `eq(...)`

```ts
eq(
  (user: User) => user.department,
  (resource: Resource) => resource.department,
)
eq((user: User) => user.suspended, true)

// expression-based (SQL-compilable with postgres adapter)
eq(
  attr(userTerm<User>(), "department"),
  attr(resourceTerm<Resource>(), "department"),
)
```

### `ge(...)`

```ts
ge(
  (user: User) => user.clearance,
  (resource: Resource) => resource.sensitivity,
)

// expression-based
ge(
  attr(userTerm<User>(), "clearance"),
  attr(resourceTerm<Resource>(), "sensitivity"),
)
```

### `eqEnv(...)`

```ts
eqEnv(env => env.isBusinessHours, false)
```

### `all(...rules)`

Alias for logical `and` composition.

## Types

Main exported types:

* `ActionToken`
* `FailureToken`
* `RuleRef`
* `PolicyRef`
* `CanContext`
* `CanDecision`
* `DecisionTrace`
* `RuleTrace`
* `ABACEnforcer`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://he-said.tdreyno.com/package-guides/abac-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
