Node.js/NPM
Integrate Docs Embed using the NPM package for full application-level control
Steps
1
npm install @gitbook/embed2
import { createGitBook } from "@gitbook/embed";const { createGitBook } = require("@gitbook/embed");3
const gitbook = createGitBook({
siteURL: "https://docs.company.com",
});4
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";
iframe.allow = "clipboard-write";5
const frame = gitbook.createFrame(iframe);
document.getElementById("gitbook-embed-container").appendChild(iframe);6
// 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();7
frame.configure({
trademark: false,
tabs: ['assistant', 'search', 'docs'],
actions: [
{
icon: 'circle-question',
label: 'Contact Support',
onClick: () => window.open('https://support.example.com', '_blank')
}
],
greeting: { title: 'Welcome!', subtitle: 'How can I help?' },
assistantName: 'Support Copilot',
closeButton: true,
suggestions: ['What is GitBook?', 'How do I get started?'],
tools: [/* ... */]
});8
frame.on('close', () => {
console.log('Frame closed');
});
// Unsubscribe when done
const unsubscribe = frame.on('navigate', (data) => {
console.log('Navigated to:', data.path);
});API Reference
Client Factory
Frame Client Methods
Configuration Options
tabs
actions
greeting
assistantName
closeButton
suggestions
trademark
tools
Frame URL options
colorScheme
visitor (Authenticated Access)
Common pitfalls
Last updated
Was this helpful?