English | ä¸ć–‡
Bella API Doc Gen is an API service designed to automatically generate and update OpenAPI 3.0 documentation. It works by fetching a source OpenAPI specification, processing it, and storing the result in a database. Users can track the generation process via a task ID and retrieve the generated OpenAPI JSON through a dedicated API endpoint.
- Project Registration: Configure projects with specific details including source OpenAPI URL and optional Git credentials for private source repositories.
- API-Triggered Generation: Documentation generation is initiated via a secure API call.
- Authentication: Project-specific operations are protected by Bearer token authentication.
- Task-Based Generation: All document generation processes are handled as asynchronous tasks. A
task_idis returned for tracking. - Task Status Tracking: API endpoint to check the status of generation tasks (pending, processing, success, failed) and view results or error messages.
- Database Storage: Generated OpenAPI specifications are stored in the database.
- Direct OpenAPI Access: API endpoint to retrieve the latest successfully generated OpenAPI JSON for a project.
- Project Status Tracking: Monitor the overall state of projects (
init,pending,active,failed). Generation progress is tracked via tasks. - OpenAPI Spec Diffing: Identifies changes between the current source spec and the previously generated one to enable targeted processing.
- Targeted Description Completion: Supported by Code-Aware-RAG
Project Link: Code-Aware-RAG Description: If you plan to use the description completion feature, please ensure that the Code-Aware-RAG service is running before starting the Bella API documentation generation service. Important Note: About vector database updates:
- If the vector database for a project in the Code-Aware-RAG service does not exist, the system will automatically create it.
- If the vector database for a project in the Code-Aware-RAG service already exists, the system will not force update it.
- If you need to update the vector database (e.g., after major code updates), you need to manually call the
/v1/code-rag/repository/setupendpoint of the Code-Aware-RAG service and set bothforce_reindexandforce_recloneparameters totrue. - For detailed operation instructions, please refer to the Code-Aware-RAG project documentation.
-
Re-index repository for new projects or after major updates:
- Send a request to POST /v1/code-rag/repository/setup endpoint.
- Request Header:
Authorization: Bearer {apikey}(only required innon-configured apikey mode) Request body example:{ "repo_id": "bella-issues-bot", "repo_url_or_path": "https://github.com/szl97/bella-issues-bot.git", "force_reclone": true, "force_reindex": true }- This operation runs in the background and immediately returns a task ID.
repo_idis the unique identifier you specify for this repository.force_recloneandforce_reindexwill trigger re-indexing.
-
Query repository setup status:
- Send a request to GET /v1/code-rag/repository/status/{repo_id} endpoint.
- Request Header:
Authorization: Bearer {apikey}(only required innon-configured apikey mode)
- Response example:
{ "repo_id": "bella-issues-bot", "status": "completed", // "pending"(in progress), "completed"(finished), or "failed"(failed) "message": "Repository setup process completed", "index_status": "Indexed Successfully", "repository_path": "/path/to/repository" }
The base URL for all API endpoints is /v1/api-doc .
Most project-specific endpoints (PUT, DELETE for projects, and triggering generation) require a Bearer Token to be included in the Authorization header:
Authorization: Bearer <your_project_token>, expect for GET openapi-docs
This token is defined by you when creating the project and is unique to that project.
-
POST /v1/api-doc/projects- Description: Registers a new project for documentation generation. An initial documentation generation task is automatically started.
- Request Header:
Authorization: Bearer {apikey}(where{apikey}is your chosen API key for creating projects, not to be confused with the project-specific bearer token generated upon creation). - Request Body:
name(string, required): Unique name for the project.source_openapi_url(string, required): URL to fetch the source OpenAPI 3.0 JSON spec. This can be a public URL or a URL to a file in a Git repository.git_repo_url(string, required): This is the URL of that Git repository which is used for pull code.git_auth_token(string, optional): Authentication token (e.g., GitHub PAT) if thegit_repo_urlis private.
- Response (Success: 201 Created): Details of the created project and a
task_idfor the initial documentation generation.{ "project": { "id": 1, "name": "My Awesome API Project", "source_openapi_url": "https://api.example.com/v1/openapi.json", "git_repo_url": "https://git.example.com/my/repo.git", "created_at": "2025-05-28T12:00:00Z", "updated_at": "2025-05-28T12:00:00Z" }, "task_id": "xyz123abc" } - Request Body Example:
{ "name": "My Awesome API Project", "source_openapi_url": "https://api.example.com/v1/openapi.json" } - Request Body Example (Source from Private Git):
{ "name": "Project From Private Git", "source_openapi_url": "path/to/openapi.json", // Relative path within the cloned repo "git_repo_url": "https://github.com/your_username/my-private-spec-repo.git", "git_auth_token": "your_github_pat" }
-
GET /v1/api-doc/projects- Description: Lists all registered projects. (Note: This endpoint might be restricted or require admin privileges in a production environment).
- Response: A list of project details. Sensitive tokens are excluded.
-
GET /v1/api-doc/projects/{project_id}- Description: Get details of a specific project by its ID.
- Response: Project details. Sensitive tokens are excluded.
-
PUT /v1/api-doc/projects/{project_id}- Description: Updates an existing project's configuration. Requires Bearer token authentication for this project.
- Request Body: Similar to project creation (name, source_openapi_url, git_repo_url, git_auth_token), all fields optional. Can also include
bearer_tokento update the project's API token. - Response: The updated project details.
-
DELETE /v1/api-doc/projects/{project_id}- Description: Deletes a registered project. Requires Bearer token authentication for this project.
- Response: The details of the deleted project.
POST /v1/api-doc/gen/{project_id}- Description: Triggers the documentation generation and processing workflow for a specific project. Requires Bearer token authentication for this project.
- Response (Success: 202 Accepted): A confirmation message and a
task_idfor tracking the generation process.{ "message": "Documentation generation process initiated for project: ExampleProjectName", "task_id": "abc789def" }
GET /v1/api-doc/tasks/{task_id}- Description: Retrieves the status and result of a specific documentation generation task.
- Response:
id(string): The Task ID.project_id(integer): ID of the project this task belongs to.status(string): Current status of the task (e.g.,pending,processing,success,failed).created_at(datetime): Task creation timestamp.updated_at(datetime): Task last update timestamp.result(string/json, optional): Contains a success message or structured error details upon task completion.error_message(string, optional): Detailed error message if the task failed.
- Example Response (Success):
{ "id": "xyz123abc", "project_id": 1, "status": "success", "created_at": "2023-10-28T12:00:00Z", "updated_at": "2023-10-28T12:05:00Z", "result": "{"message": "OpenAPI documentation generated successfully."}", "error_message": null } - Example Response (Failure):
{ "id": "abc789def", "project_id": 2, "status": "failed", "created_at": "2023-10-28T11:00:00Z", "updated_at": "2023-10-28T11:01:00Z", "result": "{"error": "Failed to fetch OpenAPI spec from source."}", "error_message": "HTTPError: 404 Client Error: Not Found for url: https://invalid.url/openapi.json" }
GET /v1/api-doc/{project_id}- Description: Retrieves the latest successfully generated OpenAPI JSON document for the specified project.
- Response: The OpenAPI JSON document.
- Example Response:
{ "openapi": "3.0.0", "info": { "title": "Sample API", "version": "1.0.0" }, "paths": { "/items": { "get": { "summary": "List all items" } } } }
name: (string, required) Unique name for the project.bearer_token: (string, required on create/update) The secret token you define for authenticating API requests related to this project. This token is hashed and stored by Bella.source_openapi_url: (string, required) URL from which Bella will fetch the source OpenAPI 3.0 JSON specification. Can be a public URL or a path within a Git repository ifgit_repo_urlis also provided.git_repo_url: (string, optional) URL of the Git repository. Required ifsource_openapi_urlpoints to a resource within a private Git repository that needs to be cloned.git_auth_token: (string, optional) Token for Bella to authenticate with the Git repository (e.g., a GitHub Personal Access Token) if it's private.
- Register Project: Use
POST /v1/api-doc/projectsto register your API project. Provide itsnameandsource_openapi_url. If the source spec is in a private Git repository, also providegit_repo_urlandgit_auth_token. Define abearer_tokenfor securing your project's API interactions with Bella. - Initial Generation Task: Upon successful registration, Bella automatically triggers an initial documentation generation task. A
task_idis returned in the response. - Track Task Status: Use
GET /v1/api-doc/tasks/{task_id}with the receivedtask_idto monitor the generation progress (states:pending->processing->successorfailed). Theresultanderror_messagefields provide details on completion. - Manual Trigger: To update the documentation later, make a
POSTrequest to/v1/api-doc/gen/{project_id}, authenticating with your project'sbearer_token. This also returns atask_id. - Fetch Source Spec: Bella fetches the latest OpenAPI spec from the
source_openapi_url(using Git credentials if provided for a private repo). - Fetch Previous Spec (for diffing): Bella retrieves the last successfully generated OpenAPI specification for this project from its internal database.
- Process Spec:
- Bella Docs calculates the differences between the new source spec and the previous one.
- For new or modified parts, it can apply description completion using an LLM.
- The changes are then merged into a new version of the specification.
- Store Generated Spec: The newly processed OpenAPI specification is saved to Bella's database, associated with the project and the completed task.
- Retrieve Generated Spec: Once the task status is
success, you can access the latest generated OpenAPI document viaGET /v1/api-doc/openapi/projects/{project_id}/openapi-json. - Status Updates: The project's main status (
active,failed) reflects its overall health regarding documentation. Task statuses provide details on individual generation attempts.
This outlines a basic setup for development or testing. Production deployment would require further consideration for aspects like WSGI servers, containerization, and database management.
- Clone the Repository:
git clone <repository_url> cd bella-api-doc-gen # Or your project directory name
- Install Dependencies:
pip install -r requirements.txt
- Configure Environment:
- The application uses Pydantic's
BaseSettingsfor configuration, which can load from environment variables or a.envfile. - Key settings include
DATABASE_URL(e.g.,sqlite:///./test.dborpostgresql://user:pass@host:port/dbname). Refer toapp/core/config.py. - Create a
.envfile in the project root if you prefer, e.g.:DATABASE_URL="sqlite:///./bella_doc_gen.db"
- The application uses Pydantic's
- Initialize Database:
- The application is configured to initialize the database and create tables on startup (via
init_db(engine)inapp/main.py). Ensure your database server is running and accessible if using a non-SQLite database.
- The application is configured to initialize the database and create tables on startup (via
- Run the Application:
The API will typically be available at
uvicorn app.main:app --reload
http://127.0.0.1:8000. - Start with docker:
docker build -t bella-api-doc-gen . docker run -d -p 8000:8000 --name bella-api-doc-gen bella-api-doc-gen
The service uses SQLAlchemy and Pydantic settings, allowing database connection configuration through app/core/config.py or environment variables.
MySQL Configuration:
If you are using a MySQL database, the DATABASE_URL should be configured in the following format:
mysql+mysqlconnector://USER:PASSWORD@HOST:PORT/DATABASE_NAME
Where:
USER: Your MySQL username.PASSWORD: Your MySQL password.HOST: The hostname or IP address of your MySQL server (e.g.,localhost).PORT: The port number for MySQL (default is3306).DATABASE_NAME: The name of your database.
Example:
mysql+mysqlconnector://bella_user:[email protected]:3306/bella_db
You can set this DATABASE_URL in a couple of ways:
-
Via Environment Variable (Recommended): Set the
DATABASE_URLenvironment variable before running the application:export DATABASE_URL="mysql+mysqlconnector://USER:PASSWORD@HOST:PORT/DATABASE_NAME" uvicorn app.main:app --reload
This is the recommended approach for security and flexibility, especially in production.
-
Directly in
app/core/config.py: You can also hardcode it in theSettingsclass withinapp/core/config.py(though this is less flexible and not recommended for sensitive credentials in production):# In app/core/config.py class Settings(BaseSettings): # ... other settings ... DATABASE_URL: str = "mysql+mysqlconnector://USER:PASSWORD@HOST:PORT/DATABASE_NAME" # ...
The application is configured by default to use a SQLite database (sqlite:///./test.db) if DATABASE_URL is not otherwise specified, which is convenient for quick local testing but not suitable for production.