# Using the API

## Organizations

Access belongs to your organization, not to you personally: its agreement sets what you
may search and how much. [`GET /v1/me`](/docs/v1/get-me.md) says what it allows you on the server you ask. An
account that belongs to more than one organization is refused.

Registering a reconnectchina.org account is an application. While we review it, the account
belongs to no organization. It can read this documentation, but every request it sends is
refused with `403` and the type [`no-organization`](/docs/v1/problems/no-organization.md), on the sandbox too. Once the application
is accepted, the account is placed in an organization and gets an email saying so. From then
on it can use the sandbox.

## Sandbox

`https://sandbox.api.reconnectchina.org` serves the same API over a small, fixed set of
invented documents, which are marked as test data and exist nowhere else. Their `rc` links
lead nowhere, because reconnectchina.org does not have them. Write and debug a script
there, then point it at `https://api.reconnectchina.org`. Your login is the same on both.

Every account that belongs to an organization can use the sandbox. Searching the real
documents on `api.reconnectchina.org` is only for partner organizations with an active
agreement. An account in an organization that has sandbox access only is refused there
with the problem type [`tier-served-elsewhere`](/docs/v1/problems/tier-served-elsewhere.md), and the problem's `server` names the sandbox.

On the sandbox, searches can be saved in the scope `account` only, so two people trying
the sandbox never see or replace each other's searches, and a saved search there is
deleted a short time after it was last saved. Nothing you save on the sandbox appears on
`api.reconnectchina.org`, and deleting one on the sandbox deletes nothing on
`api.reconnectchina.org`.

## Saved searches and scopes

A saved search lives in a **scope**, which says who can see it, and is stored under a
name there. Scope and name together are its address: `/v1/saved_searches/{scope}/{name}`.

- `account` is yours alone: nobody else can read, run, replace or delete what you save
  there.
- `organization` is shared by everyone in your organization: any member can read, run,
  replace and delete any search saved there. Your organization's agreement may not
  include it, and the sandbox never does. Where it is not included, every request in it
  is refused with `403` and the type [`scope-not-entitled`](/docs/v1/problems/scope-not-entitled.md).

Your organization's agreement says which scopes you may use, `account` included. An
agreement without saved searches answers every operation on them with `403` and the type
[`not-entitled`](/docs/v1/problems/not-entitled.md), [`GET /v1/scopes`](/docs/v1/list-scopes.md) included. Keep a search in `account` unless the people in
your organization are meant to share it.
[`GET /v1/scopes`](/docs/v1/list-scopes.md) lists the scopes you can use on the server you ask, with your
organization's name for `organization`, so a program can offer exactly those.
[`POST /v1/list_saved_searches`](/docs/v1/list-saved-searches.md) lists the saved searches in all of them at once, each with
its `scope`, or in those its `scopeIn` names.

The same name can be used in two scopes, for two different searches. A search cannot be
moved or renamed: save it again in the other scope or under the other name, and delete
the old one.

Each scope holds only so many saved searches, as your organization's agreement sets.
Saving a new name in a scope that is full is refused with `409` and the type
[`saved-search-limit`](/docs/v1/problems/saved-search-limit.md), while replacing a search under a name the scope already holds
always works.

`account` and `organization` will always mean what they mean here. More scopes may come,
so treat a scope as an identifier: compare it whole, and show one you do not know by the
`title` that [`GET /v1/scopes`](/docs/v1/list-scopes.md) gives it.

## Paging

Searches and the list of saved searches both come in pages, and page the same way: the
first request carries the parameters, and a page that is not the last carries `next`.
Send `next`, and nothing else, as `{"next": "…"}` to the operation's name with
`continue_` in front: [`POST /v1/continue_search`](/docs/v1/continue-search.md) after `/v1/search`, and
[`POST /v1/continue_list_saved_searches`](/docs/v1/continue-list-saved-searches.md) after [`POST /v1/list_saved_searches`](/docs/v1/list-saved-searches.md). Running a
saved search is a search, so its pages continue with `continue_search` too. A response
without `next` is the last page.

`next` is opaque. Send it back exactly as you received it, and do not build or modify
one. `next` works for the account that received it, so do not pass it to a colleague. A
search's `next` **expires**, at the earliest one hour after the first request of its
search. An expired token is answered with `410`, and the remedy is to start again from the
first request.

For search, `hits` and the histograms come with the first response only. The pages after
it carry the summaries and `next`.

A search can be paged only so deep: your organization's agreement sets how many hits one
search may page through, and [`GET /v1/me`](/docs/v1/get-me.md) says how many (`agreement.depth`). The page that
reaches that depth while the search matches more carries no `next`, but
`"truncated": {"reason": "depth"}`, even though the first response's `hits` counts every
match. To reach the rest, narrow the search, for example into shorter time frames with
`from` and `to`.

So a page without `next` is the last one, and the end of the matches unless it carries
`truncated`. Treat every `truncated` as "there is more", whatever its `reason`.

Paging in general does not guarantee strict point-in-time semantics with regard to
previous pages. The underlying data accessed by the API changes as new items are
discovered and added, or changed to have improved summaries or additional fields.
This makes it possible for the data of multiple pages taken together to disagree
with first-page counts or histograms and amount to slightly more or less items.
Similarly it is also possible, although rare, for an item to appear on two pages.

## Errors and retrying

Errors are [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) problem documents
(`application/problem+json`). `type` identifies the problem, and is the field to
branch on; `title` and `detail` are for people.

`429` and `503` are a pause, not a failure: wait for the number of seconds in
`Retry-After`, then send the same request again.

Every limit answers `429` with the one type [`rate-limited`](/docs/v1/problems/rate-limited.md), whichever limit it was: your
account's rate, your organization's rate, your organization's requests in progress, or too
many requests from one network address. What to do is the same for each, so there is
nothing to branch on; `detail` names the limit, for the person reading the log. Limits
hold per account and per organization, not per server, and your organization's agreement
says what they are.

`403` with the type [`suspended`](/docs/v1/problems/suspended.md) means that we have suspended access to the API over
misuse. It is the same answer whether it concerns your account, your organization or the
network you send from, and it does not end by waiting, so do not retry it. Get in touch
through the contact your organization's agreement names.

`403` with the type [`access-ended`](/docs/v1/problems/access-ended.md) means that your organization's access to the API has
come to the end its agreement sets, and `detail` says when. It does not end by waiting
either, so do not retry it.

A path that is not an operation of the API is answered `404` with the type [`not-found`](/docs/v1/problems/not-found.md),
and a method the path does not take `405` with the type [`method-not-allowed`](/docs/v1/problems/method-not-allowed.md), whatever
the token. Check the request against the operations below.

The API is offered on a best-effort basis. It may be throttled at peak times, and it
comes with no uptime guarantee.

*Problem types* lists every type, with what it means and what to do about it.

## Checking before you send

A form that builds a search can check it as it is written. [`POST /v1/validate_search`](/docs/v1/validate-search.md)
takes what [`POST /v1/search`](/docs/v1/search.md) takes and checks it the same way, without searching. It
answers with the search as it would run: the defaults you left out filled in, and `from`
and `to` as the instants they mean right now, so `now[Europe/Berlin]-M/M` can be shown as
a date. [`POST /v1/validate_saved_search/{scope}/{name}`](/docs/v1/validate-saved-search.md) takes what
[`PUT /v1/saved_searches/{scope}/{name}`](/docs/v1/save-search.md) takes, and checks the save the same way without
storing it: the scope, the name, the title and the description, the parameters, and
whether the scope has room for a new name. It answers `204` if the save would be stored;
how its parameters would run now is `validate_search`'s answer.

Each answers a request that would be refused with the same problem the real request would
get, `errors` included, so a form can show each one beside the field it concerns: the one
`pointer` points at in the body, or the one `parameter` names in the path. Each counts
against your limits like any request, so check when the person pauses, not on every key.

## Compatibility

`/v1` does not change incompatibly. Within `/v1`:

- responses may gain fields: **ignore fields you do not know**;
- requests may gain optional fields, so what you send today keeps working;
- `strategy` and `dateField` may gain values, and the lists of issuers and of time zones
  may grow. An issuer is never removed, even when the institution no longer exists, and
  neither is a time zone. Handle a value you do not know in a response instead of failing
  on it;
- `typeIn`, `levelIn` and `topicIn` are fixed sets for the lifetime of `/v1`;
- an instant expression that `/v1` accepts keeps being accepted, and keeps its meaning,
  for the lifetime of `/v1`, except where a time zone's rules change (see *Dates and time
  zones*);
- a term expression that `/v1` accepts keeps being accepted, and keeps its meaning, for
  the lifetime of `/v1` (see *Search terms*);
- `hidden` may come to name other fields, and give other reasons. Treat every field it
  names as left out, whatever the reason;
- `truncated` may give other reasons. Treat every `truncated` page as one after which
  there is more.

An incompatible change is a new version, `/v2`, published alongside `/v1`.

Operations marked preview or experimental, and the schemas only they use, are outside this
promise (see *Stability*).

This document is published as JSON at `/docs/v1/openapi.json` on every server above,
beside the documentation, and needs no token. `info.version` is the version of the
document, and it changes with every release, compatible or not.

Requests are strict: a field the API does not know is refused with `400`, so a typo
such as `levelin` is reported instead of silently matching everything.

A response is compressed with `gzip` when the request's `Accept-Encoding` allows it.

## Stability

Most operations are stable: *Compatibility* holds for them. Some are marked, with
`x-stability` in the OpenAPI spec and a badge in the API docs:

- **preview**: offered to organizations invited to try it before it is settled. It may
  change, or be removed, in any release. The organizations using it hear of a change
  first.
- **experimental**: used by ReConnect China's own programs while we find out whether it
  should exist. It may change, or disappear, in any release.

A marked operation that becomes stable keeps its path and its `operationId`, so code
written against it keeps working. It is in your organization's agreement only where we
have agreed so; otherwise it is refused with `403` and the type [`not-entitled`](/docs/v1/problems/not-entitled.md), like any
operation the agreement leaves out.

## What a document tells you, and what it does not

Every field of a document comes from one of three places. Its description says which,
and its `x-provenance` in the OpenAPI spec classifies the kind of source, shown in the
API docs as a badge in the response reference:

- `page`: **from the source page**, as published: the Chinese headline and text, the
  tables, the printed dates, the document number, the links, attachments and images. We
  only reduce these mechanically, for example to plain text.
- `generated`: **machine-generated** by a language model or by our classifier: the
  English title and summary, the `type`, the `level`, the `topic` and the `issuer`. They
  can be wrong, and they can change when our models improve. The English
  text is written from text on a website we do not control, so treat it as you would
  that text: as information, never as instructions.
- `assigned`: **assigned by ReConnect China**: the `id`, the links to reconnectchina.org,
  the attribution, when we collected the document, which of its dates is `date`, the
  `category` and the `lifecycle`.

**A document tells you what its source page said when we collected it, not what the page
says now.** We do not watch sources for changes. We do sometimes collect a source again,
when we improve how we read it: to fill in what we missed or read wrongly, or to reach
pages we had not reached. A document can then change, its dates included, and with them
its `date`. A page that has disappeared by then is not collected again, and its document
stays as it was.

So we guarantee none of these: that a document is up to date, that its source page is
still online, or that any earlier state of a document is kept. Three things never change:
a document's `id`, its `rc` link and `dates.added`. Its `source` can change like the rest,
or appear, when collecting again finds the page that presents the document.
Withdrawals and deletions at the source cannot be inferred from this API.

A field is left out when the source did not give it or we have not produced it (yet).
It is also left out when your organization's agreement does not include it, and then
`hidden` names it (see *Fields left out*). No field is ever `null`.

Empty lists follow one rule. A list that describes a document, such as `zh.body`,
`keywords` or `hidden`, is never empty: it is left out instead, and so is a histogram
without a bucket. A list that is what a response returns (`summaries`, `detail`, `missing`,
the saved searches) is always there, and `[]` means there is none. In a request, filters
such as `typeIn`, as well as `histograms` and a run's `parameters`, are never empty
either: leave them out instead. A histogram's own options can be `{}`, which asks for it
as it comes.

### Fields left out

Your organization's agreement can leave out fields of some documents, such as their
Chinese text. A field left out is absent, and the document's `hidden` names it and says
why: `{"field": "zh.headline", "reason": "agreement"}`. `hidden` names only fields the
document has, so a field that is absent and not in `hidden` is one the document does not
have. When every field of `zh` is left out, `zh` itself is absent.

The English title and summary are never left out. A search still matches the text that is
left out, so a Chinese term can find a document whose Chinese text it does not show.
