Developer Platform Copy

API Guide & Reference

Comprehensive REST API reference for integrating BetterContext's causal memory graph, ingestion pipelines, and agent guardrails.

The BetterContext REST API allows developers to programmatically query knowledge graphs, upsert episodic & procedural memories, evaluate causal interventions, and manage workspace access controls.

Base URL: https://api.bettercontext.dev/v1
All API requests must be transmitted over HTTPS. JSON is returned in all endpoints.

Authentication

BetterContext authenticates API requests using Bearer tokens. Pass your API secret key in the Authorization HTTP header of every request:

Authorization: Bearer bc_live_99f8e3a2b1c4d5e6f7a8b9c0
Keep your secret keys secure: Never expose API keys in client-side code, public GitHub repos, or browser scripts. Use server environment variables (e.g. BETTERCONTEXT_API_KEY).

Core REST Endpoints

Method Endpoint Description
POST /v1/context/query Query memory graph with synergistic context packing and ACL filtering.
POST /v1/memory/upsert Ingest raw text, markdown, or structured facts into the memory graph.
GET /v1/graph/nodes Retrieve nodes, entity relationships, and half-life confidence scores.
POST /v1/interventions/evaluate Focus Mode causal calculation: evaluate direct effect $P(Y|do(X))$.
DELETE /v1/memory/{id} Prune or forget specific facts with dual-clock tombstoning.

Code Examples: Querying Context

Querying the memory graph with prompt text, agent identifier, and role ACLs:

Python SDK:

import os
from bettercontext import BetterContextClient

client = BetterContextClient(api_key=os.environ.get("BETTERCONTEXT_API_KEY"))

# Retrieve grounded, de-confounded context for a coding agent
response = client.context.query(
    prompt="Refactor the authentication middleware to support JWT refresh tokens",
    agent_id="cursor-agent-01",
    user_role="engineer",
    max_tokens=2048,
    focus_mode=True
)

print("Synthesized Context:", response.context_string)
print("Causal Attribution:", response.causal_delta)
print("Graph Nodes Cited:", [node.title for node in response.citations])

TypeScript / Node.js:

import { BetterContext } from '@bettercontext/sdk';

const bc = new BetterContext({
  apiKey: process.env.BETTERCONTEXT_API_KEY,
});

async function run() {
  const context = await bc.context.query({
    prompt: "Refactor the authentication middleware to support JWT refresh tokens",
    agentId: "claude-code-worker",
    userRole: "engineer",
    maxTokens: 2048,
    focusMode: true,
  });

  console.log("Packed Tokens:", context.tokensUsed);
  console.log("Citations:", context.citations);
}

run();

cURL:

curl -X POST https://api.bettercontext.dev/v1/context/query \
  -H "Authorization: Bearer $BETTERCONTEXT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Refactor auth middleware for JWT refresh",
    "agent_id": "cli-agent",
    "max_tokens": 2048,
    "focus_mode": true
  }'

Rate Limits & Error Handling

BetterContext enforces tiered request throughput per API key:

  • Free Tier: 60 requests / minute, 10,000 queries / month.
  • Pro Tier: 600 requests / minute, unlimited queries (fair use).
  • Max & Scale: 3,000+ requests / minute with dedicated high-throughput edge nodes.

Standard HTTP status codes are returned:

  • 200 OK — Request succeeded.
  • 400 Bad Request — Malformed JSON or invalid schema.
  • 401 Unauthorized — Missing or invalid API key.
  • 403 Forbidden — Agent does not have ACL permission for requested resources.
  • 429 Too Many Requests — Rate limit exceeded. Back off exponentially.

Next Steps

Was this page helpful?

Powered By BetterContext