Connect to Contextual
Choose the configuration and platform access needed by your first Worker.
Start with the single-image HTTP quickstart. Use Compose or a managed template when your application needs several services, platform features, or event processing.
Configure a Custom Compose Worker
The following compose.yaml defines a service using the quickstart Dockerfile and public port 8080:
x-contextual:
version: 1
services:
web:
build: .
x-contextual:
public: 8080
Use --compose compose.yaml in place of --build . when creating or updating the Worker. Keep the Dockerfile's health check. The CLI processes build: declarations before submitting the Compose definition.
Contextual supports a subset of Compose, with up to 50 services. Several services in one Worker share a network and must use distinct ports. Only named volumes are supported.
A public service needs a readiness check. A service can use the image's HEALTHCHECK, a Compose healthcheck, or an HTTP check that needs no shell or HTTP client in the image:
services:
web:
build: .
x-contextual:
public: 8080
http_healthcheck:
path: /health
port: 8080
Compose variables such as ${NAME} are not supported in a custom Compose file. Write $$ for a literal $. The Worker's environment variables are already set on every service, except variables used by Compose secrets. A service's own environment values apply only to that service and override Worker variables with the same name. Environment names that start with CONTEXTUAL_ are reserved for the platform.
Use the x-contextual Specification and Version 1 JSON Schemas when asking a coding agent to prepare a file.
Request Platform Access
A custom service requests platform access through a service-level x-contextual.features list. Enable only the features the service uses. Each feature sets environment variables in the service that requests it, and in no other service:
| Feature | What the Service Receives |
|---|---|
native-object-api |
CONTEXTUAL_NATIVE_OBJECT_URL, CONTEXTUAL_ORG_ID, and the tenant's Agent OAuth client: CONTEXTUAL_NATIVE_OBJECT_AUTH_DOMAIN, CONTEXTUAL_NATIVE_OBJECT_CLIENT_ID, CONTEXTUAL_NATIVE_OBJECT_CLIENT_SECRET, and CONTEXTUAL_NATIVE_OBJECT_AUDIENCE. |
messaging |
The Worker's topic and subscription in CONTEXTUAL_MESSAGING_TOPIC and CONTEXTUAL_MESSAGING_SUBSCRIPTION, and the tenant's Pulsar connection settings in other CONTEXTUAL_MESSAGING_* variables. The tenant must use Pulsar messaging. |
messaging-producer |
The same messaging settings, for a service that only sends messages. |
keda |
No variables. Scales the Worker on its topic backlog. Requires messaging on the same service. See Events and Schedules. |
logging |
Connection settings for publishing the Worker's logs to the tenant log topic. A tenant without its own log topic receives none. |
platform-services |
CONTEXTUAL_PLATFORM_SERVICES, a JSON object of platform API endpoints, such as Files, Connections, Logs, and AI Search. Endpoints grant no permissions. |
For example, a service that needs direct Native Object API access declares:
x-contextual:
features: [native-object-api]
Place the x-contextual.features block under the service that needs Native Object API access, alongside image or the CLI's build declaration. Read platform addresses from these variables. Do not hard-code them.
The managed-event-worker, managed-http-worker, and managed-scheduled-worker handler templates select the trigger and the available platform access; your application's worker.json defines its handlers. The template's host makes platform calls for your code, so your app requests no features and receives no credentials. See Managed Workers.
Read tenant records from a custom Worker
Enable native-object-api on the Compose service that reads records. The feature supplies the Native Object API address and Agent OAuth client. Use the Agent token to look up the tenant's native-object-api Connection. The Connection's credentials need Read access to the custom Object Types the Worker uses.
- Get an Agent token with
POST https://$CONTEXTUAL_NATIVE_OBJECT_AUTH_DOMAIN/oauth/token. Sendgrant_type: client_credentialsin the JSON body. Setclient_id,client_secret, andaudiencefromCONTEXTUAL_NATIVE_OBJECT_CLIENT_ID,CONTEXTUAL_NATIVE_OBJECT_CLIENT_SECRET, andCONTEXTUAL_NATIVE_OBJECT_AUDIENCE. Use the response'saccess_token. See Get and use an access token. - Read the Connection with
GET $CONTEXTUAL_NATIVE_OBJECT_URL/api/v1/api-configuration/native-object-api. Send the Agent token asAuthorization: Bearer <token>and setx-org-idto the value ofCONTEXTUAL_ORG_ID. This request needs the Connection's credential fields, so omitdoNotDecryptand keep the response inside the Worker. - Get a token for record reads from that Connection. For
client-grant, send aclient_credentialsrequest toauthEndpointusing the Connection'sclientIdandclientSecretasclient_idandclient_secret. Includeaudienceandscopeif set, and use the Connection's configured content type or JSON by default. Forbearer, usebearerTokenas the record token. - Read a custom record with
GET $CONTEXTUAL_NATIVE_OBJECT_URL/api/v1/<type>/<id>?doNotDecrypt=true. Send the record token from step 3 in the Authorization header and the samex-org-idheader. Encode the type and record ID as URL path segments. ThedoNotDecryptparameter keeps secret values out of this response.
Secrets and Configuration Files
Worker environment variables are stored encrypted, and every service receives them unless a Compose secret uses them. Set secret values in the admin console under Environment variables. The CLI's --env flag puts the value in your shell history.
To deliver a secret as a file, declare a Compose secret that names a Worker environment variable in upper case. The service reads it from /run/secrets/<name>:
secrets:
api_key:
environment: API_KEY
services:
web:
build: .
secrets: [api_key]
A variable used this way is delivered only as the file. Only the services that list the secret receive the file, and no service receives the variable as an environment variable. A secret that no service lists has no effect. Secrets with a file: source are not supported.
To deliver a configuration file, declare a Compose config. The CLI reads a file: source and submits its content. The service reads it, read-only, at the target path:
configs:
settings:
file: ./settings.json
services:
web:
build: .
configs:
- source: settings
target: /app/settings.json
Configuration files are limited to 64 KiB in total. Compose variables are not supported in their content.
Events and Schedules
An event Worker consumes the topic named for its Worker ID, through the tenant's topic template. Send it events the same way you send events to an Agent with that ID.
For event processing, use the managed-event-worker template. The template delivers each event to your handler and acknowledges it only after the handler succeeds. A failed event is redelivered, and there is no dead-letter queue. Your handler can receive the same event more than once, so make repeat processing safe. See Templates and Options.
A custom Compose service can consume the topic itself with messaging and keda. Configure its backlog scaling under the same service's x-contextual.scaling:
services:
consumer:
build: .
x-contextual:
public: false
features: [messaging, keda]
scaling:
minReplicas: 0
maxReplicas: 3
The minimum defaults to 0, which scales the Worker to zero while the backlog is empty. A Worker with a public service needs a minimum of at least 1. A Compose service is public only when it sets x-contextual.public. Backlog scaling replaces the Worker's replica and CPU settings.
An Object Type trigger can send an event containing record data to an event Worker after a record is inserted, updated, or deleted. The trigger's Agent picker lists Workers that scale on their event backlog, such as Workers using managed-event-worker.
For a scheduled job, use a UTC cron schedule through --schedule, with an application or scheduled template that finishes after each tick. Scheduled Workers have no public endpoint. The quickstart HTTP server keeps running and is not a scheduled-job example. See Scheduled Workers for run limits.
Find available template names and versions in the console's Managed worker fields. To build an event or scheduled handler, follow Build Your First Managed Worker and choose the template in Templates and Options. For a coding agent, see Build with a Coding Agent.
Records and Files
Keep durable application state in Native Objects or an external data store. See Runtime Environment for the writable paths and storage limits in a Worker.
A Worker receives platform credentials only through the features its services request. A managed Worker reads and writes records through its host without credentials. It moves large attachment files straight to and from the Files API, with a short-lived token for that one file. See Platform Capabilities.