Agents Endpoints

Implementation: inc/Api/Agents.php, inc/Core/Auth/AgentAuthorize.php, inc/Core/Auth/AgentAuthCallback.php

Base URL: /wp-json/datamachine/v1

Overview

Agent endpoints manage agent records, user access grants, runtime bearer tokens, and the browser authorization flow used by external agent clients.

Authentication

There are three auth modes:

  • Agent CRUD, access, and token management use the current WordPress user and Data Machine agent capabilities.
  • GET /agents/me accepts either an agent bearer token context or a logged-in WordPress user.
  • /agent/authorize and /agent/auth/callback are browser-facing flow endpoints with open REST permissions, then validate login cookies, nonces, redirect URIs, or callback payloads inside the handler.

Token values are sensitive. POST /agents/{agent}/tokens returns raw_token once; list endpoints return token metadata only.

Agent bearer-token requests populate PermissionHelper agent context. Token capabilities can be restricted by a capability ceiling, so a token can be narrower than the owning agent’s full Data Machine capability set.

Route Table

MethodRouteAuth modelPurpose
GET/agentsLogged-in userList agents visible to the caller.
POST/agentsmanage_agents or create_own_agentCreate an agent.
GET/agents/meAgent token or logged-in userDiscover the current agent identity.
GET/agents/{agent}manage_agentsFetch an agent by slug or numeric ID.
PUT/PATCH/agents/{agent}manage_agentsUpdate agent display name or config.
DELETE/agents/{agent}manage_agentsDelete an agent.
GET/agents/{agent}/accessmanage_agentsList user access grants.
POST/agents/{agent}/accessmanage_agentsGrant user access.
DELETE/agents/{agent}/access/{user_id}manage_agentsRevoke user access.
GET/agents/{agent}/tokensmanage_agents plus agent access checkList token metadata.
POST/agents/{agent}/tokensmanage_agents plus admin access to agentCreate a bearer token.
DELETE/agents/{agent}/tokens/{token_id}manage_agents plus admin access to agentRevoke a token.
GET/agent/authorizeBrowser sessionShow consent or redirect to login.
POST/agent/authorizeBrowser session plus nonceApprove or deny authorization.
GET/agent/auth/callbackCallback payloadReceive and store an external token.
GET/agent/auth/tokensmanage_optionsList stored external token metadata.
GET/agent/auth/tokens/{key}manage_optionsReturn one stored external token record.

{agent} accepts an agent slug (sarai) or numeric agent ID (42). Slug routes are preferred.

Core Parameters

Agent CRUD

ParameterRoutesTypeNotes
agent_slugPOST /agents, authorize flowstringRequired when creating or authorizing. Sanitized as a slug.
agent_namePOST /agents, PUT/PATCH /agents/{agent}stringDisplay name. Defaults to slug on create.
configPOST /agentsobjectInitial config object.
agent_configPUT/PATCH /agents/{agent}objectReplaces existing config.
delete_filesDELETE /agents/{agent}booleanAlso remove the agent filesystem directory. Default false.
scopeGET /agentsstringmine or all; all requires admin privileges.
user_idGET /agents, access routesintegerAdmin-only filter for list, required for grants/revokes.
include_roleGET /agentsbooleanInclude caller role data. Enabled by default for REST UI payloads.

Access And Tokens

ParameterRoutesTypeNotes
rolePOST /agents/{agent}/accessstringadmin, operator, or viewer. Default viewer.
labelPOST /agents/{agent}/tokens, authorize flowstringHuman-readable token label such as kimaki-prod.
capabilitiesPOST /agents/{agent}/tokensarrayOptional allowed capability subset. Omit/null for all agent capabilities.
expires_inPOST /agents/{agent}/tokensintegerExpiry in seconds from now. Omit/null for no expiry.
token_idDELETE /agents/{agent}/tokens/{token_id}integerToken metadata ID to revoke.

Browser Authorization

ParameterRoutesTypeNotes
redirect_uri/agent/authorizestringRequired. Validated against the agent allowlist or localhost rules.
actionPOST /agent/authorizestringauthorize or deny.
_authorize_noncePOST /agent/authorizestringWordPress nonce from the consent form.
code_challenge/agent/authorizestringOptional PKCE-style challenge.
code_challenge_method/agent/authorizestringOptional challenge method.
state/agent/authorizestringOptional opaque client state echoed through redirects.
token, agent_slug, agent_id, error/agent/auth/callbackmixedCallback result fields from the remote authorizing site.
key/agent/auth/tokens/{key}stringStorage key in the form remote-site/agent-slug.

Response Shape

Most agent management responses use:

json
{
  "success": true,
  "data": {}
}

List responses return data as an array of agent or grant objects. Token ability responses return ability-native objects such as:

json
{
  "success": true,
  "token_id": 123,
  "raw_token": "datamachine_...",
  "token_prefix": "datamachine_abcd",
  "message": "Token created. Save it now - it cannot be retrieved again."
}

GET /agents/me returns identity metadata:

json
{
  "success": true,
  "data": {
    "agent_id": 2,
    "agent_slug": "sarai",
    "agent_name": "Sarai",
    "owner_id": 1,
    "site_url": "https://example.com",
    "site_name": "Example"
  }
}

Agent Usage Examples

Create a token for an external agent client:

bash
curl -X POST https://example.com/wp-json/datamachine/v1/agents/sarai/tokens 
  -H "Content-Type: application/json" 
  -u username:application_password 
  -d '{"label":"kimaki-prod","expires_in":2592000}'

Use the returned bearer token to discover the active identity:

bash
curl https://example.com/wp-json/datamachine/v1/agents/me 
  -H "Authorization: Bearer datamachine_..."

Start the browser authorization flow for a local client:

text
https://example.com/wp-json/datamachine/v1/agent/authorize?agent_slug=sarai&redirect_uri=http://localhost:31337/callback&label=local-cli&state=abc123