Node.js/NPM
Integrate GitBook Assistant using the NPM package for full application-level control
Last updated
Was this helpful?
Integrate GitBook Assistant using the NPM package for full application-level control
Last updated
Was this helpful?
Was this helpful?
In your application code, import the createGitBook function:
Or using CommonJS:
Initialize GitBook
Create a GitBook instance with your docs site URL:
const gitbook = createGitBook({
siteURL: "https://docs.company.com",
});Create an iframe
Generate an iframe element and set its source to the Assistant URL:
const iframe = document.createElement("iframe");
iframe.src = gitbook.getFrameURL();
iframe.id = "assistant-container";
iframe.style.border = "none";
Attach the frame
Create a GitBook frame instance and mount it to your page:
const frame = gitbook.createFrame(iframe);
document.getElementById("assistant-container").appendChild(iframe);Control the Assistant programmatically
Use the frame instance to interact with the Assistant:
// Open the Assistant
frame.open();
// Close the Assistant
frame.close();
// Toggle the Assistant
frame.toggle();
// Navigate to a specific page
frame.navigateToPage("/getting-started");
Add configuration options
Pass customization options when initializing:
const gitbook = createGitBook({
siteURL: "https://docs.company.com",
welcomeMessage: "Welcome to our help center!",
suggestions: ["How do I get started?", "What are the pricing plans?"],
buttons: [
siteURL
string
Yes
N/A
Your GitBook docs site URL (e.g., https://docs.company.com).
welcomeMessage
string
No
null
Custom welcome message shown when the Assistant opens.
Frame Methods:
open()
None
Open the Assistant panel.
close()
None
Close the Assistant panel.
toggle()
None
Toggle the panel open/closed.
navigateToPage(path)
path: string
Forgetting to install the package β Run npm install @gitbook/embed before importing.
Missing siteURL β The siteURL option is required and must match your published docs site.
iFrame not rendering β Ensure the parent container has sufficient width/height for the iframe to display.
Frame methods called before initialization β Wait until createFrame() completes before calling frame methods.
Not cleaning up on unmount β Always call frame.destroy() to prevent memory leaks in single-page apps.
npm install @gitbook/embedimport { createGitBook } from "@gitbook/embed";const { createGitBook } = require("@gitbook/embed");suggestions
string[]
No
[]
Array of suggested questions displayed to users.
buttons
object[]
No
[]
Custom buttons in the Assistant header. Each button has label, icon, and onClick properties.
tools
object[]
No
[]
Custom tools for the Assistant. See Creating custom tools.
Navigate to a specific docs page.
navigateToAssistant()
None
Navigate directly to the Assistant interface.
postUserMessage(message)
message: string
Send a message as the user.
clearChat()
None
Clear all chat messages.
destroy()
None
Remove the Assistant and clean up resources.