Quickstart
Learn how to use the GitBook API within minutes
Explore GitBook’s API
Last updated
Was this helpful?
Was this helpful?
npm install @gitbook/apiimport { GitBookAPI } from "@gitbook/api";
const ORGANIZATION_ID = "<your organization id>"
const SITE_ID = "<your site id>"
const API_TOKEN = "<your gitbook api token>"
const client = new GitBookAPI({
authToken: API_TOKEN
});
const stream = await client.orgs.streamAskInSite(
ORGANIZATION_ID,
SITE_ID,
{
question: "How do I get started?",
scope: {
mode: "default",
},
);
// Stream chunks as they arrive
for await (const chunk of stream) {
console.log(chunk);
}import json
import requests
ORGANIZATION_ID = "<your organization id>"
SITE_ID = "<your site id>"
API_TOKEN = "<your gitbook api token>"
response = requests.post(
f"https://api.gitbook.com/v1/orgs/{ORGANIZATION_ID}/sites/{SITE_ID}/ask",
headers={
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json"
},
json={
"question": "How do I get started?",
"scope": {"mode": "default"}
},
stream=True
)
# Get the last response before "done"
final = None
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: ') and line[6:] != 'done':
final = json.loads(line[6:])
print(final)