Quickstart: SDK
Install @maats/flapjack and stream your first AI agent response in 10 lines of TypeScript.
Install the TypeScript SDK and send your first streaming message to a Flapjack agent.
Time: 5 minutes Prerequisites: Node.js 18+, a Flapjack account with an API key and agent
Step 1: Install
npm install @maats/flapjack
π Copy as prompt
Install the Flapjack SDK:
npm install @maats/flapjack
Step 2: Set Environment Variables
# .env or .env.local
FLAPJACK_API_KEY=fj_live_... # from flapjack.chat β Keys
FLAPJACK_BASE_URL=https://api.flapjack.dev
Step 3: Stream Your First Message
import { FlapjackClient } from '@maats/flapjack';
const client = new FlapjackClient({
apiKey: process.env.FLAPJACK_API_KEY!,
baseUrl: process.env.FLAPJACK_BASE_URL,
});
// List your agents
const agents = await client.listAgents();
console.log('Agents:', agents.map(a => a.name));
if (agents.length === 0) {
console.error('No agents found. Create one at flapjack.chat');
process.exit(1);
}
// Create a conversation thread
const thread = await client.createThread(agents[0].id);
// Send a message and stream the response
for await (const event of client.sendMessage(thread.id, 'Hello!')) {
switch (event.type) {
case 'token':
process.stdout.write(event.delta);
break;
case 'tool_call':
console.log('\n[Tool]', event.tool.name);
break;
case 'done':
console.log('\n\nDone. Message ID:', event.messageId);
break;
case 'error':
console.error('Error:', event.code, event.detail);
break;
}
}
π Copy as prompt
Create a TypeScript script that uses
@maats/flapjackto list my agents, create a thread with the first agent, send "Hello!", and stream the response to stdout. UseFlapjackClientwith my API key from theFLAPJACK_API_KEYenvironment variable.
Step 4: Add React (Optional)
If you're building a React app, use the built-in hooks:
Security warning: The example below uses NEXT_PUBLIC_ environment variables for simplicity. This exposes your API key in the client bundle. For production, use the server proxy pattern instead.
import { FlapjackProvider, useChat } from '@maats/flapjack/react';
function App() {
return (
<FlapjackProvider config={{
apiKey: process.env.NEXT_PUBLIC_FLAPJACK_API_KEY!, // β οΈ Dev only β use server proxy in production
baseUrl: process.env.NEXT_PUBLIC_FLAPJACK_BASE_URL,
}}>
<Chat agentId="your-agent-id" />
</FlapjackProvider>
);
}
function Chat({ agentId }: { agentId: string }) {
const { messages, sendMessage, isStreaming } = useChat(agentId);
return (
<div>
{messages.map((msg, i) => (
<div key={i}>{msg.role}: {msg.content}</div>
))}
{isStreaming && <div>Thinking...</div>}
<input
onKeyDown={(e) => {
if (e.key === 'Enter') {
sendMessage(e.currentTarget.value);
e.currentTarget.value = '';
}
}}
disabled={isStreaming}
placeholder="Type a message..."
/>
</div>
);
}
π Copy as prompt
Set up Flapjack React hooks in my app. Wrap the app with
FlapjackProviderusing my API key from environment variables, then create aChatcomponent using theuseChathook that shows messages, a streaming indicator, and an input field.
What's Happening
FlapjackClientauthenticates with your API key and sends requests to the Flapjack APIcreateThreadstarts a new conversation session for the specified agentsendMessagesends a user message and returns an async generator of SSE events- The agent processes the message, optionally calls tools, and streams text tokens back
- The
doneevent contains the full final response and a persistedmessageId
Next Steps
- SDK: Client Reference β all FlapjackClient methods
- SDK: React Hooks β FlapjackProvider and useChat in depth
- SDK: Server Proxy β secure pattern for production React apps
- Concepts: Streaming β SSE event types explained