PermuteDocs
Go to Permute

Operations · operationsSourceUpdate

Save operation code as a draft

Saves externally authored handler code using the current operation configuration. Requires the revisions and updatedAt returned by getSource. Does not run an AI build, execute, or publish.

PUT/v1/operations/{operationId}/source
betaapiKeyworkspace

When to use

Save a complete handler as a draft. First read getSource, edit that source, and pass its revision, dataFlowRevision, and updatedAt back as the expected values (the JSON values below are illustrative). A 409 conflict means the source or configuration changed; read again and reconcile your edits before retrying.

For agents

Use JavaScript and module.exports = { handler }. Use event.query(sql) for local rows, event.remoteQuery(connectorId, sql) for live SQL sources, and event.currentDate for relative dates (the execution's UTC date in YYYY-MM-DD format). For large SQL outputs, return await event.query(sql, { result: 'tableReference' }) or an object of named results from that mode; rows stay in DuckDB instead of entering JavaScript. Otherwise return plain data without functions. Custom toJSON() methods are not invoked; convert custom values to plain data before returning them. Imports and require() are unavailable. Saving does not execute or publish; run and inspect the output afterward.

Example

typescript
import { PermuteClient } from '@permute/sdk';

const orgClient = new PermuteClient({
  apiKey: process.env.PERMUTE_API_KEY!,
});

const client = orgClient.withWorkspace(process.env.PERMUTE_WORKSPACE_ID!);

const operationId = 'oper_revenue123';
const current = await client.operations.getSource(operationId);

// Edit current.source, then submit the complete updated handler.
const newSource = "async function handler(event) { return await event.query(\"SELECT customer, SUM(revenue) AS revenue FROM blok_revenue GROUP BY customer\"); } module.exports = { handler };";

const result = await client.operations.updateSource(operationId, {
  source: newSource,
  expectedRevision: current.revision,
  expectedDataFlowRevision: current.dataFlowRevision,
  expectedUpdatedAt: current.updatedAt,
});

Request body

json
{
  "source": "async function handler(event) { return await event.query(\"SELECT customer, SUM(revenue) AS revenue FROM blok_revenue GROUP BY customer\"); } module.exports = { handler };",
  "expectedRevision": {
    "revisionId": "rvsn_code123",
    "sequence": 1
  },
  "expectedDataFlowRevision": 2,
  "expectedUpdatedAt": "2026-09-10T12:00:00.000Z"
}

Response

json
{
  "data": {
    "operationId": "oper_revenue123",
    "source": "async function handler(event) { return await event.query(\"SELECT customer, SUM(revenue) AS revenue FROM blok_revenue GROUP BY customer\"); } module.exports = { handler };",
    "revision": {
      "revisionId": "rvsn_code456",
      "sequence": 2
    },
    "instructions": "Sum revenue by customer.",
    "dataSourceIds": [
      "data_abc123"
    ],
    "dependencyOperationIds": [],
    "dataFlowRevision": 3,
    "updatedAt": "2026-09-10T12:00:00.000Z"
  }
}