Chuyển tới nội dung chính

TypeScript SDK Quickstart

Prerequisites

  • Node.js 18+ or a modern browser
  • A FAOS API key (get one here)

Install

npm install @faos/sdk

Initialize

import { FaosClient } from '@faos/sdk';

const client = new FaosClient({
apiKey: process.env.FAOS_API_KEY ?? 'faos_sk_your_key_here',
});
<PlaygroundButton code={"import { FaosClient } from '@faos/sdk';\n\nconst client = new FaosClient({\n apiKey: process.env.FAOS_API_KEY ?? 'faos_sk_your_key_here',\n});"} lang="typescript" />

Invoke an Agent

import { FaosClient } from '@faos/sdk';

const client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });

// Basic invocation
const response = await client.agents.invoke(
'credit-risk-analyst',
{ query: 'Analyze Q1 2024 financial statements' },
);
console.log(response.result);
<PlaygroundButton code={"import { FaosClient } from '@faos/sdk';\n\nconst client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });\n\n// Basic invocation\nconst response = await client.agents.invoke(\n 'credit-risk-analyst',\n { query: 'Analyze Q1 2024 financial statements' },\n);\nconsole.log(response.result);"} lang="typescript" />

Typed Responses with Generics

import { FaosClient } from '@faos/sdk';

interface CreditAnalysis {
insights: string[];
riskScore: number;
recommendation: string;
}

const client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });
const res = await client.agents.invoke<CreditAnalysis>(
'credit-risk-analyst',
{ query: 'Analyze Q1 2024' },
);

res.result.insights; // string[] — fully typed!
res.result.riskScore; // number
res.result.recommendation; // string
<PlaygroundButton code={"import { FaosClient } from '@faos/sdk';\n\ninterface CreditAnalysis {\n insights: string[];\n riskScore: number;\n recommendation: string;\n}\n\nconst client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });\nconst res = await client.agents.invoke<CreditAnalysis>(\n 'credit-risk-analyst',\n { query: 'Analyze Q1 2024' },\n);\n\nres.result.insights; // string[] — fully typed!\nres.result.riskScore; // number\nres.result.recommendation; // string"} lang="typescript" />

Streaming

import { FaosClient } from '@faos/sdk';

const client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });

for await (const chunk of client.agents.stream(
'credit-risk-analyst',
{ query: 'Analyze Q1 2024' },
)) {
if (chunk.type === 'text') {
process.stdout.write(chunk.data);
}
}
<PlaygroundButton code={"import { FaosClient } from '@faos/sdk';\n\nconst client = new FaosClient({ apiKey: 'faos_sk_your_key_here' });\n\nfor await (const chunk of client.agents.stream(\n 'credit-risk-analyst',\n { query: 'Analyze Q1 2024' },\n)) {\n if (chunk.type === 'text') {\n process.stdout.write(chunk.data);\n }\n}"} lang="typescript" />

Next Steps