| description | Integrate Docs Embed using the NPM package for full application-level control |
|---|
If you need more control and want to work at the application level, you can install the GitBook embed package from npm. This approach is ideal for server-side rendering, build-time integration, or custom iframe management.
{% stepper %} {% step %}
Add @gitbook/embed to your project:
npm install @gitbook/embedFor the complete API reference and source code, see the @gitbook/embed package on GitHub.
{% endstep %}
{% step %}
In your application code, import the createGitBook function:
import { createGitBook } from "@gitbook/embed";Or using CommonJS:
const { createGitBook } = require("@gitbook/embed");{% endstep %}
{% step %}
Create a GitBook instance with your docs site URL:
const gitbook = createGitBook({
siteURL: "https://docs.company.com",
});{% endstep %}
{% step %}
Generate an iframe element and set its source to the embed URL:
const iframe = document.createElement("iframe");
iframe.src = gitbook.getFrameURL({
visitor: {
token: 'your-jwt-token', // Optional: for Adaptive Content or Authenticated Access
unsignedClaims: { // Optional: custom claims for dynamic expressions
userId: '123',
plan: 'premium'
}
}
});
iframe.id = "gitbook-embed-container";
iframe.style.border = "none";
iframe.style.width = "100%";
iframe.style.height = "600px";{% endstep %}
{% step %}
Create a GitBook frame instance and mount it to your page:
const frame = gitbook.createFrame(iframe);
document.getElementById("gitbook-embed-container").appendChild(iframe);{% endstep %}
{% step %}
Use the frame instance to interact with the embed:
// Navigate to a specific page in the docs tab
frame.navigateToPage("/getting-started");
// Switch to the assistant tab
frame.navigateToAssistant();
// Post a message to the chat
frame.postUserMessage("How do I get started?");
// Clear chat history
frame.clearChat();{% endstep %}
{% step %}
Configure the embed with customization options:
frame.configure({
tabs: ['assistant', 'docs'],
actions: [
{
icon: 'circle-question',
label: 'Contact Support',
onClick: () => window.open('https://support.example.com', '_blank')
}
],
greeting: { title: 'Welcome!', subtitle: 'How can I help?' },
suggestions: ['What is GitBook?', 'How do I get started?'],
tools: [/* ... */]
});{% endstep %}
{% step %}
Register event listeners to respond to embed events:
frame.on('close', () => {
console.log('Frame closed');
});
// Unsubscribe when done
const unsubscribe = frame.on('navigate', (data) => {
console.log('Navigated to:', data.path);
});{% endstep %} {% endstepper %}
createGitBook(options: { siteURL: string })→GitBookClientclient.getFrameURL(options?: { visitor?: {...} })→string- Get the iframe URL with optional authenticated accessclient.createFrame(iframe: HTMLIFrameElement)→GitBookFrameClient- Create a frame client to communicate with the iframe
frame.navigateToPage(path: string)→void- Navigate to a specific page in the docs tabframe.navigateToAssistant()→void- Switch to the assistant tabframe.postUserMessage(message: string)→void- Post a message to the chatframe.clearChat()→void- Clear chat historyframe.configure(settings: Partial<GitBookEmbeddableConfiguration>)→void- Configure the embedframe.on(event: string, listener: Function)→() => void- Register event listener (returns unsubscribe function)
Configuration options are available via frame.configure({...}):
Override which tabs are displayed. Defaults to your site's configuration.
- Type:
('assistant' | 'docs')[]
Custom action buttons rendered in the sidebar alongside tabs. Each action button triggers a callback when clicked.
Note: This was previously named buttons. Use actions instead.
- Type:
Array<{ icon: string, label: string, onClick: () => void }>
Welcome message displayed in the Assistant tab.
- Type:
{ title: string, subtitle: string }
Suggested questions displayed in the Assistant welcome screen.
- Type:
string[]
Custom AI tools to extend the Assistant. See Creating custom tools for details.
- Type:
Array<{ name: string, description: string, inputSchema: object, execute: Function, confirmation?: {...} }>
Pass to getFrameURL({ visitor: {...} }). Used for Adaptive Content and Authenticated Access.
- Type:
{ token?: string, unsignedClaims?: Record<string, unknown> }
- Forgetting to install the package – Run
npm install @gitbook/embedbefore importing. - Missing siteURL – The
siteURLoption 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 unsubscribing from events – Remember to call the unsubscribe function returned by
frame.on()to prevent memory leaks. - Using old API methods – Methods like
open(),close(),toggle(), anddestroy()are not available in the NPM package. Use the frame client methods instead.