API Import Doc Writer
Overview
Create API reference documents that are accurate enough for humans and structured enough for API import tools. Prefer source handlers, route registration, schema definitions, request builders, and tests over memory or inferred conventions.
Use references/example_byterec_api_reference.md as a complete example of the expected output shape. Use scripts/validate_api_doc.py before delivering a generated document.
Workflow
Locate source-of-truth code.
- Find route registration, handler classes/functions, request parsing, schema/model/protobuf/dataclass definitions, response wrappers, and representative callers.
- Record method and path from route registration, not just from client code.
- For internal wrappers, distinguish public API path from helper/client function names.
Build one section per endpoint.
- Title format:
## `METHOD /path`. - Include: Purpose, Query Params, Path Params, Headers, Body Example or Body, Body Fields, Response Example, Response Fields.
- Under every param/field group, include an
Import JSONblock even when empty.
- Title format:
Document common parameters under each API.
- Repeat shared query params and headers inside each endpoint section, because import tools often work per endpoint.
- Keep
fieldNameas the real parameter name, for examplecaller_name. - If the import tool needs a transport hint, put the compliance description first when applicable, then the transport hint, for example
Corporation data. http_query_params: caller_name, then add enough meaning for data tagging review.
Model request bodies for import, not only for one example.
- Include all supported body variants discovered from code, such as datasource types, schema modes, and region-specific dynamic keys.
- If the backend expects dynamic top-level keys such as
euandttp, document them as top-level fields, not under a scalarregionfield. - If the user limits allowed variants, remove the extra variants from both the field table and Import JSON.
Use only the target import tool's field types.
- Allowed
typevalues:string,boolean,integer,object,map,array. - Convert specific array spellings such as
array<object>andarray<string>toarray. - Convert typed maps such as
object<string, object>tomap. - Model every array as exactly one child named
items. If the array item is an object, put the item's fields underitems.children.
- Allowed
Structure Import JSON for importer compatibility.
- Each field item should use:
{ "fieldName": "field_name", "type": "string", "description": "Field meaning.", "compliance_tag": {} } - For
object, include non-emptychildren; do not emit an empty object field if the importer rejects empty objects. - For
array, include exactly one child:
Replace{ "fieldName": "items", "type": "object", "description": "Array item.", "compliance_tag": null, "children": [ { "fieldName": "name", "type": "string", "description": "Item field.", "compliance_tag": {} } ] }typewithstring,integer, or another scalar type for scalar arrays, and omitchildrenfor scalar items. - For nested fields, prefer nested
childrenover flat names likedata.uriwhen importing body or response schemas. - Use
[]for empty query/path/header/body groups.
- Each field item should use:
Validate before final delivery.
- Run:
python3 scripts/validate_api_doc.py <generated-doc.md> - Fix invalid JSON, unsupported field types, and any
objectfield without non-emptychildren.
- Run:
Output Contract
For each endpoint, produce this order:
## `POST /example/path`
Purpose: One concise sentence.
### Query Params
| Field | Type | Required | Description |
|---|---|---|---|
| `caller_name` | `string` | Yes | http_query_params: caller_name |
Import JSON:
```json
[
{
"fieldName": "caller_name",
"type": "string",
"description": "http_query_params: caller_name",
"compliance_tag": {}
}
]
```
Repeat the same pattern for Path Params, Headers, Body Fields, and Response Fields.
Field Description Rules
- Describe what the backend uses the field for, not just its English name.
- For query params that identify callers, credentials, systems, services, employees, or corporate accounts, start with
Corporation data., then include the transport hint and usage, for exampleCorporation data. http_query_params: caller_name. OpenAPI caller identity used by the gateway to identify the calling system and validate access. - For service identifiers such as
psm, state that it is a backend service PSM/service metadata used to locate a service. - For employee/corporate identity fields such as
staff_name, state that it is an employee or corporate account name and should be treated as Corporation Data when relevant. - Mark requirement with practical values such as
Yes,No,For kafka,Sequence only, orConditional. - For enum-like fields, list known values in the description.
- For response wrappers, document both wrapper fields such as
codeand nested payload fields such asdata.uri. - If source code returns generic errors through a common wrapper, document the success response and mention error behavior in the description.
Common Pitfalls
- Do not invent routes from client wrapper names.
- Do not leave body variants out just because the sample request uses one region or type.
- Do not use unsupported import types like
array<object>orarray<string>. - Do not place multiple children directly under an
array; wrap them under oneitemschild. - Do not emit
objectfields withoutchildrenin Import JSON. - Do not flatten nested Import JSON fields if the importer expects
children.