ChatBotKit Node SDK
    Preparing search index...

    Module @chatbotkit/next

    Follow on Twitter ChatBotKit CBK.AI NPM Email Discord

    ChatBotKit Next.js SDK

    The ChatBotKit SDK for Next.js is crafted to integrate chatbot functionalities seamlessly into Next.js applications, specifically optimized for Next.js Edge runtime environments.

    Build lighter, future-proof AI agents. When you build with ChatBotKit, the heavy lifting happens on our servers—not in your application. This architectural advantage delivers:

    • 🪶 Lightweight Agents: Your agents stay lean because complex AI processing, model orchestration, and tool execution happen server-side. Less code in your app means faster load times and simpler maintenance.

    • 🛡️ Robust & Streamlined: Server-side processing provides a more reliable experience with built-in error handling, automatic retries, and consistent behavior across all platforms.

    • 🔄 Backward & Forward Compatible: As AI technology evolves—new models, new capabilities, new paradigms—your agents automatically benefit. No code changes required on your end.

    • 🔮 Future-Proof: Agents you build today will remain capable tomorrow. When we add support for new AI models or capabilities, your existing agents gain those powers without any updates to your codebase.

    This means you can focus on building great user experiences while ChatBotKit handles the complexity of the ever-changing AI landscape.

    Begin your journey with ChatBotKit using these steps:

    1. Installation: Add the SDK to your project by running the npm command:
      npm install @chatbotkit/next @chatbotkit/react @chatbotkit/sdk
      
    2. Integration: Utilize the SDK to create and manage chatbot interactions in your Next.js application.

    The following example showcases how to implement a chatbot in a Next.js application using the Edge runtime environment.

    // file: ./pages/api/conversation/complete.js
    import { stream } from '@chatbotkit/next/edge'
    import { ChatBotKit } from '@chatbotkit/sdk'

    const cbk = new ChatBotKit({
    secret: process.env.CHATBOTKIT_API_SECRET,
    })

    export default async function handler(req) {
    const { messages } = await req.json()

    return stream(cbk.conversation.complete(null, { messages }))
    }

    export const config = {
    runtime: 'edge',
    }
    // file: ./pages/index.js
    import { AutoTextarea, useConversationManager } from '@chatbotkit/react'

    export default function Index() {
    const {
    thinking,

    text,
    setText,

    messages,

    submit,
    } = useConversationManager({
    endpoint: '/api/conversation/complete',
    })

    function handleOnKeyDown(event) {
    if (event.keyCode === 13) {
    event.preventDefault()

    submit()
    }
    }

    return (
    <div style={{ fontFamily: 'monospace', padding: '10px' }}>
    {messages.map(({ id, type, text }) => (
    <div key={id}>
    <strong>{type}:</strong> {text}
    </div>
    ))}
    {thinking && (
    <div key="thinking">
    <strong>bot:</strong> thinking...
    </div>
    )}
    <AutoTextarea
    value={text}
    onChange={(e) => setText(e.target.value)}
    onKeyDown={handleOnKeyDown}
    placeholder="Type something..."
    style={{
    border: 0,
    outline: 'none',
    resize: 'none',
    width: '100%',
    marginTop: '10px',
    }}
    />
    </div>
    )
    }

    Explore a full example with additional features here.

    For a detailed exploration of the ChatBotKit Next SDK, including its capabilities and configurations tailored for Next.js Edge runtime, visit our type documentation page.

    If you find a bug or would like to contribute to the ChatBotKit SDK, please open an issue or submit a pull request on the official GitHub repository.

    Modules

    edge