Corpus Agent API

Version 1 · Last updated: 2 December 2026

The Corpus Agent API lets your own systems ask Corpus Agent questions about the law of Andorra and receive a researched answer with its sources. It is a JSON API over HTTPS, meant to be called from a server.

Before you start

Keep the key on your server

An API key gives access to your team's credits. Send it only from your own back end, never from a browser or a mobile app: the API does not answer cross-origin requests from web pages. Do not put a key in a URL. If a key may have leaked, revoke it and create another.

Quick start

A conversation is a session. Create one, then ask.

# 1. Create a session
curl https://api.corpus.ad/v1/sessions \
  -H "Authorization: Bearer $CORPUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
# → {"session_id":"cs_…","created_at":"…","expires_at":"…","turns":0,"has_instructions":false}

# 2. Ask, waiting up to 45 seconds for the answer
curl https://api.corpus.ad/v1/queries \
  -H "Authorization: Bearer $CORPUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"session_id":"cs_…","question":"What is the general IGI rate?","wait_seconds":45}'
# → {"id":"op_…","status":"running", …}   or   {"id":"op_…","status":"completed", …}

# 3. If it is still running, collect it (repeat until it is not "running")
curl "https://api.corpus.ad/v1/queries/op_…?wait=45" \
  -H "Authorization: Bearer $CORPUS_API_KEY" \
  -H "Corpus-Session-Id: cs_…"

An answer usually takes between twenty seconds and a few minutes. Ask a follow-up in the same session and the agent remembers the conversation.

Sessions

A session holds one conversation: its turns and, optionally, your instructions. It expires 24 hours after its last use and never lives longer than 7 days. The session_id is a secret that only you hold: Corpus cannot open a session without it, and it cannot be recovered if you lose it.

Instructions (up to 12,000 characters) apply to every turn of the session: the tone, the language, the role of the reader. They cannot change the rules the agent works under.

The session id travels in the JSON body of POST and PATCH requests and in the Corpus-Session-Id header of GET and DELETE requests. It is never part of a URL.

Your own users

If one key serves several people in your organisation, send a label for each of them in user (in the body) or in the Corpus-User header. The label is yours to choose: 1 to 64 characters among letters, digits and . _ : -. Prefer an opaque identifier to a name or an email address.

Requests without a label share one conversation space of their own.

Queries

A query is running, completed or failed. Once a query has been accepted, the response is always 200: read status.

{
  "id": "op_3f9c…",
  "status": "completed",
  "answer": { "markdown": "The general rate of the IGI is …" },
  "document": null,
  "sources": [
    { "title": "Llei 11/2012 …", "url": "https://corpus.ad/…", "unit": null, "quote": null }
  ],
  "files": [],
  "warnings": [],
  "usage": { "credits": 3 }
}

A query that fails after it started may still have used credits; usage.credits says how many. Results are kept for 24 hours and are deleted with their session.

Retrying safely

Send an Idempotency-Key header (1 to 128 printable ASCII characters, unique per question) with POST /v1/queries. If the request is repeated with the same key, for example after a network error, you get the same query back and are not charged twice. The same key with a different question returns 409 idempotency_mismatch.

Documents

When the answer drafts a document, files lists it as a Word file:

"files": [
  { "id": "file_3f9c…", "name": "Contract.docx",
    "type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }
]

Download it with GET /v1/files/{id}/content, sending Corpus-Session-Id (and Corpus-User, if the session has a label). The file carries your team's document style, if you set one in Corpus. It is available as long as the result of its query is. Sending your own files to the agent is not available in this version.

Usage

GET /v1/usage?month=YYYY-MM returns what the key used in a month (the current one by default), by day and by label:

{ "month": "2026-12", "rows": [ { "day": "2026-12-02", "user": "u-17", "queries": 4, "credits": 11 } ] }

The administrator of your team sees the same figures in Corpus, under Settings → API.

Errors

A request that is not accepted returns an HTTP error status and a body of this form:

{ "error": { "code": "session_not_found", "message": "No such session for this key and user.", "request_id": "req_…" } }

Act on code, not on message, whose wording may change. Quote the request_id (also in the Corpus-Request-Id response header) when you write to us.

Limits

How your data is handled

Versioning

This is version 1, in the path of every address. Within a version we may add endpoints, optional request fields, response fields and error codes; write your client so that it ignores what it does not know. A change that would break a working client is released under a new version.

Support

Write to info@corpus.ad. Corpus is operated by CORPUS PROJECT, SL, Andorra la Vella, Andorra.