# Encrypt Data

> Encrypts data with AES-256-GCM (random IV, authenticated).

Source: https://docs.sommatic.ai/en/nodes/crypto.encrypt/

- Slug: `crypto.encrypt`
- Family: Identity
- Version: 2.0.0
- Status: Available
- Execution: sync

Encrypts data with AES-256-GCM (random IV, authenticated). Output: v1.<iv>.<tag>.<ciphertext>.

## Configuration

- `connection_id` (string, optional): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.
- `key` (string, optional): Encryption key (64 hex chars, base64 of 32 bytes, or a passphrase) as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id.
- `data` (any, required): Data to encrypt (any JSON value)

## Output ports

- `out` (default): Encrypted data
- `error`: Encryption failed

## Input contract

- `connection_id` (string, optional)
- `key` (string, optional)
- `data` (any, required)

## Output contract

- `encrypted` (string, optional): v1.<iv>.<tag>.<ciphertext> (base64url)
- `algorithm` (string, optional)

## In expressions

What a later node can read from this node's output with `$("Encrypt Data").item.json.<path>` (write your node's label instead of `Encrypt Data`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Encrypt Data").item.json.encrypted` (string) = `"v1.sEZVicEyZQ3qzFNu.L_Y2ciTg_ZoET8aUKmDTuw.S…"`: v1.<iv>.<tag>.<ciphertext> (base64url)
- `$("Encrypt Data").item.json.algorithm` (string) = `"aes-256-gcm"`


## How it works

Encrypts any JSON value with AES-256-GCM and returns a versioned token `v1.<iv>.<tag>.<ciphertext>` (base64url). The key comes from an `encryption-key` credential (`connection_id`) or, discouraged, from a literal `key`.

1. Resolves the config. A pure expression such as `{{ $json.payment }}` in `data` keeps the value type; mixed text always yields a string.
2. Leaves through `error` with `data is required` when `data` is undefined, null or an empty string.
3. Picks the key: `connection_id` first, otherwise the literal `key`. With neither, the node leaves through `error`.
4. Wraps the value as `{ "v": data }`, encrypts it with a random 12-byte IV and joins version, IV, authentication tag and ciphertext with dots.
5. Returns `{ encrypted, algorithm }` on `out`, with a secret-free audit record in `meta.crypto` and literal-key warnings in `meta.warnings`.

## In a workflow

### Node

```json
{
  "node_id": "node-encrypt-payment",
  "label": "Encrypt payment details",
  "operator_slug": "crypto.encrypt",
  "operator_version": "2.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "data": "{{ $json.payment }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "encrypted": "v1.sEZVicEyZQ3qzFNu.L_Y2ciTg_ZoET8aUKmDTuw.SRzvsE_YNuaJMYT7_OF3xFIwECFqgqkxBnj3iyY",
    "algorithm": "aes-256-gcm"
  },
  "meta": {
    "crypto": {
      "op": "encrypt",
      "key_source": "credential",
      "alias": "connection",
      "credential_id": "pi-credential-xxxx",
      "algorithm": "aes-256-gcm",
      "ok": true
    },
    "warnings": []
  }
}
```

## Output ports (behaviour in the code)

### `out`

The value was encrypted.

```json
{
  "encrypted": "v1.sEZVicEyZQ3qzFNu.L_Y2ciTg_ZoET8aUKmDTuw.SRzvsE_YNuaJMYT7_OF3xFIwECFqgqkxBnj3iyY",
  "algorithm": "aes-256-gcm"
}
```

### `error`

`data` is missing, no key is configured, or the credential cannot be resolved or has no usable key.

```json
{
  "error": "data is required"
}
```

## Behaviour in the code

- `connection_id`: Takes precedence over the literal `key`. The credential is resolved on the server when the node runs, so the secret never appears in the flow. A string secret is used as the key; an object secret must carry `key` or `secret`.
- `key`: Used only when `connection_id` is empty. Accepts 64 hexadecimal characters, base64 of 32 bytes, or any other text as a passphrase. Literal keys work, but they are stored in the flow, its version history and Git, and `meta.warnings` reports them.
- `data`: Any JSON value. Objects, arrays, numbers and booleans come back with their type from `crypto.decrypt`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `data is required` | `error` | `data` is empty or its expression resolved to nothing. `0` and `false` are valid values. |
| `A key is required: pass { credential: "<alias>" } (recommended) or { key }` | `error` | Neither `connection_id` nor the literal key field is set. Select a credential in `connection_id`. |
| `credential binding 'connection' has no key usable for symmetric` | `error` | The credential resolved, but its secret is empty or is an object without a usable field. Store the key as a string, or as an object with the field this operation reads (see `connection_id`). |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | The credential id does not exist or is not available to this workflow. Check the id and the credential type. |

## Notes and limits

- Every run uses a fresh random IV, so the same value encrypted twice gives different tokens. Do not compare tokens for equality; use `crypto.hash` for that.
- Only `crypto.decrypt` with the same key reads the token (format `v1`).
- Leaving through `error` does not fail the run: the engine follows the edges wired to `error`, and if there are none the branch simply ends.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "crypto.encrypt",
  "name": "Encrypt Data",
  "category": "identity",
  "version": "2.0.0",
  "description": "Encrypts data with AES-256-GCM (random IV, authenticated). Output: v1.<iv>.<tag>.<ciphertext>.",
  "capabilities": [
    "crypto",
    "encryption"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Encryption key credential",
        "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "encryption-key"
          ]
        }
      },
      "key": {
        "type": "string",
        "ui_widget": "password",
        "x-literal-secret": true,
        "description": "Encryption key (64 hex chars, base64 of 32 bytes, or a passphrase) as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
      },
      "data": {
        "description": "Data to encrypt (any JSON value)"
      }
    },
    "required": [
      "data"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "key": {
        "type": "string"
      },
      "data": {}
    },
    "required": [
      "data"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "encrypted": {
        "type": "string",
        "description": "v1.<iv>.<tag>.<ciphertext> (base64url)"
      },
      "algorithm": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Encrypted",
      "description": "Encrypted data",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Encryption failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard"
  ],
  "tags": [
    "crypto",
    "security"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_veripass",
    "profile": {
      "name": "Veripass",
      "verified": true,
      "url": "https://me.veripass.com/veripass",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Encryption key credential",
      "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "encryption-key"
        ]
      }
    },
    "key": {
      "type": "string",
      "ui_widget": "password",
      "x-literal-secret": true,
      "description": "Encryption key (64 hex chars, base64 of 32 bytes, or a passphrase) as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
    },
    "data": {
      "description": "Data to encrypt (any JSON value)"
    }
  },
  "required": [
    "data"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "key": {
      "type": "string"
    },
    "data": {}
  },
  "required": [
    "data"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "encrypted": {
      "type": "string",
      "description": "v1.<iv>.<tag>.<ciphertext> (base64url)"
    },
    "algorithm": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Encrypted",
    "description": "Encrypted data",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Encryption failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
