This tutorial produces a cited answer for a tender. It assumes the tender already has a processed document — either downloaded from the procurement source (see Download and read documents) or uploaded by your integration (see Upload a document). The snippets use the api helper from the quickstart.
1. Start the analysis
curl -s -X POST "$KOSMOBIDS_BASE_URL/tenders/$TENDER_ID/analyze" \
-H "x-api-key: $KOSMOBIDS_API_KEY" \
-H "content-type: application/json" \
-d '{
"questions": [
"Do we meet the mandatory requirements?",
"Which certifications are required?",
"What are the penalty clauses?"
]
}' | jq
{
"data": {
"workflowId": "workflow-id",
"status": "scheduled"
},
"error": null
}
Questions are assigned q1, q2, and so on in the order supplied. Keep the mapping on your side.
A 409 means no processed document exists yet for your company:
{
"data": {
"error": "Documents not ready",
"message": "No processed documents are available for this tender yet. Trigger the document download, or upload a document, then wait for processing."
},
"error": null
}
2. Poll until final
type Analysis = {
id: string;
status: 'pending' | 'running' | 'waiting' | 'retry' | 'completed' | 'failed' | 'aborted';
isFinalStatus: boolean;
isStatusSuccess?: boolean;
error?: string;
answers?: Array<{
questionId: string;
answer: string;
references: Array<{
idx: number;
fileId: string;
pageNumber: number;
regionIndex: number;
uploadedFileId?: string;
filename?: string;
}>;
}>;
usage?: { inputTokens: number; outputTokens: number; totalTokens: number };
};
async function waitForAnalysis(workflowId: string): Promise<Analysis> {
let delay = 2_000;
for (let attempt = 0; attempt < 90; attempt++) {
const analysis = await api<Analysis>(`/analyses/${workflowId}`);
if (analysis.isFinalStatus) {
if (!analysis.isStatusSuccess) throw new Error(analysis.error ?? 'Analysis failed');
return analysis;
}
await new Promise((resolve) => setTimeout(resolve, delay));
delay = Math.min(delay * 2, 30_000);
}
throw new Error('Timed out waiting for the analysis');
}
isFinalStatus is the portable signal. Do not hand-code the status list in your integration.
3. Resolve citations
Each reference identifies a page and a region:
{
"idx": 1,
"fileId": "processed-document-id",
"pageNumber": 12,
"regionIndex": 0,
"uploadedFileId": "page-image-id",
"filename": "specification.pdf"
}
To verify a statement:
- Read
GET /documents/:fileId/pages?startPage=12&endPage=12. - Find the page with
pageNumber === 12. - Use
regions[regionIndex]for the cited region, or usebbox_2dto highlight it in the page image. - Optionally stream the page image with
GET /files/:uploadedFileId/download.
async function pageForReference(fileId: string, pageNumber: number) {
return api<{ pages: Array<{ pageNumber: number; content?: string; regions?: Array<{ index?: number; content?: string; bbox_2d?: number[] }> }> }>(
`/documents/${fileId}/pages?startPage=${pageNumber}&endPage=${pageNumber}`,
);
}
4. Keep the citations with the answer
An analysis returns an answer rather than writing it anywhere; where the answer lives is your system's decision. Two useful things to do with a cited answer:
- Render it in your own system, resolving each reference to page text or a page image as shown above. This keeps the
[Ref:N]markers meaningful. - Leave a plain-text summary on the tender with POST /tenders/:tenderId/api-comments. That comment is labelled
APIand carries no citation metadata, so put the reference in the text (file name and page) if it matters:
curl -s -X POST "$KOSMOBIDS_BASE_URL/tenders/$TENDER_ID/api-comments" \
-H "x-api-key: $KOSMOBIDS_API_KEY" \
-H "content-type: application/json" \
-d '{
"content": "Analysis 2026-11-25: mandatory requirements met (specification.pdf p.12); one certification gap remains (specification.pdf p.18)."
}' | jq
{
"data": {
"id": "comment-id",
"timestamp": "2026-11-25T08:30:00.000Z"
},
"error": null
}
5. Handle failures
| Outcome | What to do |
|---|---|
409 on start |
No processed document yet: trigger the download, or upload one, and wait for processing (documentStatus = processed). |
failed or aborted |
The workflow will not recover. Start a new analysis if you still need the answer. |
404 on poll |
The workflow does not exist, belongs to another company, or is not a document-research workflow. |
| Partial answers | Successful runs return one answer per question. If a question is missing, check isStatusSuccess and the workflow status. |
Analyses consume LLM tokens. usage reports consumption for a successful run; budget accordingly and avoid duplicate analyses.