For the complete documentation index, see llms.txt. This page is also available as Markdown.

Authentication

Use the Docs Embed with sites that require authentication by passing visitor tokens or using authenticated access

If your GitBook documentation requires authentication, Docs Embed needs a GitBook visitor token to access content.

There are two approaches:

  1. Pass the token directly (recommended) - Initialize the embed with a GitBook visitor token.

  2. Use cookie-based detection - Check for the GitBook visitor token before loading.

When initializing the embed, pass the GitBook visitor token as visitor.token.

visitor.token isn't a raw access token or ID token from your identity provider. It is the GitBook visitor token that GitBook issues for authenticated docs access. The your-jwt-token values in these examples represent that GitBook-issued token.

<script src="https://docs.company.com/~gitbook/embed/script.js?jwt_token=your-jwt-token"></script>
<script>
  window.GitBook(
    "init",
    { siteURL: "https://docs.company.com" },
    { visitor: { token: "your-jwt-token" } }
  );
  window.GitBook("show");
</script>

The Embed config API has not changed. Pass GitBook-issued visitor tokens as visitor.token.

For authenticated sites, GitBook forwards this token to the site as jwt_token in the iframe/script URL. If you load the standalone script from an authenticated site, you must include jwt_token in the <script src> URL.

OIDC-backed sites

Docs Embed accepts the GitBook visitor-token flow using HS256. You can't change or configure this algorithm to accept RS256 identity-provider tokens.

Don't pass an OIDC provider's access token or ID token as visitor.token. Auth0 and Descope tokens that use RS256 are incompatible and Docs Embed rejects them because of their signing algorithm. Don't pass through, re-sign, or mint a GitBook visitor token from an identity-provider token.

For an OIDC-backed docs site, use this flow:

  1. Send the user to your protected docs-site URL, such as https://docs.example.com.

  2. Complete the normal interactive OIDC sign-in on the docs site.

  3. After GitBook establishes the docs-site session and stores gitbook-visitor-token, retrieve that token.

  4. Pass the token with the cookie-based or direct-token approach on this page.

This flow needs a prior successful docs-site sign-in. It also needs the required cookie and domain availability.

If your docs site stores the visitor token in cookies (as gitbook-visitor-token), you can check for it before loading the embed.

After a user signs in to your authenticated docs site, GitBook stores a visitor token in their browser cookies under the key gitbook-visitor-token. The embed needs this token to fetch content from your docs.

The flow:

  1. The user signs in to your docs site.

  2. GitBook stores the visitor token in browser cookies.

  3. Your app checks for the token.

  4. If the token exists, load the embed and pass the token.

  5. If the token doesn't exist, send the user to your docs site to sign in.

Copy-paste snippet

Use this snippet only after the user completes docs-site sign-in:

Alternative: Prompt users to sign in

If the token is missing, send the user to the protected docs-site URL. This starts the supported interactive OIDC sign-in flow:

Common pitfalls

  • Using an identity-provider token – Don't use an OIDC access token or ID token as visitor.token. Use the GitBook-issued HS256 visitor token after docs-site authentication.

  • Loading the embed before sign-in – Send first-time visitors to the docs site to complete sign-in before loading the script or components.

  • Token not persisting across domains – Cookies don't persist across different domains due to browser security policies. Your app and docs must be on the same domain or subdomain, or pass the token directly.

  • Token expired – Tokens can expire. If the embed returns authentication errors, prompt users to sign in again.

  • Using wrong cookie name – The token is stored as gitbook-visitor-token, not gitbook-token or other variations.

  • Not passing token to init/getFrameURL – When using the cookie-based approach, make sure to pass the token to GitBook('init', ..., { visitor: { token } }) or getFrameURL({ visitor: { token } }).

Debugging

To verify the token is present, open your browser console and run:

If this returns undefined, the user hasn't signed in to your docs yet.

Next steps

Last updated

Was this helpful?

Powered by GitBook