# Gitbook -> raia Agent

<figure><img src="https://978389080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmUCQZRcY4U9lLNybdRmB%2Fuploads%2FzjRUX1IN49OTLwPRQ14q%2FTitle_%20Build%20n8n%20Workflow%20to%20Sync%20GitBook%20Content%20to%20Raia%20Agent%20-%20visual%20selection.png?alt=media&#x26;token=d7a727fd-a870-4c77-8dac-eee914330de9" alt=""><figcaption></figcaption></figure>

As a user of Raia Agent, I want to automatically sync content from a GitBook space into a Raia Agent as a Markdown file. This will ensure the agent has access to our most up-to-date documentation and knowledge base, allowing it to provide accurate and consistent information.

The n8n workflow should run on a nightly schedule and perform the following actions:

1. **Fetch GitBook Content:** Retrieve all pages from a specified GitBook space.
2. **Consolidate to Markdown:** Convert the fetched GitBook pages into a single, well-structured Markdown file. The Markdown file should include clear metadata, such as:
   * Space Name
   * Page Hierarchy (Table of Contents)
   * Sync Date
3. **Upload to Raia Agent:** Upload the generated Markdown file to the Raia Agent using the API.

**Acceptance Criteria:**

* An n8n workflow is created that successfully fetches all pages from a specified GitBook space.
* The workflow consolidates the content into a single Markdown file with the specified metadata.
* The workflow successfully uploads the Markdown file to the Raia Agent via the API.
* The workflow is scheduled to run every night.
* The workflow includes error handling and notifications for failed runs.

**Technical Details:**

* **GitBook API:**
  * **Authentication:** Bearer Token (`Authorization: Bearer YOUR_GITBOOK_TOKEN`)
  * **Key Endpoints:**
    * `GET /v1/orgs/{orgId}/spaces`: To list all spaces in an organization.
    * `GET /v1/spaces/{spaceId}/content`: To get the content structure of a space.
    * `GET /v1/spaces/{spaceId}/content/pages`: To list all pages in a space.
    * `GET /v1/spaces/{spaceId}/content/page/{pageId}`: To get the content of a specific page (use `format=markdown` if available).
* **Raia Agent API:**
  * **Endpoint:** `POST /external/agent-files/upload`
  * **Request Type:** `multipart/form-data`
  * **Authentication:** API Key in the header.
  * **Request Body:**
    * `file`: The Markdown file to upload.
    * `autoAddToVectorStore`: `true` (to make the content searchable by the agent).

**Implementation Notes:**

* The n8n workflow will require credentials for both the GitBook API and the Raia Agent API.
* The workflow should be designed to handle pagination when fetching the list of pages from GitBook.
* The Markdown file should be structured to maintain the hierarchy of the GitBook space for better readability.
* Consider using the GitBook API to get the space's table of contents to structure the final Markdown file.
* The workflow should be tested with a sample GitBook space before being deployed to production.
