Skip to main content

Programmatic Context Primary Keys

This guide covers everything you need to provision, query, and manage context primary keys through the Orchestrator API. Use it alongside the user-facing overview when you need to automate onboarding, hydration pipelines, or large-scale migrations.

Prerequisites

To call these endpoints you need:

  • A valid JWT signed with the owner wallet of the agent you manage
  • The agent's collection address and token ID
  • A chosen context primary key value that matches your domain format

Generate a JWT with the official helper project:

# Clone the repository
git clone https://github.com/6022protocol/py-generate-jwt

# Install dependencies
cd py-generate-jwt
pip install -r requirements.txt

# Generate token
python generate_jwt.py \
--chain-id 137 \
--api-base-url https://<your-orchestrator-api-host>/
NetworkChain ID
Polygon Mainnet137
Amoy Testnet80002

Tokens are currently valid for one month (planned to decrease to one day). Rotate credentials whenever team members change.

REST Endpoints

All endpoints live under the orchestrator API base URL — replace the placeholder with the host your team operates:

https://<your-orchestrator-api-host>/

Swagger documentation: {base_url}/swagger/index.html

List Context Primary Keys

GET /agent/{agentCollectionAddress}/{agentTokenId}/contexts/keys
Authorization: Bearer

Path parameters:

ParameterTypeDescription
agentCollectionAddressstringAgent NFT collection address (0x...)
agentTokenIdstringAgent's token ID within collection

Query parameters:

ParameterTypeDescriptionDefault
pageuintPage number (1-indexed).1
elementsPerPageuintPage size, clamped between 1 and 100.10
searchstringOptional substring filter applied to context keys.
sortBystringSort field. Only key is currently supported.key
sortDirectionstringSort order (asc or desc).asc

Successful response (200 OK):

{
"page": 1,
"elementsPerPage": 10,
"totalElements": 3,
"contextKeys": ["C45653", "C78901", "C12345"]
}

Create Context Primary Key

POST /agent/{agentCollectionAddress}/{agentTokenId}/context
Authorization: Bearer
Content-Type: application/json

Body:

{
"key": "C45653"
}
  • 200 OK: key provisioned (empty response body)
  • 400 Bad Request: validation failure (bad format, not owner, agent not internal/expert)
  • 500 Internal Server Error: downstream storage error (for example, duplicate keys at persistence time)

Delete Context Primary Keys

Deletion is intentionally not exposed through the public API. Use database tooling in exceptional cases only (see Administrative access below).

Bulk Provisioning Example (Python)

import requests

BASE_URL = "https://<your-orchestrator-api-host>"
AGENT_ADDRESS = "0xCOLLECTION"
AGENT_TOKEN_ID = "2"
JWT = "your_jwt_token_here"

headers = {
"Authorization": f"Bearer {JWT}",
"Content-Type": "application/json",
}

keys_to_create = ["C45653", "C78901", "C12345", "C67890"]

for key in keys_to_create:
response = requests.post(
f"{BASE_URL}/agent/{AGENT_ADDRESS}/{AGENT_TOKEN_ID}/context",
headers=headers,
json={"key": key},
)

if response.status_code == 200:
print(f"✅ Created: {key}")
elif response.status_code == 400 and "already exists" in response.text:
print(f"⏭️ Skipped (exists): {key}")
else:
print(f"❌ Error for {key}: {response.text}")

Managing Keys Programmatically

ActionEndpointNotes
List keysGET /agent/{address}/{tokenId}/contexts/keysReturns paginated envelope with contextKeys
Create keyPOST /agent/{address}/{tokenId}/contextIdempotent thanks to duplicate check
Hydrate memoryPOST /agent/{address}/{tokenId}/context/{contextKey}/messageSee Memory Hydration API

When building automations, always validate the key format locally before calling the API to avoid noisy 400 responses.

Administrative Access (Direct DB)

Direct database queries should only be used for break-glass situations. Each agent has its own isolated PostgreSQL database — there is no agent_id discriminator column on agent_contexts. The agent identity is the database connection itself, so connect to the correct per-agent database first, then query:

-- Connect to the agent's own database, then:
SELECT key, created_at, updated_at FROM agent_contexts;

-- Count context primary keys
SELECT COUNT(*) FROM agent_contexts;

Internal threading scratchpads stored under the conversation-context-discovery/ prefix will appear in raw queries but are filtered out of the user-facing API response (LIST /contexts/keys).

Coordinate with the platform team before running manual SQL to avoid violating tenant isolation guarantees.

Troubleshooting

StatusErrorCauseFix
400agent context with key X already existsKey previously createdTreat as success, skip duplicates
400You are not the owner of this agentJWT signed with wrong walletRegenerate JWT with the current owner
401UnauthorizedMissing or invalid JWTRefresh token, confirm header formatting
404Context not foundKey missing when hydrating memoryCreate key before pushing conversations
500Internal Server ErrorDatabase or sync issueCheck orchestrator logs, open ticket if needed

For UI-driven workflows, see the user documentation.