> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# secret node

> Declare a named credential that an effect can read, without putting its value in the procedure.

A `secret` block declares a named slot for a credential, such as an API token for your policy
administration system. It never holds the value. You give the slot a value in Effective after you
publish, and only an effect that lists the slot can read it.

## Syntax

```yaml secret id=policy-admin-token theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
format: text
description: API token for the Northwind policy administration system
```

The `id` is the slot's name. The block takes no other attributes. Like other declarations, it can
sit anywhere in the document.

## Fields

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `format` | `text`, `json` or `bytes` | Yes | None | The kind of value, which decides how an effect reads it |
| `description` | string | No | None | What the credential is for, shown to the people who set it |

There is no field for the value itself, and none should be added to the source. Never paste a
credential into a procedure.

## Read a secret in an effect

An effect reads a secret only when its header grants the `secrets` service and lists the slot:

```text theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
ts effect id=get-policy-status input=policy-request result=policy-response uses=fetch,secrets secrets=policy-admin-token
```

Inside the effect, read the value with the method that matches the slot's `format`:

| `format` | Method | Returns |
| - | - | - |
| `text` | `secrets.getText('<id>')` | A string |
| `json` | `secrets.getJson('<id>')` | The parsed JSON value |
| `bytes` | `secrets.getBytes('<id>')` | A read-only byte array. The value is stored as base64 text |

* The slot name must be a string literal, written directly in the call. A name built at run time
  is refused.
* The slots the effect reads and the slots its `secrets=` lists must be the same. A listed slot
  the effect never reads is an error.
* Every listed slot must have a `secret` block.

Never return a secret, log it, or put it in data the procedure keeps. An effect that can read a
credential can also send it anywhere it can reach, so read secrets only in the effects that need
them.

## Give a secret a value

1. **Publish the procedure.** A [draft](https://docs.canary.effectiveai.app/guides/procedures.md#try-a-draft) can't declare
   secrets, so try secret-free changes as drafts and publish the source that reads secrets.
   Publishing records each slot. [Reading a version](https://docs.canary.effectiveai.app/guides/procedures.md#check-its-inputs)
   lists them under `secrets`, each with its `name`, `format` and `description`.
2. **Set a value in Effective.** Set it on the procedure's page in the Effective app, under
   **Secrets**. Each slot can have two values:
   * **Shared default**: one value for the procedure, used by every run that has no override.
   * **Personal override**: a person's own value, used for runs that run as that person. It takes
     precedence over the shared default.

When an effect reads a slot, the run uses the override of the person the run runs as, if they set
one, and otherwise the shared default. A run started from the API runs as the API key's user; a
run started by a [trigger](https://docs.canary.effectiveai.app/procedures/reference/triggers.md) runs as the trigger's authorized user.

If the slot has no usable value, or the value doesn't match the slot's format, that effect call
fails. An override that is set but unusable doesn't fall back to the shared default.

Removing a `secret` block from a later version doesn't remove the slot: older versions and runs
in progress may still need it.

## Example

This procedure reads a policy's status from an illustrative policy administration system at
`pas.northwind.example`, with a bearer token from the `policy-admin-token` slot.

````markdown read-policy-status.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: read-policy-status

inputs:
  policy_number: { type: string }
outputs:
  policy: { schema: policy-status }
---

# Read a policy's status

## Read the policy

```yaml call id=read-policy
function: '@local.get-policy-status'
input:
  policyNumber: $inputs.policy_number
output: policy_status
```

```yaml complete id=done
output: policy
value:
  policyNumber: $inputs.policy_number
  status: $policy_status.status
```

## Resources

```yaml secret id=policy-admin-token
format: text
description: API token for the Northwind policy administration system
```

```ts effect id=get-policy-status input=policy-request result=policy-response uses=fetch,secrets secrets=policy-admin-token
async ({ policyNumber }, { fetch, secrets, signal }) => {
  const token = await secrets.getText('policy-admin-token');
  const response = await fetch(`https://pas.northwind.example/policies/${policyNumber}`, {
    headers: { Authorization: `Bearer ${token}` },
    signal,
  });
  if (!response.ok) throw new Error(`Policy system returned ${response.status}`);
  const body: unknown = await response.json();
  if (
    typeof body !== 'object' ||
    body === null ||
    !('status' in body) ||
    typeof body.status !== 'string'
  ) {
    throw new Error('Policy system returned an unexpected policy');
  }
  return { status: body.status };
};
```

```yaml schema id=policy-request
type: object
additionalProperties: false
required: [policyNumber]
properties:
  policyNumber: { type: string }
```

```yaml schema id=policy-response
type: object
additionalProperties: false
required: [status]
properties:
  status: { type: string }
```

```yaml schema id=policy-status
type: object
additionalProperties: false
required: [policyNumber, status]
properties:
  policyNumber: { type: string }
  status: { type: string }
```
````

## On retry and resume

* **The value is read for each call.** Each time the effect is called, including each retry, the
  run looks up the current value. All reads within one call see the same value.
* **Changing a value needs no new version.** Set a new shared default or override, and later
  effect calls use it. The procedure's source and version don't change.
* **The value stays out of the record.** Effective never writes a secret's value to the source,
  the version or the run record. The run records which slot was used, never the value. Code that
  reads a secret must not return it in a step's result, because results are recorded.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-SECRET-001` | The body has a field other than `format` and `description`, or an unsupported `format` | Use `format: text`, `json` or `bytes` |
| `PROC-SECRET-003` | The effect lists a slot without `uses=secrets`, or reads it with the wrong method for its format | Add `secrets` to `uses=`, and match the method to the format |
| `PROC-EFFECT-004` | A listed slot has no `secret` block, isn't listed in `secrets=`, is never read, or is read with a name the compiler can't check | Declare the slot, list it, and read it with a string literal |

A missing value isn't a compiler error. It fails the effect call when the run reaches it.

## See also

* [Functions and effects](https://docs.canary.effectiveai.app/procedures/reference/nodes/function-and-effect.md): the effect header,
  `uses=` and the other services an effect can use.
* [`call`](https://docs.canary.effectiveai.app/procedures/reference/nodes/call): run the effect that reads the secret.
* [Writes to your systems](https://docs.canary.effectiveai.app/procedures/concepts/writes-to-your-systems.md): calling your own systems
  safely.
* [`schema`](https://docs.canary.effectiveai.app/procedures/reference/nodes/schema): the other declaration that never runs.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.