TypeScript client cho IAI Flow API. Typed methods, error handling, và cookie session management.
// iai-flow-client.ts
const API_BASE = "https://api.flow.iai.one";
class IaiFlowClient {
private async request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, {
credentials: "include",
headers: {
"Content-Type": "application/json",
...(options?.headers || {}),
},
...options,
});
if (!res.ok) {
const err = await res.text().catch(() => "");
throw new Error(err || `HTTP ${res.status}`);
}
return res.json() as Promise<T>;
}
// Auth
async getSession() {
return this.request<SessionResponse>("/api/auth/session");
}
async login(email: string, password: string) {
return this.request<SessionResponse>("/api/auth/login", {
method: "POST",
body: JSON.stringify({ email, password }),
});
}
async logout() {
return this.request("/api/auth/session", { method: "DELETE" });
}
// Flows
async listFlows() {
return this.request<{ items: Flow[] }>("/api/flows");
}
async getFlow(id: string) {
return this.request<{ workflow: Flow }>(`/api/workflows/${id}`);
}
async createFlow(payload: Partial<Flow>) {
return this.request<{ workflow: Flow }>("/api/flows", {
method: "POST",
body: JSON.stringify(payload),
});
}
async runFlow(id: string, input?: unknown) {
return this.request<{ run_id: string; status: string }>(
`/api/workflows/${id}/run`,
{ method: "POST", body: JSON.stringify({ input }) }
);
}
// Runs
async listRuns(params?: { limit?: number; status?: string }) {
const q = new URLSearchParams(params as Record<string, string>).toString();
return this.request<{ runs: Run[] }>(`/api/runs${q ? "?" + q : ""}`);
}
async getRun(id: string) {
return this.request<{ run: Run }>(`/api/runs/${id}`);
}
// Nodes
async getNodes(search?: string) {
const q = search ? `?search=${encodeURIComponent(search)}` : "";
return this.request<{ nodes: NodeCatalogEntry[] }>(`/api/nodes${q}`);
}
}
export const iai = new IaiFlowClient();
import { iai } from "./iai-flow-client";
// Login
await iai.login("you@company.com", "password");
// List flows
const { items } = await iai.listFlows();
console.log(items.map(f => f.name));
// Run a flow
const { run_id } = await iai.runFlow(items[0].id, {
topic: "AI trends 2026"
});
// Poll until done
let run;
do {
await new Promise(r => setTimeout(r, 1000));
run = await iai.getRun(run_id);
} while (run.run.status === "running");
console.log("Result:", run.run.output);
try {
const session = await iai.getSession();
if (!session.authenticated) {
// redirect to login
}
} catch (error) {
if (error.message.includes("401")) {
// session expired
}
console.error("API error:", error);
}