API for creating snippets #489
Replies: 2 comments 1 reply
-
Hey @malessainray 👋 We have an API to create snippets, but it's not documented yet. You can find references to it in our OpenAPI spec (https://api.gitbook.com/openapi.yaml): You can also find APIs to capture snippets from events (terminal commands, messages, etc). |
Beta Was this translation helpful? Give feedback.
-
@SamyPesse I was able to generate some python code to generate a Snippet and set the title, however I have found it challenging to set the content of the snippet to fully integrate this into the workflow I am trying to create. For @malessainray I was able to get this code to somewhat work as it initiates a snippet generation, then updates the generated snippet. This works with updating the title but I haven't been able to figure out the endpoint to add content to the snippet body. If anyone @GitbookIO or @SamyPesse could assist with this to let me know how I might send some example content to be in the body of the snippet that would be greatly appreciated! import requests
# API credentials
API_KEY = "gb_api_yHzUWBb5xbwADch8TKUHgKabtBiAN89i085Rl4EO"
ORG_ID = "mtpAalXU2Ft9qGVpZhW1"
# Headers for both POST and PUT requests
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
def create_snippet():
"""Step 1: Create a new snippet (POST request)"""
create_url = f"https://api.gitbook.com/v1/orgs/{ORG_ID}/snippets"
# Initial payload for creating the snippet
create_payload = {
"title": "Initial Snippet Title",
"source": {
"sourceId": "source-12345",
"name": "Snippet Source Name"
}
}
print("Creating a new snippet...")
response = requests.post(create_url, json=create_payload, headers=headers)
if response.status_code == 201:
print(f"Snippet created successfully! Location: {response.headers['Location']}")
snippet_id = response.json().get('id') # Extract the ID of the created snippet
return snippet_id
else:
print(f"Failed to create snippet. Status Code: {response.status_code}")
print(f"Response: {response.json()}")
return None
def update_snippet(snippet_id):
"""Step 2: Attempt different methodologies to update the snippet with content (PUT request)"""
update_url = f"https://api.gitbook.com/v1/orgs/{ORG_ID}/snippets/{snippet_id}"
# Different payload methodologies to try
payload ={
# Method 3: Trying a combination of both content in source and at root level
"title": "Snippet with Content in Source and Root"
}
# Try each payload and output debugging information
print(f"Payload: {payload}")
response = requests.put(update_url, json=payload, headers=headers)
if response.status_code == 200:
print(f"Snippet updated successfully!")
print(f"Response: {response.json()}")
else:
print(f"Failed to update snippet with Method {i}. Status Code: {response.status_code}")
print(f"Response: {response.json()}")
# Main logic
snippet_id = create_snippet()
if snippet_id:
update_snippet(snippet_id)
else:
print("Snippet creation failed, skipping update attempts.") |
Beta Was this translation helpful? Give feedback.
-
I would like to create a small tool which collects data from local only on premise sources and after sanitizing I want to automatically create a snippet out of that. Sadly I was not able to find any documentation regarding how to create a snipped through the use of the api.
Beta Was this translation helpful? Give feedback.
All reactions