React SDK
@torquefi/react provides client hooks for Intelligence feeds and Assistant chat. Hooks call your BFF Route Handlers — never embed a business API key in the browser. Pair with torque-node on the server.
Installation
yarn add @torquefi/react @torquefi/types
# Server BFF also needs torque-node + TORQUE_API_KEYHooks
| Hook | Import | Default BFF route |
|---|---|---|
| useIntelligenceFeed() | @torquefi/react/intelligence | GET /api/torque/intelligence/feed |
| useAssistantChat() | @torquefi/react/assistant | POST /api/torque/assistant/chat |
Override BFF paths via hook options when your routes differ from the defaults.
Client usage
dashboard.tsx
'use client'
import { useIntelligenceFeed } from '@torquefi/react/intelligence'
import { useAssistantChat } from '@torquefi/react/assistant'
export function Dashboard({ walletAddress }: { walletAddress: string }) {
const { items, refresh, isLoading } = useIntelligenceFeed({
walletAddress,
chainId: 8453,
includeBrief: 1,
})
const { send, streamText, functionResults, isStreaming } = useAssistantChat()
return (
<div>
<button type="button" onClick={() => refresh()} disabled={isLoading}>
Refresh feed
</button>
<ul>{items?.map((item) => <li key={item.id}>{item.title}</li>)}</ul>
<button
type="button"
onClick={() =>
send({
content: 'What moved today?',
context: { walletAddress, chainId: 8453 },
stream: true,
})
}
disabled={isStreaming}
>
Ask assistant
</button>
{streamText && <p>{streamText}</p>}
{functionResults?.length ? <pre>{JSON.stringify(functionResults, null, 2)}</pre> : null}
</div>
)
}BFF routes (torque-node)
Implement the default routes on your server. The hooks proxy to these endpoints; your API key stays in env.
app/api/torque/intelligence/feed/route.ts
import { createTorqueFromEnv } from 'torque-node'
import { NextRequest, NextResponse } from 'next/server'
export async function GET(req: NextRequest) {
const { searchParams } = req.nextUrl
const torque = createTorqueFromEnv()
const feed = await torque.intelligence.feed({
walletAddress: searchParams.get('walletAddress') ?? undefined,
chainId: Number(searchParams.get('chainId') ?? 8453),
includeBrief: searchParams.get('includeBrief') === '1' ? 1 : undefined,
})
return NextResponse.json(feed)
}app/api/torque/assistant/chat/route.ts
import { createTorqueFromEnv } from 'torque-node'
import { NextRequest, NextResponse } from 'next/server'
export async function POST(req: NextRequest) {
const body = await req.json()
const torque = createTorqueFromEnv()
const stream = req.nextUrl.searchParams.get('stream') === 'true'
if (stream) {
// Return SSE from torque.assistant.chatStream — see Platform SDK streaming
return new NextResponse(/* SSE stream */)
}
const response = await torque.assistant.chat(body)
return NextResponse.json(response)
}Architecture
Browser (@torquefi/react hooks)
↓ same-origin fetch
Your BFF (/api/torque/**)
↓ TORQUE_API_KEY
app.torque.fi/api/v1
Related
Was this helpful?