Corpus Agent API
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.
| Base URL | https://api.corpus.ad/v1 |
|---|---|
| Authentication | Authorization: Bearer <API key> |
| Format | JSON request and response bodies, UTF-8 |
Before you start
- The API is enabled team by team. If your team does not have it yet, write to info@corpus.ad.
- The administrator of the team creates the keys in Corpus, under Settings → API. A key is shown once, when it is created: store it then. It can be revoked there at any time.
- Usage is charged to the credits of your team. Every answer says how many credits it used.
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.
| Request | What it does |
|---|---|
POST /v1/sessions | Creates a session. Body: user and instructions, both optional. Returns 201. |
GET /v1/session | Returns the session named by the Corpus-Session-Id header. |
PATCH /v1/session | Replaces the instructions. Body: session_id, instructions (a text, or null to remove them), user if the session has one. |
DELETE /v1/session | Deletes the session now, with the results of its queries. A query still running in it is stopped. |
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.
- A session belongs to the label it was created with. Every later request about that session must send the same label.
- Each label runs one query at a time; different labels run in parallel, up to the limit of your team.
- Usage is reported per label, to you and to your team's administrator.
Requests without a label share one conversation space of their own.
Queries
| Request | What it does |
|---|---|
POST /v1/queries | Asks a question. Body: session_id, question (up to 12,000 characters), and optionally user and wait_seconds (0 to 45). |
GET /v1/queries/{id}?wait=45 | Returns the query. With wait, the request stays open up to that many seconds while the query runs. Send Corpus-Session-Id. |
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 }
}
| Field | Meaning |
|---|---|
answer.markdown | The answer, in Markdown, with links to its sources. |
document | When you asked the agent to draft a document: its name and its text in markdown. Otherwise null. |
sources | The public references the answer cites, in order of appearance. unit and quote are reserved and may be null. |
files | Files you can download for this answer (see below). |
warnings | Machine-readable notes. answer_incomplete: the answer was cut before its end; ask again. |
usage.credits | Credits the query used. null for a moment after it ends, until the charge is recorded. |
error.code | Only when status is failed: refused (the question could not be processed safely), budget (it reached the limit of one query), timeout, session_deleted, lost or error. |
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.
| Status | Code | What to do |
|---|---|---|
| 400 | invalid_request | A field is missing, too long or of the wrong form. |
| 401 | invalid_key | The key is missing, wrong or revoked. |
| 402 | credits_exhausted | Your team has no credits left for the period. |
| 403 | team_not_enabled, team_not_active, team_frozen, team_payment_method, agreement_missing, key_owner_inactive, api_unavailable | The team or the key cannot use the API at present. Check the team's subscription and its data processing agreement in Corpus, or contact us. |
| 404 | session_not_found, query_not_found, file_not_found, not_found | It does not exist, has expired, or belongs to another key or label. |
| 409 | query_in_progress | This key and label already have a query running; query_id names it when it is in the same session. Collect it, then ask again. |
| 409 | idempotency_mismatch | The Idempotency-Key was used with another question. |
| 410 | result_expired | The result of the query is no longer kept. |
| 413 | body_too_large | The request body is over 256 KB. |
| 429 | rate_limited, team_concurrency_limit, too_many_sessions | Wait for the Retry-After header's seconds, or for a running query to end, and try again. |
| 503 | unavailable, api_draining | Temporary. Try again with a growing delay. |
Limits
- 120 requests a minute per key.
- One running query per key and label; 5 running queries per team unless agreed otherwise.
- 20 open sessions per key and label.
- A single query stops when it reaches its own spending limit and reports
budget.
How your data is handled
- Questions and instructions are anonymised on Corpus's servers before the agent's model reads them: names and other personal identifiers are replaced by placeholders, and the real values are put back in the answer you receive. If the anonymiser is not available, the query is refused rather than sent.
- Sessions and results are stored encrypted, in the European Union, with a key derived from the
session_idthat only you hold. They are deleted when the session expires or when you delete it. - Corpus does not record questions, answers, session ids or user labels in its logs.
- The privacy policy, the terms of use and your team's data processing agreement apply to the API.
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.