# Lawline API Documentation `https://api.dev.lawline.se` Lawline is a legal AI agent for Swedish and EU law. Lawline's agent researches primary legislative sources and answers with verifiable citations. The Lawline API exposes Lawline's legal agent over an HTTP API ([`/v1`](/v1.md)) and an MCP server ([`/mcp`](/mcp.md)). An embedded widget ([`/iframe`](/iframe.md)) is coming soon. Access is managed by the [Lawline Platform](https://platform.dev.lawline.se). See the [connection and billing guide](/getting-started.md). ## Endpoints | Surface | Path | Use-case | | --- | --- | --- | | HTTP API | [`/v1`](/v1.md#quickstart) | Call the legal agent directly. Its chat endpoint is OpenAI-compatible, so an OpenAI SDK works unchanged. | | MCP server | [`/mcp`](/mcp.md#quickstart) | Give an existing agent Lawline as a legal tool. | | Embedded widget | [`/iframe`](/iframe.md) | Coming soon: Lawline directly inside a web page. | The [OpenAPI 3.1 document](https://api.dev.lawline.se/v1/openapi.json) describes every `/v1` operation. [`/v1/health/ready`](https://api.dev.lawline.se/v1/health/ready) answers `200` when the API database answers, and [`/v1/health`](https://api.dev.lawline.se/v1/health) answers while a server is up. None of them needs an API key. Every page of these docs is also plain markdown for coding agents and other tools: start at [`/llms.txt`](/llms.txt). Changes and deprecations are in the [changelog](/changelog.md). The [status page](/status.md) shows whether the API answers right now, and lists past incidents. ## Authentication Use an API key for `/v1` or `/mcp`. Create and manage keys in the [Lawline Platform](https://platform.dev.lawline.se). Send `Authorization: Bearer llk_...` and select the required scope. For MCP, OAuth is also available when enabled in the environment. OAuth grants `mcp` access only. See the [connection and billing guide](/getting-started.md). A key is rejected with `403` if out of scope. Revoked and expired API-keys return `401`. An expired key carries `code` `expired_api_key` and `details.expired_at`, so it is not mistaken for a wrong key. A key can be issued without an expiry. ## Legal Sources The legal agent searches the following sources. All are always active, except those marked (legacy). | Key | Coverage | | --- | --- | | `sfs` | Swedish statutes | | `proposition` | Preparatory works, including provision commentary. | | `case` | Swedish cases and precedent (e.g. NJA). | | `eu` | EU regulations, directives, and decisions (e.g. GDPR). | | `agency` | Documents from Swedish agencies and similar bodies: regulations, general advice, guidance, statements and decisions. | | `qa` | Lawline's Q&A database. | | `boverket` (legacy) | Building regulations (BBR, EKS, OVK). Not searched. The documents are part of `agency`. | | `authority` (legacy) | Agency regulations not in SFS (SKVFS, AFS, SOSFS, TSFS, SKOLFS). Not searched. The documents are part of `agency`. | | `skatteverket` (legacy) | Tax authority guidance. Not searched. The documents are part of `agency`. | A citation names the corpus it came from in `type`. The value matches the key above except for statutes, which carry `sfs2`. Keys marked (legacy) do not appear in `type`. A file from the organisation's memory carries `document`. ## Idempotency `POST /v1/chat/completions` and `POST /v1/review` accept an `Idempotency-Key` header: 1 to 255 printable ASCII characters, chosen by you. On `/v1/chat/completions` the first request with a key runs and its answer is kept in the API database for 24 hours, and is removed with your tenant; a repeat of the same request returns that answer with `Idempotent-Replayed: true` and is not run or billed again. Each replay, also on `/v1/review` and on background answers, carries `Idempotent-Replay: true` as well. It is a deprecated alias of the same header, so read `Idempotent-Replayed`. A repeat while the first is still running returns `409 idempotency_in_progress` with `Retry-After`; the same key over a different body returns `409 idempotency_key_reused`; a key whose answer was above the 256 KB kept for replay returns `409 idempotency_not_replayable`, and needs a new key. Each carries `details.original_request_id`. A key that a run already holds, from `/v1/review` or a background answer, returns `409 idempotency_key_reused` with `details.original_run_id` instead. A request that failed gives its key back, so a retry runs. A request whose connection you dropped before the answer was whole gives its key back too, if the server saw the drop, as the next paragraph describes. What that attempt ran is metered as a cancellation. After `prompt_injection_blocked`, the error table below says what is billed. A failure inside a streamed answer counts as a failure even though the status line already said 200. A timeout or an abort in your client stops an answer only when the server sees that the connection dropped. Over HTTP/2 the server sees it at once. Over HTTP/1.1 the server sees it only when it next writes to the connection. A streamed answer writes text or a ping at least every 15 seconds, so it stops within that time. A non-streamed answer writes nothing until it is complete, so over HTTP/1.1 it runs to the end, is billed in full and keeps its key. Both OpenAI SDKs use HTTP/1.1 by default, so stream the answer or use an HTTP/2 client when a timeout must stop it. The OpenAI SDKs retry after a timeout and send no key of their own, so each retry without a key starts and bills a new answer. To make a retry free, send an `Idempotency-Key` with each call. In openai-python, pass it in `extra_headers`. In openai-node, pass it in the `headers` of the request options. The SDK then sends the same key with each retry of that call. While the first attempt runs, a retry gets `409 idempotency_in_progress` with `Retry-After`, and once the first attempt is complete, a retry gets its answer and is not billed again. An OpenAI SDK retries a `409` at most twice by default, so it can give up while the first attempt still runs. Then wait the `Retry-After` seconds and send the same request with the same key yourself, or raise `max_retries`. On `/v1/review` the key names the review's run, and a repeat of the same request reaches that run for as long as it is kept, seven days after it ends: it follows the run while it is going and hands back its result once it is done, with a freshly signed report link. Nothing is run or billed again. A repeat that reaches the run carries `Idempotent-Replayed: true`. A completed run keeps its key, so use a new key for a new review; the same key over a different body returns `409 idempotency_key_reused` with `details.original_run_id`. A run that failed or was cancelled gives its key back, so a retry runs. If the review cannot be handed on, the answer is `503` with the run in `details.run_id`; with a key, repeating the request hands it on, and the service also picks it up by itself within minutes. Without a key, repeating a request with the same body while its run is still queued or running reaches that run; once it has ended, the repeat starts and bills a new review. So send a key with any review you may retry. A body with `document_url` counts as the bytes that the link served, not as the link. So a new signed link to the same file is the same request, and a link whose file changed is a new request. Lawline downloads the link again on each repeat to read those bytes, so keep it valid for as long as you may retry. A repeat whose link no longer works gets `400 document_url_unreachable` and does not reach the run. Follow the run with `GET /v1/runs/{id}` instead. On `/v1/chat/completions` with `background: true`, where background answers are available, the key names the answer's run. The rules of `/v1/review` above apply. Where background answers are no longer available, a repeat with the same key and body still gets its run with `200`. A conflict over a key that a run holds carries `details.original_run_id`, and one over a key that an inline answer holds carries `details.original_request_id`. One key names one request: the same key over any other body returns `409 idempotency_key_reused`, also when only `background` differs. So a key used for an inline answer is refused for a background answer, and the reverse. A key that an inline answer received before background answers were available keeps that answer, and a repeat gets it back. Without a key, a repeat starts and bills a new answer. The one exception is an automatic retry from an OpenAI SDK, which reaches a run with the same body from the same API key. That run started at most 30 seconds earlier for each retry the SDK counts, and at most two minutes earlier. It is the run that the lost response described, unless you sent identical requests at the same time. So send a key with every background answer. ## Agent Memory Shape legal responses by modifying the agent's memory. On the **Memory** page of the Lawline Platform, the **Context** block can be set to inject policies, terminology, and rules up to 20,000 characters. It shapes how the agent answers. **Files** uploaded on the same page are read by the agent when it answers a legal question, on `/v1/chat/completions` and in the MCP tool `lawline-v2`. Document review does not read them. The agent reads PDF, Word (`.docx`) and text files (`.txt`, `.md`, `.csv`) of up to 25 MB. It reads up to about 1,000 pages of an organisation's files in all, and up to about 130 pages of one Word file. An upload past that limit is refused, and a single file longer than that is stored but not read. Each file is read when it is uploaded. The **Memory** page marks a file that the agent does not read. An organization stores at most 1 GiB on standard or 10 GiB on enterprise. All files count, including files the agent cannot read. Uploads in progress reserve storage. Files awaiting deletion count until storage confirms deletion. A full budget returns `400 memory_storage_full` with `used_bytes`, `requested_bytes` and `limit_bytes`. Existing files are kept. A file or queued deletion with missing size metadata blocks new uploads with `503 memory_storage_unknown` until its metadata is corrected or storage confirms deletion. Contact support. A file is a fact about your organisation and never a legal source. Where a file conflicts with the law, the answer follows the law. An answer names the file that it cites, and quotes the passage when the sentence uses its words. The citation in `citations` carries `type` `document` and the file's `id`. It has no `url`, so it is not in `annotations`. The **Context** block applies to all requests across all endpoints. ## Limits & Billing Usage is metered in **Lawline tokens**. **Usage** can be tracked and visualised in the Lawline Platform. All new organisations have the standard tier by default. Contact Lawline to upgrade. | Tier | Requests Per Minute | Lawline Tokens Per Month | Active Reviews | Active Answers | Long requests per instance | | --- | --- | --- | --- | --- | --- | | Standard | 60 | **100M** | 3 | 20 | 2 | | Enterprise | 600 | **10B** | 20 | 60 | 4 | Exceeding the request rate returns `429` with a `retry_after` (seconds). An organisation without active billing is rejected with `402`. The monthly quota counts recorded Lawline-token usage in the current calendar month, in UTC. Each metered model or embedding call checks this quota before it starts. If recorded usage reaches the quota, new calls stop with `402 quota_exceeded`. Calls already admitted can finish and remain billable, so simultaneous work can exceed the quota. A review on `POST /v1/review`, and a chat answer with `background: true`, run on the server as runs. Your tier allows the active reviews and active answers in the table, queued or running at once. Each kind counts only its own runs, so reviews and answers never block each other. One more returns `429 too_many_active_runs` with `Retry-After`: 30 seconds for a review and 5 for an answer. Reading or following a run is not checked against the monthly quota or billing, so a result you already paid for stays readable, but it counts against your requests per minute. A repeat that reaches its run, with the same key or as an OpenAI SDK retry, is a read in this sense. Cancelling a run is checked against none of the three, and its response carries the shared ceiling's `RateLimit-*` numbers. The MCP tool `review_document` also creates a server-owned run and counts toward the same limit of active reviews. So does `review_document_upload`: the call is refused the same way while your organization already has as many reviews queued or running as its tier allows, and the upload takes a place once it lands. A review that waits for its document holds no place. `get_review` reads that run without new model work or a billing/quota check. It counts against your request rate. `cancel_review` stops the run without a billing, quota or tenant request-rate check. Where answers run on the server, the MCP tool `lawline-v2` also creates a server-owned run and counts toward the same limit of active answers. `get_answer` reads it like `get_review`, and `cancel_answer` stops it like `cancel_review`. Authentication, scope and the shared IP limit still apply. The API refuses new runs when the oldest ready run of the same organization and kind has waited five minutes for its first start. Preparation and upload time do not count. Existing runs continue, and other organizations and run kinds remain independent. A second ceiling of 1000 requests per minute per source IP applies ahead of the tier limit, shared by everyone behind the same egress address. Each server instance counts this ceiling on its own. The API can run on several instances, so one source IP can get more than 1000 requests per minute through. Pace against your tier's limit, not against this ceiling. A response that reached your tier's limiter carries `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset` and `RateLimit-Policy` for your per-minute window. A request rejected before it, which is any `401`, `402`, `403`, `413` or a `429` from the shared ceiling, carries that ceiling's numbers instead. The instance that answered counted these numbers, so `RateLimit-Remaining` from the ceiling can go up between two responses in the same window. `RateLimit-Limit` tells the two apart: your tier's limit is the one in the table above. ## Errors Errors return a JSON `error` object with `type`, `code`, `message`, `request_id`, and on some errors `param` and `details`. `param` names the request field that the error is about, as a dot path, for example `messages.1.role`. `type` is the value in the table of types below. `code` is a narrower machine string. It equals `type` when there is no narrower cause. The table of codes below lists every other `code` that the API sends. New codes can be added, so use `code` to tell cases apart, and handle a `code` that you do not know by its `type`. On `402 payment_required`, `code` is the billing status of the organisation (`pending`, `past_due`, `canceled`). `details` carries `retry_after` (seconds) on `rate_limited`, `used` and `limit` on `quota_exceeded`, and `max_bytes` on `payload_too_large`. A `429` also sets the standard `Retry-After` response header. `too_many_connections` means that this organization or this instance has reached its simultaneous-request limit. `details.limit` names the limit. `details.scope` is `tenant` or `instance` on HTTP and MCP. The tenant allowance is per instance. Wait for `Retry-After` before retrying. Long requests include inline answers, foreground reviews, run event streams and uploads. Background reviews also count while fetching `document_url`. MCP generation and waiting calls count too. Metadata, cancellation, upload-link creation and `get_answer` or `get_review` with `wait_seconds: 0` bypass the long-request limit. After a tool call's body is read, an MCP refusal is a `200` tool result with `isError: true`. Worker deliveries share the instance's long-request budget. This leaves application headroom for shorter work. It does not reserve Cloud Run request slots or guarantee that cancellation bypasses platform queuing. On `too_many_active_runs` it also carries `limit`, the most runs of that kind your tier allows at once, and `kind`, the kind of run that reached it (`review` or `answer`). A background answer sent without a key carries `x-should-retry: false` on `too_many_active_runs` or `queue_wait_exceeded`. An OpenAI SDK does not retry it. So do `background_unavailable`, `idempotency_key_reused`, `idempotency_not_replayable` and `upstream_refused`, which a retry meets again, and `request_timeout`, which a retry runs and bills again. On `409 conflict` it carries `original_request_id` when an inline request holds the `Idempotency-Key`, and `original_run_id` when a run holds it. On the `/v1/review` stream, the error that ends a review carries the run in `run_id`. For `queue_wait_exceeded`, `used` and `limit` measure queue wait in seconds. `limit` is 300 and `scope` is `tenant`. On `POST /v1/chat/completions` and `POST /v1/review`, a top-level field with the value `null` counts as absent, so its default applies. A field that the endpoint does not take returns `400` with `code` `unsupported_parameter`. `details` names each such field, up to 20, and `param` names the first. If there are more than 20, `message` gives the number of the other fields. If the body has another error too, the answer is `422` `validation_failed`, and `details` names up to 20 fields that failed, the unsupported ones included. A request body has a size cap. The cap is 1 MB, and 15 MB on the endpoints that take a document, which are `/v1/review` and `/mcp`. A body above the cap gets `413`. On `/v1`, a body without `Content-Length` (chunked transfer-encoding, or an HTTP/2 client that omits the header) is accepted, and its bytes count against the cap as they arrive. On `/mcp`, a request body must declare `Content-Length`. A body without it gets `400` `missing_content_length`, and a malformed length gets `400` `invalid_content_length`. The document in a review has its own cap of 10 MB after base64 decoding or after download from `document_url`, and a larger document also gets `413`. An upload link of `review_document_upload` takes the PDF or Word (.docx) file itself, up to 10 MB, with or without `Content-Length`. Declared lengths above the cap are refused before authentication. Bodies without a declared length authenticate before their bytes are read. Bodies on `/v1`, `/mcp` and upload links must arrive within 120 seconds, or receive `408 body_timeout`. Arriving bodies and long requests have separate tenant allowances on each instance, using the table's limit. A full body allowance can also refuse a body carrying an MCP cancellation. Refusals while a body is arriving are HTTP errors, because its JSON-RPC id is not known. | Status | Type | Meaning | | --- | --- | --- | | 400 | `bad_request` | Malformed request, or a field that the endpoint does not take (`code` `unsupported_parameter`). | | 401 | `unauthorized` | Missing or invalid API key. `code` is `expired_api_key` when the key was valid and has expired, with `details.expired_at`. | | 402 | `payment_required` | No active billing. | | 402 | `quota_exceeded` | Monthly Lawline-token quota reached. The quota covers a UTC calendar month. Each metered model or embedding call checks it before starting. Calls already admitted finish and are billed, so simultaneous work can exceed the quota. | | 403 | `forbidden` | Key lacks the required scope, or the API key or connection that started the review of an upload link no longer works. | | 404 | `not_found` | No such route, an unsupported method on a documented path, a run that is not yours or does not exist, or an upload link that is not valid. | | 408 | `body_timeout` | The request body did not arrive within 120 seconds. Send it again. | | 409 | `conflict` | An `Idempotency-Key` that is still running, was used for a different request, or whose answer is gone, or an upload link whose review already has its document. | | 413 | `payload_too_large` | Request body above the endpoint's cap, or a review document above 10 MB. `details.max_bytes` names the cap that applied. | | 422 | `validation_failed` | Request failed validation. `details` names each field that failed, up to 20, and `param` names the first. | | 429 | `rate_limited` | Too many requests. | | 503 | `upstream_unavailable` | The model provider or another service that the request needs is not available, or the answer did not complete. `code` names the cause. | | 500 | `internal` | Unexpected error. | A `500` or a `503` points at Lawline or its model provider, not at your request. [Status and incidents](/changelog.md#status) says how to tell an incident from a fault in your request, and what to send Lawline about one. One more `type`, `tool_failed`, has no status. It comes only in an MCP tool result, when the tool name or the arguments are not valid. Some codes arrive in a response that already started: in an `error` event of a stream, in the `error` of a run, or in an MCP tool result. These carry the same `type` and `code`, and the table gives the status of their `type`. | Code | Status | Meaning | | --- | --- | --- | | `invalid_content_type` | 400 | The body is not sent as JSON. Send `Content-Type: application/json`. | | `missing_content_length` | 400 | On `/mcp`, the body does not declare `Content-Length`. | | `invalid_content_length` | 400 | On `/mcp`, `Content-Length` is not a number of bytes. | | `invalid_idempotency_key` | 400 | `Idempotency-Key` is blank, or has a character that is not printable ASCII. | | `invalid_starting_after` | 400 | `starting_after` or `Last-Event-ID` is not a sequence number. | | `unsupported_parameter` | 400 | The body has a field or a value that the endpoint does not take. Remove it. | | `background_unavailable` | 400 | `background: true` where background answers are not available. Send the request without `background`. | | `background_stream_unavailable` | 400 | `background: true` with `stream: true`. Send only one of them. | | `invalid_document` | 400 | The document is not base64, not a PDF or Word (.docx) file, encrypted, an old Word 97-2003 (.doc) file, or has no text that can be read. A scan needs OCR first. Send an unencrypted PDF with a text layer, or a .docx file without a password. | | `invalid_document_url` | 400 | `document_url` is not a public https link, or it redirects to one that is not. The message says why. Send a link to a public host, or send the document base64 encoded. On MCP, you can upload the file with `review_document_upload`. | | `document_url_unreachable` | 400 | Lawline could not download `document_url`: the host does not resolve, does not start to answer within 5 seconds, does not finish within 15 seconds, answers with an error status, sends the file compressed, or redirects more than 3 times. Check the link, or send the document base64 encoded. On MCP, you can upload the file with `review_document_upload`. | | `document_too_long` | 400 | The PDF has more than 30 pages, or the Word file holds more than 120,000 characters or unpacks to more than a review reads, which a file heavy with formatting can do first. Save a Word file as a PDF of at most 30 pages, or split it into smaller documents. | | `report_requires_pdf` | 400 | A Word file with `include_pdf: true`. The annotated report exists for a PDF only. Send the Word file without `include_pdf`, or with `include_pdf: false`. On an upload link, upload a PDF, or start a new review with `include_pdf` false or unset. | | `upload_link_expired` | 400 | The upload link expired 15 minutes after the review started. Start a new review with `review_document_upload`. | | `document_not_uploaded` | 400 | No document reached the upload link before it expired, 15 minutes after the review started, or in the 2 minutes that an upload in progress gets after that. The review ended without cost. Start a new review. | | `prompt_injection_blocked` | 400 | The safety check stopped text that tries to reveal or replace the agent's instructions. It reads the last user message, and on a review the `instructions` and the `document`. The request stops as soon as the check flags the text. Lawline does not bill the work that the request did, except work that was already billed before the check flagged the text. A long answer or review is billed while it runs, so its earlier parts can already be billed. This applies to 5 blocked requests per organization in 24 hours. Lawline bills each blocked request after those as a failed request. Change that text. | | `mcp_session_required` | 400 | On `/mcp`, a tool call came without the session from `initialize`. Initialize the session first. | | `mcp_request_id_used` | 400 | On `/mcp`, the JSON-RPC request id is already used or cancelled. Use a new id. | | `run_cancelled` | 400 | The run that the request waited for was cancelled. | | `missing_bearer_token` | 401 | The request has no `Authorization: Bearer` header. | | `invalid_api_key` | 401 | The API key does not exist or is revoked. | | `expired_api_key` | 401 | The API key expired at `details.expired_at`. Create a new key. | | `scope_required` | 403 | The API key does not have the scope for this surface, `v1` or `mcp`. | | `upload_link_revoked` | 403 | The API key or connection that started the review of the upload link was revoked or has ended, so the link no longer works. Start a new review from the app with `review_document_upload`. | | `run_not_found` | 404 | Your organisation has no run with this id. | | `upload_link_invalid` | 404 | The upload link is not valid. Use `upload_url` exactly as the MCP tool gave it. | | `upload_requires_put` | 404 | The upload link takes only a `PUT` of the PDF or Word (.docx) file itself, for example with `curl -T`, not a form. | | `idempotency_in_progress` | 409 | A request with this key still runs. Send the request again after `Retry-After` seconds. | | `idempotency_key_reused` | 409 | The key belongs to a different request. Use a new key. | | `idempotency_not_replayable` | 409 | The answer to the request with this key was too large to keep. Use a new key. | | `run_not_awaiting_upload` | 409 | The review of the upload link already has its document, or it ended. Read it with `get_review`. | | `too_many_active_runs` | 429 | Your organisation has the most active runs of this kind that its tier allows. Send the request again after `Retry-After` seconds. | | `queue_wait_exceeded` | 429 | The oldest ready run of this kind in your organization has waited five minutes to start. Wait for `Retry-After`, then retry. Existing runs continue. | | `too_many_waiting_uploads` | 429 | The API key or connection already has as many reviews waiting for an upload as your organisation may run at once. Upload to one of the reviews that the message names, or stop one with `cancel_review`. | | `run_attempts_exhausted` | 500 | The run failed in each attempt. Start a new run. | | `run_result_not_storable` | 500 | The run finished, but the service could not store its result. Start a new run. | | `request_timeout` | 503 | The time ran out. A non-streamed chat answer stops after 4 minutes, so stream it or use `background: true`. On `/v1/review`, the review continues: read its run. A background answer fails after 13 minutes. | | `empty_answer` | 503 | The model gave no text. Send the request again. | | `incomplete_answer` | 503 | The model stopped before the answer was complete. Without `stream`, no part of it is sent. With `stream`, discard the content chunks that came before the `error` event. Send the request again. | | `upstream_refused` | 503 | The model provider refused the request, and it refuses the same request again. The error carries `x-should-retry: false`. Change the request before you send it again. If it fails again, contact Lawline with the `request_id`. | | `run_result_unavailable` | 503 | The review finished, but the report link could not be made. Read the run again. |