Skip to content

Quickstart Script

This is a complete Node.js script for testing UClaw agents from your terminal. It creates an agent, starts a run, streams the response, and cleans up.

Prerequisites

  • @uclaw/sdk installed with npm install @uclaw/sdk
  • UCLAW_API_KEY set in your environment

The Script

Save this as run.mjs in your project:

javascript
// run.mjs — UClaw terminal testing script
// Usage: UCLAW_API_KEY=uc_live_... node run.mjs "Your prompt here"
import { AppClient } from "@uclaw/sdk";

const apiKey = process.env.UCLAW_API_KEY;
if (!apiKey) {
  console.error("Error: UCLAW_API_KEY environment variable is not set.");
  console.error("  export UCLAW_API_KEY=uc_live_...");
  process.exit(1);
}

const prompt = process.argv.slice(2).join(" ") || "Hello! Introduce yourself.";
const app = new AppClient({ apiKey });

console.log(`\\nUClaw Agent\\n${"─".repeat(40)}`);
console.log(`Prompt: ${prompt}\\n`);

const agent = await app.agents.create({
  title: "Terminal agent",
  config: {
    model: "anthropic/claude-sonnet-4",
    instructions: "You are a helpful assistant.",
  },
});

const run = await agent.run(prompt);
await run.wait({ until: "running", timeoutMs: 60_000 });

process.stdout.write("Response: ");
for await (const event of run.stream()) {
  if (event.type === "text-delta") {
    process.stdout.write(event.delta);
  }
}

console.log(`\\n${"─".repeat(40)}`);
await app.agents.delete(agent.id);
console.log("Done (agent cleaned up)");

Running the Script

bash
UCLAW_API_KEY=uc_live_... node run.mjs "Explain what UClaw is in one sentence."

If your key is in .env, run:

bash
node --env-file=.env run.mjs "Write a haiku about Cloudflare Workers."

Persistent Agent (Multi-Turn)

The script above creates a fresh agent per run. If you want a multi-turn session, keep the agent alive and run multiple prompts against it:

javascript
import { AppClient } from "@uclaw/sdk";
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

const app = new AppClient({ apiKey: process.env.UCLAW_API_KEY });
const agent = await app.agents.create({
  title: "Terminal chat",
  config: {
    model: "anthropic/claude-sonnet-4",
    instructions: "You are a friendly assistant. Remember the conversation history.",
  },
});

const rl = readline.createInterface({ input, output });
console.log(`UClaw Chat (agent: ${agent.id})`);
console.log('Type your message and press Enter. Type "exit" to quit.\\n');

while (true) {
  const message = await rl.question("You: ");
  if (message.trim().toLowerCase() === "exit") break;

  const run = await agent.run(message);
  await run.wait({ until: "running", timeoutMs: 60_000 });

  process.stdout.write("Agent: ");
  for await (const event of run.stream()) {
    if (event.type === "text-delta") process.stdout.write(event.delta);
  }
  process.stdout.write("\\n\\n");
}

await app.agents.delete(agent.id);
rl.close();

Choosing a Different Model

Pass model settings inside config:

javascript
const agent = await app.agents.create({
  title: "Model test",
  config: {
    model: "openai/gpt-4.1",
    instructions: "You are a helpful assistant.",
  },
});

See the full Models list for all available model IDs and pricing.

Next Steps