# TRUEREF-0010 — REST API (Search & Context Endpoints) **Priority:** P0 **Status:** Pending **Depends On:** TRUEREF-0006, TRUEREF-0009 **Blocks:** TRUEREF-0011 --- ## Overview Implement the public-facing REST API endpoints that replicate context7's `/api/v2/libs/search` and `/api/v2/context` interfaces. These are the endpoints that the MCP server, CLI tools, and external integrations call. The response format is designed to be compatible with context7's API surface so that tools built for context7 can be pointed at TrueRef with minimal changes. --- ## Acceptance Criteria - [ ] `GET /api/v1/libs/search` — search libraries by name (equivalent to context7's `/api/v2/libs/search`) - [ ] `GET /api/v1/context` — fetch documentation for a library (equivalent to context7's `/api/v2/context`) - [ ] Both endpoints support `type=json` (structured) and `type=txt` (plain text for LLMs) - [ ] `libraryId` supports `/owner/repo` (default branch) and `/owner/repo/version` (specific version) - [ ] Rules from `trueref.json` are prepended to context responses - [ ] CORS headers set (allow all origins) for browser-accessible usage - [ ] Response times < 500ms p99 for typical queries - [ ] Integration tests covering both endpoints --- ## Endpoint: `GET /api/v1/libs/search` **Equivalent to:** `GET https://context7.com/api/v2/libs/search` ### Query Parameters | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | `libraryName` | string | Yes | Library name to search for | | `query` | string | No | User's question for relevance ranking | | `limit` | integer | No | Max results (default: 10, max: 50) | ### Response `200` (`type=json`, default): ```json { "results": [ { "id": "/facebook/react", "title": "React", "description": "A JavaScript library for building user interfaces", "branch": "main", "lastUpdateDate": "2026-03-22T10:00:00Z", "state": "finalized", "totalTokens": 142000, "totalSnippets": 1247, "stars": 228000, "trustScore": 9.2, "benchmarkScore": 87, "versions": ["v18.3.0", "v17.0.2"], "source": "https://github.com/facebook/react" } ] } ``` Note: `state: "finalized"` maps from TrueRef's `state: "indexed"` for compatibility. ### State Mapping | TrueRef state | context7 state | |---------------|---------------| | `pending` | `initial` | | `indexing` | `initial` | | `indexed` | `finalized` | | `error` | `error` | --- ## Endpoint: `GET /api/v1/context` **Equivalent to:** `GET https://context7.com/api/v2/context` ### Query Parameters | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | `libraryId` | string | Yes | Library ID, e.g. `/facebook/react` or `/facebook/react/v18.3.0` | | `query` | string | Yes | Specific question about the library | | `type` | string | No | `json` (default) or `txt` (plain text for LLM injection) | | `tokens` | integer | No | Approximate max token count for response (default: 10000) | ### Response `200` (`type=json`): ```json { "snippets": [ { "type": "code", "title": "Basic Component", "description": "Getting Started > Components", "language": "tsx", "codeList": [ { "language": "tsx", "code": "function MyComponent() {\n return