Skip to Content
Docs

Build Your First Managed Worker

Write an HTTP handler in TypeScript, Rust, Go, or Python, package it with the runtime adapter, and deploy it with the managed HTTP template.

Build a managed HTTP Worker that returns a greeting and writes a log entry for each request. You write the handler and a worker.json manifest. You package them with the runtime adapter image and deploy them with the managed-http-worker template.

Before You Start

You need:

  • A tenant with Workers enabled, and permission to create Workers and push images.
  • The Contextual CLI, signed in, with Docker configured for the image hosts. Follow step 1 of Deploy Your First Worker.
  • Docker that builds linux/amd64 images.
  • For TypeScript, Node.js 24 or later. The adapter image runs Node.js 24, and the bundle targets it.

The example uses Worker ID hello-managed and CLI configuration ID my-config. Replace them with your own.

1. Write the Handler and Manifest

Create a project directory. Add worker.json, the release manifest. It names the app, its handlers, the triggers each handler accepts, and the JSON Schema of its configuration:

JSON
{
  "workerId": "hello-managed",
  "version": "1.0.0",
  "handlers": { "default": { "triggers": ["http"] } },
  "configSchema": {
    "type": "object",
    "additionalProperties": false,
    "required": ["greeting"],
    "properties": { "greeting": { "type": "string" } }
  }
}

Then add the handler in your language. In every language, the handler logs through the host and returns an HTTP response object: status, optional headers, and a string body.

Only the TypeScript adapter checks the Worker's CONFIG against configSchema, when the Worker starts. The Rust, Go, and Python adapters pass the configuration to your handler unchecked, so check the values you read.

Add "entrypoint": "index.cjs" to worker.json. Install the build tools and save the context types, as Get the Runtime Adapters shows. Adapter 0.10.0 has no library to import: your module default-exports the Worker, and the adapter supplies context at run time. Save src/index.ts:

TypeScript
import type { Context, Invocation } from "./worker-types"

type Config = { greeting: string }

export default {
  apiVersion: "1",
  name: "hello-managed",
  version: "1.0.0",
  // The TypeScript adapter has already checked the configuration against worker.json's configSchema.
  parseConfig: (raw: unknown) => raw as Config,
  async run(event: Invocation, context: Context<Config>) {
    await context.logger.info("Request received", { id: event.id, trigger: event.trigger.type })
    if (event.trigger.type !== "http") return
    return {
      status: 200,
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ message: context.config.greeting, path: event.trigger.path }),
    }
  },
}

Save tsconfig.json so you can type-check before you bundle:

JSON
{
  "compilerOptions": { "strict": true, "target": "es2024", "module": "preserve", "noEmit": true, "types": ["node"] },
  "include": ["src"]
}

2. Build

Compile the handler for linux/amd64.

Type-check, then bundle the handler and its imports into one CommonJS file:

Terminal
npx tsc -p .
npx esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --outfile=dist/index.cjs

3. Package the Release

Run the adapter image's package command. It checks worker.json, hashes the release, and writes .worker-image: a Docker build directory whose image is built FROM the adapter and labeled with the release and its digest. Saving a managed Worker whose image wasn't packaged this way fails with "A managed host must depend on exactly one release-labelled application".

docker run --rm --platform linux/amd64 -u "$(id -u):$(id -g)" -v "$PWD:/work" -w /work \
  images.contextual.io/worker/base/worker-adapter-node:0.10.0 \
  package worker.json dist .worker-image

Add the build output, the installed packages, and the copied library to .gitignore: .worker-image/, build/, dist/, node_modules/, .contextual/, and contextual-worker/.

4. Create the Worker

Save the Worker's configuration in config.json. It must be a JSON object. The TypeScript adapter also checks it against configSchema when the Worker starts:

JSON
{ "greeting": "Hello from a managed Worker" }

Create the Worker with the managed HTTP template:

Terminal
ctxl workers create hello-managed \
  --size small \
  --managed managed-http-worker:0.10.0 \
  --build .worker-image \
  --option-from-file CONFIG=config.json \
  --config-id my-config \
  --pretty

The workers create command builds .worker-image, pushes the image to your organization's image host, and sets it as the template's IMAGE option. It then saves the Worker and starts deployment. Record the Worker version and image reference from the response.

5. Verify the Deployment

Open Workers > hello-managed > Deployment in Console V2. Wait until the instance is ready, then copy the Public Worker URL. Replace <worker-url> below with it:

Terminal
curl --fail <worker-url>/orders/1

Expect {"message":"Hello from a managed Worker","path":"/orders/1"}.

Read the handler's log entries from the tenant log pipeline:

Terminal
ctxl logs hello-managed --since 10m --config-id my-config --pretty

Expect one Request received entry for each request. Entries reach the tenant log pipeline only when the tenant has its own contextual/logs topic. The host also writes every entry to its own container output, so you can always read them there:

Terminal
ctxl workers logs hello-managed -c host --config-id my-config

See Read Logs.

6. Change Configuration or Code

To change the greeting without rebuilding, edit config.json and update the option:

Terminal
ctxl workers update hello-managed \
  --option-from-file CONFIG=config.json \
  --config-id my-config \
  --pretty

To deploy a code change, remove the previous package output, repeat steps 2 and 3, then update with the new build. The package command refuses an output directory that already exists:

Terminal
rm -rf .worker-image
Terminal
ctxl workers update hello-managed \
  --build .worker-image \
  --config-id my-config \
  --pretty

The update keeps the template selection and the options you do not supply. Raise the version in worker.json and in your code for each release, so logs and diagnostics identify it: the default export in TypeScript, Cargo.toml in Rust, and the Worker name and version in Go and Python. The name and version in your code must match worker.json, or the Worker doesn't start.

7. Remove the Test Worker

Terminal
ctxl records delete native-object:worker/hello-managed --config-id my-config

The records delete command deletes the Worker immediately, without a confirmation prompt.

Next Steps