# Decrypt Data

> Decrypts a value produced by crypto.encrypt or $encrypt.

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

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

Decrypts a value produced by crypto.encrypt or $encrypt. Fails if the key is wrong or the value was altered.

## Configuration

- `connection_id` (string, optional): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `encryption-key`.
- `key` (string, optional): Encryption key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id.
- `encrypted` (string, required): v1.<iv>.<tag>.<ciphertext>

## Output ports

- `out` (default): Decrypted string
- `error`: Decryption failed

## Input contract

- `connection_id` (string, optional)
- `key` (string, optional)
- `encrypted` (string, required)

## Output contract

- `data` (any, optional): Decrypted value

## In expressions

What a later node can read from this node's output with `$("Decrypt Data").item.json.<path>` (write your node's label instead of `Decrypt 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.

- `$("Decrypt Data").item.json.data` (any) = `{…}`: Decrypted value
- `$("Decrypt Data").item.json.data.card_last4` (string) = `"4242"` (Not suggested by the editor)
- `$("Decrypt Data").item.json.data.amount` (number) = `129.9` (Not suggested by the editor)
- `$("Decrypt Data").item.json.data.currency` (string) = `"USD"` (Not suggested by the editor)


## How it works

Decrypts a `v1.<iv>.<tag>.<ciphertext>` token produced by `crypto.encrypt` and returns the original JSON value under `data`.

1. Resolves the config; `encrypted` is usually `{{ $json.encrypted }}`.
2. Leaves through `error` with `encrypted data is required` when `encrypted` is empty.
3. Picks the key: `connection_id` first, otherwise the literal `key`.
4. Checks the format: four dot-separated parts, the first one `v1`.
5. Decrypts with AES-256-GCM and verifies the authentication tag: a wrong key or an altered token fails.
6. Returns `{ data: <original value> }` on `out`, with the audit record in `meta.crypto`.

## In a workflow

### Node

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

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "data": {
      "card_last4": "4242",
      "amount": 129.9,
      "currency": "USD"
    }
  },
  "meta": {
    "crypto": {
      "op": "decrypt",
      "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 token was decrypted and authenticated.

```json
{
  "data": {
    "card_last4": "4242",
    "amount": 129.9,
    "currency": "USD"
  }
}
```

### `error`

The token is missing or malformed, the key is wrong, or the credential cannot be resolved.

```json
{
  "error": "Decryption failed: wrong key or tampered value"
}
```

## 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`. It must hold the same key used by `crypto.encrypt`.
- `key`: A literal key works, but it is stored in the flow, its version history and Git, and `meta.warnings` reports it on every run. Prefer `connection_id`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `encrypted data is required` | `error` | `encrypted` is empty. |
| `Invalid encrypted value: expected v1.<iv>.<tag>.<ciphertext>` | `error` | The value is not a token from `crypto.encrypt` (for example the whole `{ encrypted, algorithm }` object was passed instead of `encrypted`). |
| `Decryption failed: wrong key or tampered value` | `error` | The key differs from the one used to encrypt, or the token was altered or truncated. |
| `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

- The value comes back under `data.data`: read it downstream as `{{ $json.data.amount }}`.
- The decrypted value is plain data in the run from this point on: avoid sending it to logs or external systems you do not need.
- 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.decrypt",
  "name": "Decrypt Data",
  "category": "identity",
  "version": "2.0.0",
  "description": "Decrypts a value produced by crypto.encrypt or $encrypt. Fails if the key is wrong or the value was altered.",
  "capabilities": [
    "crypto",
    "decryption"
  ],
  "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 as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
      },
      "encrypted": {
        "type": "string",
        "description": "v1.<iv>.<tag>.<ciphertext>"
      }
    },
    "required": [
      "encrypted"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "key": {
        "type": "string"
      },
      "encrypted": {
        "type": "string"
      }
    },
    "required": [
      "encrypted"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "data": {
        "description": "Decrypted value"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Decrypted",
      "description": "Decrypted string",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Decryption 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 as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
    },
    "encrypted": {
      "type": "string",
      "description": "v1.<iv>.<tag>.<ciphertext>"
    }
  },
  "required": [
    "encrypted"
  ]
}
```

### input_contract_schema

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

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "data": {
      "description": "Decrypted value"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Decrypted",
    "description": "Decrypted string",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Decryption failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
