# Encrypt Temporal Workflow Payloads with a Custom Codec

Yes. An agent adds a PayloadCodec that encrypts workflow inputs and results, wires it into the Worker, Client and tests, adds a codec server and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-payload-encryption/temporal/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how data moves through the application. In a TypeScript project like temporalio/money-transfer-project-template-ts, `package.json` pins every `@temporalio/*` package at `~1.14.1`, `.nvmrc` says Node 18 and `tsconfig.json` extends `@tsconfig/node18`. `src/shared.ts` defines `PaymentDetails` with `amount`, `sourceAccount`, `targetAccount` and `referenceId`, and `src/client.ts` starts `moneyTransfer` with a real-looking pair of account numbers and an amount as the workflow argument. `src/workflows.ts` proxies the `withdraw`, `deposit` and `refund` activities with a retry policy of up to 500 attempts and a one-minute `startToCloseTimeout`, and each activity returns a transaction id string. Every one of those values, the argument, each activity's input and output and the workflow result, is a payload that Temporal Server stores in the event history and shows in the Web UI as plain JSON. `src/worker.ts` and `src/client.ts` both build their connection from `loadClientConnectConfig` in `@temporalio/envconfig`, and neither sets a `dataConverter`, so nothing stands between the application and the server.

It adds `src/encryption-codec.ts`, a class implementing `PayloadCodec` from `@temporalio/common` with `encode` and `decode`. `encode` takes each payload, serialises it, encrypts it with AES-256-GCM through Node's `crypto` module and returns a new payload whose `metadata` carries `encoding: binary/encrypted` and the id of the key used, with the ciphertext as `data`; `decode` reads the key id, looks the key up and reverses it, and passes through any payload that is not marked encrypted, so histories written before the change still read. Keys come from `src/keys.ts`, which reads them from the environment in development and leaves one clearly marked function to replace with a call to the reader's secrets manager. The codec is attached as `dataConverter: { payloadCodecs: [codec] }` in `Worker.create` in `src/worker.ts` and in the `Client` constructor in `src/client.ts`, so inputs are encrypted before they leave the client and activity results before they leave the worker.

The tests must change too, or they fail against an encrypting worker. `src/mocha/workflows.test.ts` creates a `TestWorkflowEnvironment.createLocal()` and its own `Worker` with mocked activities; the agent passes the same `dataConverter` to both the environment's client options and the worker so the round trip is tested, and adds `src/mocha/encryption-codec.test.ts`, which encodes a payload, checks the metadata and that the ciphertext is not the plaintext, decodes it back and confirms a payload without the encrypted marker passes through unchanged. It also adds `src/codec-server.ts`, a small HTTP server exposing `POST /decode` and `POST /encode` in the shape the Temporal Web UI and CLI call, with CORS limited to the UI's origin and a note that it must sit behind the reader's own authentication, plus a `codec-server` script in `package.json` beside the existing `worker` and `client` scripts.

To check it, the agent runs `npm install`, `npm run build`, `npm run lint` and `npm test` in the sandbox; the mocha suite downloads the Temporal dev server through `TestWorkflowEnvironment`, so the workflow test runs end to end with encryption on. It then starts the dev server itself, runs the worker and the client once, and reads the history back with the Temporal CLI to show that the workflow input and each activity result are stored as `binary/encrypted` payloads, and that pointing the CLI at the codec server decodes them. Those two listings, the opaque history and the decoded one, go into the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Do the work the Linear issue describes: encrypt the payloads this Temporal application sends to the server. Read package.json, .nvmrc, tsconfig.json and every file under src/ including the tests first. Add a PayloadCodec that encrypts payload data with AES-256-GCM, marks encrypted payloads in their metadata with the encoding and a key id, and passes unmarked payloads through on decode. Read keys from the environment with a single function to replace with a secrets manager. Attach the codec as a dataConverter on the Worker and on the Client, and on the test environment and test worker so the existing workflow test still passes. Add a unit test for the codec and a codec server with the /decode and /encode endpoints the Web UI and CLI use, with CORS limited to the UI origin, and a script to start it. Run the install, build, lint and test commands, then run the dev server, worker and client once and show with the CLI that the history holds encrypted payloads and that the codec server decodes them. Do not change the workflow or activity logic. Open a pull request with those listings.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request adding `src/encryption-codec.ts`, `src/keys.ts`, `src/codec-server.ts` and a codec unit test, changing the `Worker.create` and `Client` calls in `src/worker.ts` and `src/client.ts` by one option each, updating the test environment setup in the existing workflow test, and adding one script to `package.json`. The workflow and activity files are untouched, so the transfer logic and its retry policy are exactly as they were.

The description shows two readings of the same workflow history: the raw one where the input and every activity result appear as `binary/encrypted` payloads with a key id, and the one read through the codec server where the account numbers and amounts are visible again. It states which key id the sample key uses and where the function to replace with a secrets manager lives.

Before merging, a reviewer decides how keys are provisioned to workers and the codec server in each environment, where the codec server runs and what authentication sits in front of it, and whether histories written before the change need re-encrypting, which the pass-through decode makes optional rather than urgent.

## Run it on every Linear issue labelled security

Add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled` with the label your security team uses for agent work. The trigger adds the labelled issue after the prompt, so an issue that names the key management service, the UI origin to allow or a field that must never be stored in clear steers the run without any edit to the prompt; a change made by an app integration never fires it, and `max_runs_per_hour` caps how many issues the automation picks up in an hour.

From your own server, send `POST /o/{org}/sessions` with the repository and the issue URL in `resources` so the issue text reaches the agent through your Linear connection, and a `request_id` that is a UUID v5 of the issue id so a retried call returns the run already started. Set a `webhook_url` on the key to receive `run.completed` with `data.session_url` when the pull request is open.

## Why run it on Rush?

Payload encryption touches the worker, the client, the tests and the tooling at once, and the only convincing proof is a workflow run whose history is opaque on the server and readable through the codec server. The agent's sandbox is a clean machine where it starts the Temporal dev server, runs the worker and the client and reads the history both ways, and every command and both listings are in the run's record beside the pull request, so a reviewer sees the encryption working rather than trusting that the codec is wired in.

The same request runs across every Temporal service in an organisation at once, each in its own sandbox with its own worker and client entry points, which is how a security team gets one consistent codec and key convention into all of them in one review cycle instead of one service at a time.

## Frequently asked questions

### Will old workflow histories still be readable?

Yes. `decode` passes through any payload not marked `binary/encrypted`, so histories written before the change keep reading, and running workflows pick up encryption from the next payload a worker or client writes. Re-encrypting old histories is not possible and not needed.

### Where do the keys live?

In the pull request they come from an environment variable through one function in `src/keys.ts`, clearly marked as the place to call your key management service. The key id travels in the payload metadata, so rotating keys means adding a new id and keeping the old one readable.

### Why does the Web UI need a codec server?

The server only ever stores ciphertext, so the UI and the CLI cannot show inputs or results without something that decrypts for them. The codec server does that over `/decode` and `/encode`, and because it holds keys it must run inside your network behind your own authentication, which the pull request leaves to you.

### Does encryption change the workflow code or determinism?

No. The codec runs in the data converter outside the workflow sandbox, on the way to and from the server, so `src/workflows.ts` is unchanged and replay is unaffected. Activity inputs and results are encrypted the same way without touching `src/activities.ts`.

### Will the tests slow down?

The existing workflow test already starts a local Temporal dev server through `TestWorkflowEnvironment`, so adding the codec to it costs the encryption of a few small payloads. The new codec unit test runs without a server at all.

## More Temporal jobs

- [Expose Temporal Worker Metrics to Prometheus in a TypeScript Project](https://getrush.ai/run/add-prometheus-metrics/temporal/)
- [Set Up Renovate for a Temporal TypeScript Monorepo](https://getrush.ai/run/add-renovate/temporal/)
- [Add Replay Tests to Temporal Workflows](https://getrush.ai/run/add-replay-tests/temporal/)

## More jobs like this: Turn a Linear label into a PR

- [Add PDF Invoices to a Filament Admin Panel from a Linear Issue](https://getrush.ai/run/add-pdf-invoices/filament/)
- [Add a Periodic Task with Celery Beat](https://getrush.ai/run/add-periodic-tasks/celery/)
- [Add a Scheduled Job to a Flask App That Uses RQ](https://getrush.ai/run/add-periodic-tasks/rq/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
