Integration runtime
GitBook Runtime is the platform that lets you build and run integrations within GitBook. Integrations can extend GitBook’s functionality by providing custom UI components, handling events, managing OAuth authentication, and communicating over HTTP. This guide explains how to build an integration, what each key element does, and how they interact.
The main building blocks are:
Integration Initialization – Register your integration.
UI Components – Create custom, interactive components.
OAuth Handling – Manage authentication flows.
Environment Context – Access runtime and installation details.
HTTP Communication – Fetch external data using HTTP.
Actions & Rendering – Define component behavior and display.
Event Handling – Listen and respond to GitBook events.
URL Utilities – Work with URLs and query strings.
createIntegration
createIntegration
Integrations are created using the createIntegration()
method. This method is the entrypoint for your integration and sets up its runtime context, including HTTP fetch methods, UI components, and event handlers.
createIntegration()
accepts the following object parameters:
fetch
Async Function
A function to handle incoming HTTP requests or action dispatches.
components
Array
A list of UI component definitions created using createComponent()
.
events
Object
An object mapping event names to their handler functions.
createComponent
createComponent
UI components let you build interactive elements that appear in GitBook’s quick insert menu (⌘ + /) or in the configuration screen of your integration. Use the createComponent()
method to define a component’s identifier, initial state, actions, and rendering logic.
createComponent()
accepts the following object parameters:
componentId
string
Unique identifier for the component.
initialState
Function (props) ⇒ object
Function that initializes the component state based on its props.
action
Async Function
Callback that handles user interactions; receives element
, action
, and context
.
render
Async Function
Function that returns the component’s UI as ContentKit markup.
createOAuthHandler
createOAuthHandler
When your integration requires user authentication via OAuth, you use createOAuthHandler()
to define the OAuth flow. This function sets up redirection, token exchange, and extraction of credentials.
createOAuthHandler()
accepts the following object parameters:
clientId*
string
Your client application ID from the OAuth provider.
"my-client-id"
clientSecret*
string
Your client secret from the OAuth provider.
"my-client-secret"
authorizeURL*
string
The URL where users are redirected for authorization.
"https://oauth.example.com/authorize"
accessTokenURL*
string
The URL used to exchange the authorization code for an access token.
"https://oauth.example.com/token"
redirectURL
string
(Optional) URL used for redirection if a static URL is needed.
"https://myapp.example.com/oauth"
scopes
string[]
An array of scopes to request during authentication.
["read", "write"]
prompt
string
(Optional) Configuration for prompting the user during authentication.
"consent"
extractCredentials
function
A function that processes the OAuth response and returns credentials in the expected format.
(response) => { … }
Rendering components
The render function returns the UI for the component using ContentKit markup.
Environment context
GitBook provides an environment object that gives your integration details about the runtime context, such as API endpoints, integration configuration, installation details, and secrets.
The context.environment
object can include the following:
API information
apiEndpoint
string
URL of the HTTP API.
apiTokens
object
Contains authentication tokens for the API.
Integration information
integration
object
Details about the integration itself.
Site installation (if applicable)
space
string
ID of the space where the integration is installed.
status
object
Installation status; e.g., Active, Pending, Paused.
configuration
object
Custom configuration variables for the space.
externalIds
any
External identifiers.
urls
object
Contains URLs associated with the installation (e.g., public endpoint).
Organization installation (if applicable)
id
string
Installation ID.
space_selection
object
Specifies whether all spaces or selected spaces are used.
configuration
object
Custom configuration for the organization.
urls
object
Contains organization-related URLs.
externalIds
string[]
Array of external IDs assigned by the integration.
target
string
Target of the integration installation.
Runtime secrets
secrets
object
Secrets stored on the integration for runtime use. Defined in the integration's configuration.
Events
GitBook fires events when specific actions occur (like viewing a page or updating content). Your integration can listen to these events to update state, log information, or trigger other actions.
Depending on the event, your integration may need the correct scope permissions.
Inside the createIntegration()
call, you define event handlers:
Here are the available events you can read:
installation_setup
Triggered when the integration is first installed globally.
space_installation_setup
Triggered when the integration is installed into a specific space.
space_view
Triggered when a user views a space.
ui_render
Triggered when the integration's UI component is rendered.
space_content_updated
Triggered when content in the space is changed or updated.
space_visibility_updated
Triggered when a space's visibility settings are changed.
space_gitsync_started
Triggered when a Git sync process begins for a space.
space_gitsync_completed
Triggered when a Git sync process completes for a space.
Actions
Interactive components can respond to user events via actions. They are defined in your component when using createComponent()
.
HTTP communication
Integrations communicate with external services via HTTP using the Fetch API.
method
string
HTTP method (GET, POST, etc.).
"POST"
headers
object
HTTP headers as key/value pairs.
{ "Content-Type": "application/json" }
body
string
Payload to send with the request.
'{"message": "Hello World"}'
Fetch Method Example
Request Example:
Response Example:
Learn more from the MDN Request docs and MDN Response docs.
Last updated
Was this helpful?