Getting Started with BlockNote AI
This guide walks you through the steps to add AI functionality to your BlockNote rich text editor.
First, install the @blocknote/xl-ai
package:
npm install @blocknote/xl-ai
BlockNote AI uses the the AI SDK to standardize integrating artificial intelligence (AI) models across supported providers.
Setting up the editor
Let's create an editor with the AI Extension enabled:
import { createBlockNoteEditor } from "@blocknote/core";
import { BlockNoteAIExtension } from "@blocknote/xl-ai";
import { en } from "@blocknote/core/locales";
import { en as aiEn } from "@blocknote/xl-ai/locales";
import { createAIExtension } from "@blocknote/xl-ai";
import "@blocknote/xl-ai/style.css"; // add the AI stylesheet
const editor = createBlockNoteEditor({
dictionary: {
...en,
ai: aiEn, // add default translations for the AI extension
},
extensions: [
createAIExtension({
transport: new DefaultChatTransport({
api: `/api/chat`,
}),
}),
],
// ... other editor options
});
See the API Reference for more information on the createAIExtension
method.
Adding AI UI elements
The next step is to customize the UI elements of your editor. We want to:
- add an AI button to the formatting toolbar (shown when selecting text)
- add an AI option to the slash menu (shown when typing a
/
)
We do this by disabling the default FormattingToolbar and SuggestionMenu and adding our own. See Changing UI Elements for more information.
<BlockNoteView
editor={editor}
// We're disabling some default UI elements
formattingToolbar={false}
slashMenu={false}
>
{/* Add the AI Command menu to the editor */}
<AIMenuController />
{/* Create you own Formatting Toolbar with an AI button,
(see the full example code below) */}
<FormattingToolbarWithAI />
{/* Create you own SlashMenu with an AI option,
(see the full example code below) */}
<SuggestionMenuWithAI editor={editor} />
</BlockNoteView>
Backend setup
Now, we'll set up a backend route to handle AI requests that will be forwarded to your LLM provider.
This example follows the basic example from the AI SDK for Next.js:
import { openai } from "@ai-sdk/openai";
import { convertToModelMessages, streamText } from "ai";
import { toolDefinitionsToToolSet } from "@blocknote/xl-ai";
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages, toolDefinitions } = await req.json();
const result = streamText({
model: openai("gpt-4.1"), // see https://ai-sdk.dev/docs/foundations/providers-and-models
messages: convertToModelMessages(messages),
tools: toolDefinitionsToToolSet(toolDefinitions),
toolChoice: "required",
});
return result.toUIMessageStreamResponse();
}
See Backend integrations for more information on how to integrate BlockNote AI with your backend.
Full Example
See the full example code and live demo. Select some text and click the AI (stars) button, or type /ai
anywhere in the editor to access AI functionality.