Agents API
Start, stop, restart, and check the running deployments of your Agents from code.
The Agents API controls and checks the running deployments of your Agents. Use it from scripts and backend services that operate Agents. For example, a maintenance script can stop an Agent and start it again afterward, or a monitoring job can check that its instances are running. The CLI's ctxl agents instances and ctxl agentmeta commands also use it to check an Agent's instances and to list the available Agent sizes, images, and npm packages.
Agent definitions are Records of the agent Object Type. Create, change, and delete them through the Native Object API at /api/v1/agent. In the Admin Console, you manage Agents under Components → Agents.
What you can do
- Check an Agent's instances, their status, and when each started.
- Start, stop, or restart one Agent, or several Agents at once.
- Read and change an Agent's log level.
- List the Agent sizes, runtime image versions, and npm packages available to your tenant.
- Run a scheduled Agent on demand through its
manual-runAction in the Native Object API.
The reference also lists operations that the platform calls when an Agent Record changes: POST /agents, PUT /agents/{agentId}, DELETE /agents/{agentId}, and the [\$]event operations. These lifecycle callbacks aren't an interface for editing Agents, so don't call them directly. They don't create, change, or delete the Agent Record, and some act on the running deployment without updating the Record. The platform calls them for you when you create, change, or delete an Agent Record through the Native Object API.
Base URL and reference
- API base URL:
https://agents.<tenant-id>.my.contextual.io/api/v1 - Swagger reference: Agents API reference
- Raw OpenAPI: openapi.json
Replace <tenant-id> with the tenant ID from your Tenant Workspace URL. On the API base URL, the hostname identifies your tenant, so the x-org-id header is optional. If you send it, its value must match the hostname.
Replace <agent-id> with the Agent's ID, shown in the Agent ID column of the Agents list. In an Agent Record, this is the name field.
The OpenAPI document uses the relative server /api/v1. When you import it into an API client or AI tool, set the server to the API base URL.
Authentication and permissions
Send an access token from a tenant API Key in the Authorization: Bearer <access-token> header. To get one, follow Get an access token, which also explains when you can reuse a token with other APIs.
Set the key's permissions on its Permissions tab, under Service components → Agents. The Native Object API uses the same permissions for Agent Records.
Each Agents API operation on this page that targets a specific Agent reads that Agent's Record with your token. A bulk request reads the Records of the Agents it selects. Include Read for these operations.
The manual-run Action also needs Read in addition to Execute: the Action calls the Agents API, which reads the Agent Record using your token.
| Task | Permissions to grant |
|---|---|
| Check an Agent's instances or read its log level | Service components → Agents → Read |
| Change one Agent's log level | Service components → Agents → Read and Update |
Run a scheduled Agent with the manual-run Action |
Service components → Agents → Read and Execute |
Request and response details
- Request and response bodies are JSON.
- Start, stop, and restart return
204when the request is accepted. The Agent then changes state in the background. Check its instances to follow the change. Starting an Agent that isn't stopped, or stopping one that already is, returns400. Scheduled Agents can't be restarted. PUT /agentsapplies anaction(start,stop,restart, orset-log-level) to the Agents listed inids. If you omitidsor send an empty list, the request selects every Agent returned by the Native Object API for your tenant. Of the selected Agents,startacts only on stopped ones.stop,restart, andset-log-levelskip stopped ones, andrestartalso skips scheduled Agents.GET /agents/{agentId}/instancesreturns instances initems, each with aname,status,createdAt, andstartedAt. Operations explainsstatusvalues such asRunningandOOMKilled.- To run a scheduled Agent on demand, send
PATCH https://native-object.<tenant-id>.my.contextual.io/api/v1/agent/<agent-id>/actions/manual-runwith the body{}. The Agent must not be stopped.
Example request
This request lists an Agent's instances:
curl "https://agents.<tenant-id>.my.contextual.io/api/v1/agents/<agent-id>/instances" \
--header "Authorization: Bearer <access-token>"
Replace <access-token> with your access token. A successful response has status 200 and one entry in items for each instance.