Code examples thật cho các use cases phổ biến nhất.
Dùng trên mọi protected page của dash.iai.one:
// auth-guard.js
const API_BASE = "https://api.flow.iai.one";
async function requireAuth() {
try {
const res = await fetch(`${API_BASE}/api/auth/session`, {
credentials: "include"
});
if (res.status !== 200) throw new Error("Unauthenticated");
const data = await res.json();
if (!data.authenticated) throw new Error("Unauthenticated");
return data; // { user, workspace }
} catch {
const next = encodeURIComponent(location.pathname);
location.replace(`/login/?next=${next}`);
return null;
}
}
// Usage
const auth = await requireAuth();
if (!auth) return; // redirect already triggered
console.log("Hello,", auth.user.name);
const API = "https://api.flow.iai.one";
async function runAndWait(flowId, input) {
// Trigger run
const { run_id } = await fetch(`${API}/api/workflows/${flowId}/run`, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input })
}).then(r => r.json());
// Poll for completion
for (let i = 0; i < 30; i++) {
await new Promise(r => setTimeout(r, 1000));
const { run } = await fetch(`${API}/api/runs/${run_id}`, {
credentials: "include"
}).then(r => r.json());
if (run.status === "success") return run.output;
if (run.status === "failed") throw new Error(run.error_text);
}
throw new Error("Timeout: run did not complete in 30s");
}
async function loadStepLogs(runId) {
const res = await fetch(
`https://api.flow.iai.one/api/runs/${runId}/steps`,
{ credentials: "include" }
);
const { steps } = await res.json();
steps.forEach(step => {
console.log(
`[${step.status.toUpperCase()}]`,
step.node_name || step.node_id,
`(${step.node_type})`,
`— ${step.duration_ms}ms`
);
});
}
const res = await fetch("https://api.flow.iai.one/api/agent/run", {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
agent_id: "research-assistant",
input: { topic: "AI trends in Vietnam 2026" },
tools: ["http.call", "memory.write", "log.write"]
})
}).then(r => r.json());
console.log("Agent run ID:", res.run_id);
console.log("Iterations:", res.iterations);
console.log("Output:", res.output);
const flow = await fetch("https://api.flow.iai.one/api/flows", {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "My Automated Flow",
status: "draft",
definition: {
name: "My Automated Flow",
entry: "trigger_1",
nodes: [
{ id: "trigger_1", type: "manual_trigger", name: "Start", config: {} },
{ id: "ai_1", type: "claude", name: "Process", config: {
prompt: "Summarize: {{input.text}}"
}},
{ id: "log_1", type: "log", name: "Done", config: {} }
],
edges: [
{ id: "e1", source: "trigger_1", target: "ai_1" },
{ id: "e2", source: "ai_1", target: "log_1" }
]
}
})
}).then(r => r.json());
console.log("Created flow:", flow.workflow.id);
const { runs } = await fetch(
"https://api.flow.iai.one/api/runs?status=failed&limit=20",
{ credentials: "include" }
).then(r => r.json());
runs.forEach(run => {
console.log(run.id, run.workflow_name, run.error_text);
});