Implementation:0xSteph Pentest ai Api Server Create App
| Knowledge Sources | |
|---|---|
| Domains | API, REST, Engagement_Management |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Concrete tool for exposing pentest-ai engagement and findings data over a REST API, provided by the FastAPI library.
Description
The create_app function constructs and returns a FastAPI application that serves as the HTTP REST surface for pentest-ai. It is a read-mostly surface: list engagements, get findings, export SARIF, and stream live events via WebSocket. State-mutating endpoints (create engagement, abort engagement) are token-gated behind the PENTEST_AI_API_TOKEN environment variable. The server uses the same orchestrator path as the CLI, ensuring no second code path diverges.
Usage
Import this function when you need to serve pentest-ai's engagement and findings data over HTTP to a local dashboard, CI pipeline, or trusted internal service. Use pip install ptai[api] to install FastAPI as a dependency.
Code Reference
Source Location
- Repository: 0xSteph_Pentest_ai
- File: api/server.py
- Lines: 1-406
Signature
def create_app() -> Any:
"""Construct and return the FastAPI app.
Imports FastAPI lazily so that anyone importing `api.server` from a
minimal install (without the [api] extra) gets a clear error pointing
at the right install command.
"""
Import
from api.server import create_app, app
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| PENTEST_AI_API_TOKEN | str (env var) | No | Bearer token for state-mutating endpoints. If unset, write endpoints return 503. |
| PENTEST_AI_DB_PATH | str (env var) | No | SQLite path for findings DB. Defaults to pentest_findings.db.
|
Outputs
| Name | Type | Description |
|---|---|---|
| app | FastAPI | Configured FastAPI instance with all routes, middleware, and lifespan registered. |
Usage Examples
Basic Uvicorn Server
# Run with uvicorn from the command line:
# uvicorn api.server:app --host 0.0.0.0 --port 8000
# Or programmatically:
from api.server import create_app
app = create_app()
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Querying Findings via HTTP
import httpx
# List all engagements
resp = httpx.get("http://localhost:8000/engagements")
engagements = resp.json()
# Get findings for a specific engagement, filtered by severity
resp = httpx.get(
"http://localhost:8000/engagements/abc-123/findings",
params={"severity": "critical"},
)
findings = resp.json()
# Export SARIF for CI ingestion
sarif = httpx.get("http://localhost:8000/engagements/abc-123/sarif").json()
Launching an Engagement (Token Required)
import httpx
import os
token = os.environ["PENTEST_AI_API_TOKEN"]
resp = httpx.post(
"http://localhost:8000/engagements",
json={"target": "https://example.com", "scope": "example.com", "intensity": "balanced"},
headers={"Authorization": f"Bearer {token}"},
)
print(resp.json()) # {"status": "created", "target": "https://example.com", ...}