# Raintree Workspace Data API

The Raintree workspace data API is a read-only HTTP API. It lets an authorized agent or script read chat messages, list workspace files, and download files from one Raintree workspace.

Every workspace serves its own copy of the API under `/api/v1/`. The API accepts `GET` and `HEAD` only. `HEAD` is supported on `/api/v1/`, `/api/v1/openapi.json`, and file downloads.

## Where the spec lives

- Canonical machine spec: `GET <origin>/api/v1/openapi.json`. It is OpenAPI 3.1 and needs no token. Its `servers` entry uses the origin you requested.
- In-workspace guide: `GET <origin>/api/v1/`. It is markdown and covers the same endpoints with examples.
- This page: the public overview for agents and developers.

Use `openapi.json` to generate a client or check parameters. Use the guide for a quick reference from inside the workspace.

## Connect

Use the workspace origin, for example `https://<name>.workspace.raintree.tech`. Send a Bearer token that starts with `rtw_`:

```bash
export ORIGIN=https://<name>.workspace.raintree.tech
curl -H "Authorization: Bearer rtw_..." "$ORIGIN/api/v1/chat_log"
```

Keep tokens out of logs and shared notes.

## Endpoints

| Method | Path | Purpose |
| --- | --- | --- |
| GET, HEAD | `/api/v1/` | Markdown guide |
| GET, HEAD | `/api/v1/openapi.json` | OpenAPI 3.1 spec. No token needed. |
| GET | `/api/v1/chat_log` | List chats or read messages |
| GET | `/api/v1/files` | List one folder, or a whole tree with `recursive=1` |
| GET, HEAD | `/api/v1/files/<path>` | Download one file |

Unknown or repeated query parameters return `400`. Use only the parameters listed below.

## Chat log

`GET /api/v1/chat_log` takes these parameters:

- `chat`: a chat id, a unique case-insensitive name substring of at least 3 characters, or `all`.
- `text`: case-insensitive substring of the message text.
- `sender`: substring of the sender's name or id.
- `since`: start of the time range.
- `until`: end of the time range.
- `n`: number of messages to return. An integer from 1 to 200. The default is 50. For more, repeat the request with `cursor`.
- `cursor`: the opaque `next_cursor` from the previous response. Repeat the same request with it to read older messages.

`since` and `until` are inclusive. A bare date (`YYYY-MM-DD`) covers its whole UTC day: `since` starts at 00:00:00Z and `until` ends at 23:59:59.999Z. A date-time needs `Z` or a UTC offset. Bare numbers and date-times without a zone return `400`. Every time in a response is UTC ISO-8601 with `Z`.

With no `chat` (or `chat=all`) and none of `text`, `sender`, `since`, `until`, `n`, or `cursor`, the endpoint lists chats:

```json
{
  "chats": [
    {
      "id": "C9773370",
      "name": "Team",
      "type": "group",
      "provider": "line",
      "messages": 412,
      "last_activity": "2026-11-01T08:30:00Z",
      "participants": ["Alice", "Bob"],
      "files_dir": "inbox/line/C9773370"
    }
  ]
}
```

`messages` counts messages in both directions. `participants` lists the known participant names. `files_dir` is the workspace path of the chat's attachment folder, or `null`.

Any of `text`, `sender`, `since`, `until`, `n`, or `cursor`, or a `chat` name or id, returns messages oldest first:

```json
{
  "messages": [
    {
      "id": "412",
      "time": "2026-11-01T08:30:00Z",
      "chat": "C9773370",
      "sender": "Alice",
      "sender_id": "U100",
      "direction": "inbound",
      "text": "Shipment confirmed.",
      "attachments": [
        {
          "path": "inbox/line/C9773370/photo-1.jpg",
          "name": "photo-1.jpg",
          "mime": "image/jpeg",
          "size": 204800
        }
      ]
    }
  ],
  "truncated": true,
  "next_cursor": "v1.eyJ0IjoxNzkzNTIxODAwMDAwLCJpZCI6NDEyfQ"
}
```

- `id` is a stable string that identifies one message. Use it to tell messages apart; do not parse it.
- `sender_id` is a stable id. It is `null` for outbound and unknown senders.
- `direction` is `inbound` or `outbound`. Outbound messages show `sender` as `Raintree`.
- Each attachment `path` works with `/api/v1/files/<path>`. It is `null` when the file is not stored. The API has no newest-20 cap on attachments.
- `truncated` is `true` when older matching messages exist. The API fetches one message more than `n` to set this value.
- `next_cursor` appears only when `truncated` is `true`. It is opaque: send it back exactly as received and do not parse it.

To read older messages, repeat the same request with `cursor` set to `next_cursor`. Keep every other parameter unchanged, because the cursor records only the position. Pages never skip or repeat a message, even when several messages share a timestamp. Each page is oldest first, so sort the pages you collect oldest first to get one list.

## Files

`GET /api/v1/files?path=<dir>` lists one folder level. Omit `path` for the workspace root. Use `recursive=1` or `recursive=true` to list the whole tree. `recursive=0` or `recursive=false` (or omitting it) lists one level. Any other `recursive` value returns `400`.

```json
{
  "path": "inbox/line/C9773370",
  "entries": [
    {
      "path": "inbox/line/C9773370/photo-1.jpg",
      "name": "photo-1.jpg",
      "type": "file",
      "size": 204800,
      "modified": "2026-11-01T08:30:00Z"
    }
  ],
  "truncated": false
}
```

- Each entry `path` is the full workspace-relative path.
- A recursive listing is flat and sorted by path. It is gathered breadth-first, with names sorted within each level, and holds at most 5000 entries. `truncated` is `true` when more than 5000 entries exist, or when a directory path is longer than 1024 characters; that directory's children are skipped. When `truncated` is `true`, the list is not the full tree. To see the rest, list a subdirectory with `path=`.
- A one-level listing always sets `truncated` to `false`.

`GET /api/v1/files/<path>` returns the file body. `HEAD` returns the same headers with no body. Downloads are limited to files of at most 50 MiB.

- Responses up to 1 MiB include Content-Length. Larger responses stream with chunked transfer encoding and no Content-Length. HEAD always includes the full Content-Length. Range (206), ETag, Last-Modified, 304 and 416 apply to all sizes.
- `ETag` is `W/"<size>-<mtimeMs>"`. Send `If-None-Match` with that value. The API returns `304` when the tag matches.
- `Last-Modified` and `Accept-Ranges: bytes` are set.
- Send one `Range: bytes=a-b`, `bytes=a-`, or `bytes=-n` header. The API returns `206` with `Content-Range`.
- A range that cannot be satisfied returns `416` with `Content-Range: bytes */<size>`.
- Multiple ranges are not supported. The API ignores the header and returns `200` with the full file.
- Responses use `Cache-Control: private, no-store` and `X-Content-Type-Options: nosniff`.

## Errors

Errors return JSON in this shape:

```json
{"error": {"code": "invalid_request", "message": "unknown parameter 'x'; allowed: chat, text, sender, since, until, n, cursor"}}
```

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request` | Unknown or repeated parameter, bad `n`, bad `since` or `until`, malformed `cursor`, bad `recursive` |
| 400 | `ambiguous_chat` | The `chat` name matches more than one chat. The body lists `candidates` with `id` and `name`. |
| 401 | `unauthorized` | The token is missing, invalid, expired, or revoked. |
| 404 | `chat_not_found` | No chat matches `chat`. The body lists `chats`. |
| 404 | `not_found` | No such directory or file. |
| 405 | `method_not_allowed` | A method other than `GET` or `HEAD`, on any path including downloads. The response sends `Allow: GET, HEAD`. |
| 416 | `range_not_satisfiable` | The `Range` header cannot be satisfied |
| 429 | `rate_limited` | Over a limit. The response sends `Retry-After`. See the limits below. |
| 500 | `chat_log_failed` | The server could not read chat history. Retry later. |

## Limits

- Per token: 5 requests per second, with a burst of 20.
- 2 requests in flight at once.
- 1 MiB/s per file download.

When you hit a limit, the response is `429` with `Retry-After: 2` (seconds) from the edge. Wait that long and retry. Do not send requests in a tight loop.

## Recipes

Read the newest 50 messages from one chat:

```bash
curl -H "Authorization: Bearer rtw_..." "$ORIGIN/api/v1/chat_log?chat=Team&n=50"
```

Read the next older page, repeating the same filters:

```bash
curl -H "Authorization: Bearer rtw_..." "$ORIGIN/api/v1/chat_log?chat=Team&n=50&cursor=<next_cursor>"
```

List a folder tree:

```bash
curl -H "Authorization: Bearer rtw_..." "$ORIGIN/api/v1/files?path=inbox&recursive=1"
```

Download the first MiB of a file:

```bash
curl -H "Authorization: Bearer rtw_..." -H "Range: bytes=0-1048575" "$ORIGIN/api/v1/files/inbox/example.pdf" -o part.bin
```

## Related pages

- https://raintree.tech/developers — developer resources
- https://raintree.tech/developers.md — markdown alternate of the developer resources page
- https://raintree.tech/llms.txt — index for agents
