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.
Generate an iframe element and set its source to the embed URL:
constiframe=document.createElement("iframe");iframe.src=gitbook.getFrameURL({visitor:{token:'your-jwt-token',// Optional: for Adaptive Content or Authenticated AccessunsignedClaims:{// Optional: custom claims for dynamic expressionsuserId:'123',plan:'premium'}}});iframe.id="gitbook-embed-container";iframe.style.border="none";iframe.style.width="100%";iframe.style.height="600px";
5
Attach the frame
Create a GitBook frame instance and mount it to your page:
Use the frame instance to interact with the embed:
// Navigate to a specific page in the docs tabframe.navigateToPage("/getting-started");// Switch to the assistant tabframe.navigateToAssistant();// Post a message to the chatframe.postUserMessage("How do I get started?");// Clear chat historyframe.clearChat();
7
Configure the embed
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: [/* ... */]});
8
Listen to events
Register event listeners to respond to embed events:
frame.on('close',()=>{console.log('Frame closed');});// Unsubscribe when doneconstunsubscribe=frame.on('navigate',(data)=>{console.log('Navigated to:',data.path);});
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 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(), and destroy() are not available in the NPM package. Use the frame client methods instead.