CODI API Guide

The FastAPI service mirrors all CLI functionality and exposes schema-validated endpoints for integrations. This guide describes available endpoints, request/response structures, and usage examples.

1. Getting Started

Run the server via CLI or container:

# Local CLI
codi serve --host 0.0.0.0 --port 8000

# Slim container
docker run --rm -it -v "$PWD:/work" -p 8000:8000 codi:slim

Interactive docs available at http://<host>:<port>/docs. Health check: GET /healthz.

2. Authentication

Current release exposes endpoints without authentication. Deployments behind ingress or API gateways should enforce authentication/authorization externally. Future versions may support tokens.

3. Environment Snapshot

Each response includes a environment block derived from core.config.CodiEnvironment, showing resolved toggles (LLM_ENABLED, AIRGAP, RULES_PATH, etc.).

4. Endpoints

4.1 POST /analyze

4.2 POST /rewrite

4.3 POST /run

4.4 POST /report

4.5 POST /llm/rank (Complete deployments)

4.6 POST /llm/explain

4.7 GET /healthz

5. Request Fields

Field Type Description
project_path string Absolute or relative path accessible to the server.
stack_hint string Optional manual stack override (node, python, java).
rules_path string Alternate rules file.
out_dir string Where to place run artefacts. Defaults to CODI_OUTPUT_ROOT.
candidate_limit int Number of templates to render (1-3).
skip_llm bool Skip LLM even if enabled globally.
format string md, html, or all for reports.

6. Response Structure (example excerpt)

{
  "run_path": "runs/20251126T174725Z-python-python",
  "candidates": [
    {
      "id": "candidate_1",
      "rule_id": "python_fastapi_wheels",
      "summary": "Switches to wheel-based install with multi-stage caching",
      "cmd_rewrite": {
        "converted_to_exec_form": true,
        "promoted_runtime_installs": ["pip install --user"]
      }
    }
  ],
  "metrics": {
    "estimated_size_reduction_pct": 34.8,
    "estimated_layer_reduction": 4,
    "analysis_time_ms": 215
  },
  "environment": {
    "rules_path": "patterns/rules.yml",
    "airgap": true,
    "llm_enabled": false
  }
}

7. Error Handling

Status Meaning
400 Missing or invalid parameters.
404 Provided path does not exist.
409 Policy violation (e.g., adapter missing, air-gap breach attempt).
422 Parsing or rendering errors, includes detailed message.
500 Unexpected server error (check logs).

Responses include detail field with actionable information. For parsing issues, detail.context lists offending lines.

8. cURL Examples

# Analyze project
curl -X POST http://localhost:8000/analyze \
  -H 'Content-Type: application/json' \
  -d '{"project_path": "demo/node"}' | jq

# Trigger full run
curl -X POST http://localhost:8000/run \
  -H 'Content-Type: application/json' \
  -d '{"project_path": "demo/python", "out_dir": "runs/api-python"}' | jq '.metrics'

# Generate report
curl -X POST http://localhost:8000/report \
  -H 'Content-Type: application/json' \
  -d '{"run_path": "runs/api-python"}'

9. Python Client Snippet

import requests

BASE_URL = "http://localhost:8000"
payload = {"project_path": "demo/java", "out_dir": "runs/api-java"}
resp = requests.post(f"{BASE_URL}/run", json=payload, timeout=600)
resp.raise_for_status()
print(resp.json()["metrics"])

10. JavaScript (fetch) Example

const payload = { project_path: "demo/node" };
fetch("http://localhost:8000/analyze", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify(payload),
})
  .then((res) => res.json())
  .then((data) => console.log(data.stack));

11. Deployment Considerations

12. Logging & Monitoring