Skip to content

REST API

http(s)://your-cosmictron-host:3000

All endpoints require a Authorization: Bearer <token> header unless noted as public.

Create a new identity with email/password.

Terminal window
curl -X POST /v1/auth/sign-up \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "secret"}'

Response:

{ "token": "eyJ...", "identity": "abc123", "expires_at": 1735689600 }

Authenticate with email/password.

Send a magic link to an email address.

Verify a magic link token and return a session token.


Upload and deploy a module.

Terminal window
curl -X POST /v1/modules \
-H "Authorization: Bearer $TOKEN" \
-F "name=my-agent" \
-F "module=@my_agent.wasm"

Response:

{
"name": "my-agent",
"version": "sha256:a3f2...",
"status": "active",
"reducers": ["record_agent_message", "record_tool_call", "complete_tool_call"]
}

List deployed modules.

Get module details.

Undeploy a module.


Invoke a reducer by name.

Terminal window
curl -X POST /v1/reducers/record_agent_message \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"session_id": "abc", "role": "user", "content": "Hello"}'

Response:

{ "tx_id": 12345, "status": "committed", "duration_ms": 2 }

Execute a read-only SQL query.

Terminal window
curl -X POST /v1/sql \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"query": "SELECT * FROM agent_state WHERE session_id = $1", "params": ["session-abc"]}'

Response:

{
"columns": ["id", "session_id", "role", "content", "created_at"],
"rows": [
[1, "session-abc", "user", "Hello", 1735689600000000]
]
}
{
"query": "SELECT * FROM agent_state AS OF TX 1234567"
}

Liveness probe. Returns 200 OK if the process is running.

Readiness probe. Returns 200 OK when WAL and storage are ready.

Full diagnostic JSON. See Monitoring.


#[table(name = "users", public)]
#[rls(read = "owner_id = $identity")]
pub struct User {
#[primary_key]
#[auto_inc]
pub id: u64,
pub username: String,
}

Table attributes: name (required), public, event (append-only).

Field attributes: #[primary_key], #[auto_inc], #[index], #[unique], #[fk(table.column)], #[pii].

#[reducer]
pub fn create_user(ctx: &ReducerContext, username: String) { ... }

Lifecycle variants: #[reducer(init)], #[reducer(client_connected)], #[reducer(client_disconnected)].

#[rls(read = "author_id = $identity OR is_public = true")]
#[rls(write = "author_id = $identity")]

Variables: $identity (current caller’s identity).

#[view]
pub fn active_sessions() -> impl Iterator<Item = AgentSession> {
AgentSession::filter(|s| s.status == "active")
}

Terminal window
bun add @cosmictron/client
import { CosmictronClient } from '@cosmictron/client';
const client = new CosmictronClient('wss://your-host:3000');
// Auth
await client.signIn({ email: 'user@example.com', password: 'secret' });
// Call a reducer
await client.callReducer('create_user', { username: 'alice' });
// Subscribe to a query
const sub = await client.subscribe('SELECT * FROM users');
sub.on('update', ({ inserts, deletes }) => { ... });
sub.unsubscribe();
// Direct SQL query (read-only)
const rows = await client.query('SELECT * FROM users WHERE id = $1', [1]);
import { useQuery, useReducer, useIdentity } from '@cosmictron/react';
function UserList() {
const users = useQuery('SELECT * FROM users');
const createUser = useReducer('create_user');
return (
<ul>
{users.rows.map(u => <li key={u.id}>{u.username}</li>)}
</ul>
);
}