Quick start
Run an agent from Node.js with an AI Gateway key. For browser setup, see WebAssembly.
Install
Use Node.js 20 or later:
npm install libfx@0.0.12
Connect a model
libfx 0.0.12 sends model requests through AI Gateway. Open the API Keys page, click Create key, name the key, and copy its value.
In your terminal, replace your-api-key with the copied value:
export AI_GATEWAY_API_KEY="your-api-key"
The example below reads this variable and passes it as apiKey. CLI sign-ins, including Codex and Grok subscriptions, are not available to this API.
Run an agent
Save this as agent.mjs:
import { createFxAgent } from 'libfx'
const agent = await createFxAgent({
apiKey: process.env.AI_GATEWAY_API_KEY,
})
try {
const turn = agent.prompt('Explain closures in two sentences.')
for await (const event of turn) {
if (event.type === 'text_delta') process.stdout.write(event.delta)
}
await turn.result
process.stdout.write('\n')
} finally {
await agent.close()
}
Run it in the same terminal where you set the key:
node agent.mjs
The reply streams to your terminal.
Next steps
- Prompts and streaming: continue a conversation and handle events.
- Tools: give the agent JavaScript functions to call.
- Examples: run a complete project.