Skip to main content

Full example (Node.js)

const API = "https://agentcell.live/api";
const KEY = "YOUR_API_KEY";
const headers = { "X-API-Key": KEY, "Content-Type": "application/json" };

// 1. Create session
const session = await fetch(`${API}/sessions`, {
  method: "POST",
  headers,
  body: "{}",
}).then(r => r.json());

const sid = session.id;
console.log(`Session ${sid}${session.status}`);

// 2. Wait for boot
while (true) {
  const s = await fetch(`${API}/sessions/${sid}`, { headers }).then(r => r.json());
  if (s.status === "ready") break;
  if (s.status === "failed") throw new Error(s.error_message || "boot failed");
  console.log(`  status: ${s.status}...`);
  await new Promise(r => setTimeout(r, 2000));
}

// 3. Run a task
const result = await fetch(`${API}/sessions/${sid}/agent/task`, {
  method: "POST",
  headers,
  body: JSON.stringify({ task: "Search DuckDuckGo for the weather in Tokyo" }),
}).then(r => r.json());

console.log(result.status);  // "completed" or "failed"
console.log(result.summary);

// 4. Clean up
await fetch(`${API}/sessions/${sid}`, { method: "DELETE", headers });
console.log("Session deleted");

TypeScript types

interface Session {
  id: string;
  status: "creating" | "booting" | "ready" | "stopping" | "stopped" | "failed";
  stream_url: string | null;
  adb_serial: string | null;
  trial_expires_at: string | null;
  created_at: string;
  updated_at: string;
}

interface TaskResult {
  status: "completed" | "failed";
  summary: string;
  steps: Array<{
    tool: string;
    args: Record<string, unknown>;
    ok: boolean | null;
  }>;
}

Helper wrapper

class AgentCell {
  private api: string;
  private headers: Record<string, string>;

  constructor(apiKey: string, baseUrl = "https://agentcell.live/api") {
    this.api = baseUrl;
    this.headers = { "X-API-Key": apiKey, "Content-Type": "application/json" };
  }

  async createSession(): Promise<Session> {
    return fetch(`${this.api}/sessions`, {
      method: "POST", headers: this.headers, body: "{}",
    }).then(r => r.json());
  }

  async waitForReady(sessionId: string, timeoutMs = 120000): Promise<Session> {
    const start = Date.now();
    while (Date.now() - start < timeoutMs) {
      const s = await fetch(`${this.api}/sessions/${sessionId}`, {
        headers: this.headers,
      }).then(r => r.json());
      if (s.status === "ready") return s;
      if (s.status === "failed") throw new Error(s.error_message || "boot failed");
      await new Promise(r => setTimeout(r, 2000));
    }
    throw new Error("Boot timeout");
  }

  async runTask(sessionId: string, task: string): Promise<TaskResult> {
    return fetch(`${this.api}/sessions/${sessionId}/agent/task`, {
      method: "POST",
      headers: this.headers,
      body: JSON.stringify({ task }),
    }).then(r => r.json());
  }

  async deleteSession(sessionId: string): Promise<void> {
    await fetch(`${this.api}/sessions/${sessionId}`, {
      method: "DELETE", headers: this.headers,
    });
  }
}

// Usage:
const agent = new AgentCell("YOUR_API_KEY");
const session = await agent.createSession();
await agent.waitForReady(session.id);
const result = await agent.runTask(session.id, "Open Settings");
console.log(result.summary);
await agent.deleteSession(session.id);