# Files (/docs/entwickler/files)

> Upload documents, check their processing state, search them, and read their extracted text for use in your prompts.

Upload documents, let PeakPrivacy extract their text, then list, search, retrieve and delete them. Files belong to the user account behind the token, and a token never sees another account's files.

| Method and path | Description |
|---|---|
| `POST /v1/files` | [Upload](#upload) one or more files |
| `GET /v1/files` | [List](#pagination) files |
| `POST /v1/files/search` | [Search](#search) files |
| `GET /v1/files/{id}` | [Retrieve](#retrieve) a file with its extracted text |
| `DELETE /v1/files/{id}` | [Delete](#retrieve) a file |

The paths start at `https://api.peakprivacy.ch`, without `/ai`. Use plain HTTP: the `files` methods of the OpenAI SDKs use a different format.

## Upload
`POST /v1/files` takes `multipart/form-data`.

**curl**

```bash
curl https://api.peakprivacy.ch/v1/files \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Accept: application/json" \
  -F 'files[]=@expense-policy.pdf' \
  -F 'meta[]={"file_name":"Expense policy 2026","department":"Accounting"}'
```

**Python**

```python
import json
import os
import requests

headers = {
    "Authorization": f"Bearer {os.environ['PEAKPRIVACY_API_KEY']}",
    "Accept": "application/json",
}
with open("expense-policy.pdf", "rb") as f:
    response = requests.post(
        "https://api.peakprivacy.ch/v1/files",
        headers=headers,
        files=[("files[]", ("expense-policy.pdf", f, "application/pdf"))],
        data={"meta[]": json.dumps({"file_name": "Expense policy 2026", "department": "Accounting"})},
    )
response.raise_for_status()
file_id = response.json()[0]["id"]
```

**JavaScript**

```js
// Node.js 18+, run as an ES module
import { readFile } from "node:fs/promises";

const form = new FormData();
form.append("files[]", new Blob([await readFile("expense-policy.pdf")]), "expense-policy.pdf");
form.append("meta[]", JSON.stringify({ file_name: "Expense policy 2026", department: "Accounting" }));

const response = await fetch("https://api.peakprivacy.ch/v1/files", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.PEAKPRIVACY_API_KEY}`,
    Accept: "application/json",
  },
  body: form,
});
const [file] = await response.json();
console.log(file.id);
```

| Name | Type | Required | Description |
|---|---|---|---|
| `files[]` | file | Yes | One part per file. Maximum 10,000 KB per file. |
| `meta[]` | string (JSON object) | No | One per file, in the same order as the files. `file_name` replaces the display name. `created_at` (ISO 8601) replaces the upload date used by date filters and sorting. Other keys are stored and can be searched with `meta_filters`. |

Supported extensions: bmp, csv, doc, docx, epub, gif, heic, htm, html, jp2, jpeg, jpg, log, md, ods, odt, pdf, png, pnm, ppt, pptx, rst, rtf, text, tiff, tsv, txt, webp, xls, xlsx, xml. The detected content type must match a supported format too.

An upload is all or nothing: if one file is rejected or a `meta[]` entry isn't valid JSON, no file is stored. The response is an array with one [file object](#file-object) per file.

## The file object
```json
{
  "id": "2e039d08-f01f-4f96-bd9a-ddaaf81be082",
  "name": "Expense policy 2026",
  "ext": "pdf",
  "size": "248392",
  "mimetype": "application/pdf",
  "is_extracted": true,
  "extraction_error": null,
  "is_vectorized": true,
  "is_summarized": true,
  "summary_error": null,
  "language": "en",
  "summary": "Rules for travel, meals and mileage …",
  "keywords": ["expenses", "receipts", "mileage"],
  "meta": { "file_name": "Expense policy 2026", "department": "Accounting" }
}
```

PeakPrivacy processes each file in the background. Poll [`GET /v1/files/{id}`](#retrieve) until the flags you need are `true`:

| Field | `true` means |
|---|---|
| `is_extracted` | The text is extracted. On failure, `extraction_error` says why. |
| `is_vectorized` | The file is indexed for retrieval. |
| `is_summarized` | `summary`, `keywords` and `language` are set. On failure, `summary_error` says why. |

Until then, `content` can be empty, and searches over `summary`, `keywords` or `content` can miss the file. Treat a file as ready when `is_extracted` is `true` and `is_summarized` is `true` or `summary_error` is set. Stop polling when `extraction_error` is set, because `is_extracted` then stays `false`. Set a timeout of several minutes as well. A complete polling loop is in [Answer questions from your documents](/docs/entwickler/documents).

Things to know when you parse a file object:

- `size` is a string in list, search and retrieve responses, and a number in the upload response. Convert it before you compare.
- `is_extracted` and `is_summarized` are `null` in the upload response, then `false` while processing, then `true`. Test for `true`.
- `meta` can be an empty array `[]` instead of an object when a file has no meta data.

## List
`GET /v1/files` returns your files in upload order, oldest first.

| Name | Type | Required | Description |
|---|---|---|---|
| `limit` | integer | No | Files per page, up to 100. Default 20. |
| `cursor` | string | No | `meta.next_cursor` or `meta.prev_cursor` from the previous page. |

List and search both return `data` (file objects) and `meta`:

```json
{
  "data": [],
  "meta": {
    "count": 20,
    "total_count": 57,
    "per_page": 20,
    "next_cursor": "eyJpZCI6...",
    "prev_cursor": null,
    "has_more_pages": true
  }
}
```

While `has_more_pages` is `true`, pass `meta.next_cursor` as the `cursor` query parameter. For search, send the same body again with `?cursor=…` in the URL. In Python, with `headers` from the upload example:

```python
params = {"limit": 100}
while True:
    page = requests.get("https://api.peakprivacy.ch/v1/files", headers=headers, params=params).json()
    for f in page["data"]:
        print(f["id"], f["name"])
    if not page["meta"]["has_more_pages"]:
        break
    params["cursor"] = page["meta"]["next_cursor"]
```

## Search
`POST /v1/files/search` searches by text, by metadata, or both. Send at least `query` or `meta_filters`.

```bash
curl https://api.peakprivacy.ch/v1/files/search \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"query": "receipt travel", "mode": "words", "meta_filters": {"department": "Accounting"}, "created_from": "2026-01-01"}'
```

| Name | Type | Required | Description |
|---|---|---|---|
| `query` | string | If no `meta_filters` | Text to search for. |
| `meta_filters` | object or array | If no `query` | An object matches files where all keys match. An array of objects matches files where any object matches. Values: string, number, boolean or `null`. Strings match case-insensitively. |
| `mode` | string | No | `phrase` (default) matches the whole query. `words` matches any word of 3 or more characters. |
| `fields` | array | No | Any of `name`, `summary`, `keywords`, `content`. Default: all four. |
| `ids` | array | No | File ids. Restricts the search to these files. |
| `language` | string | No | Language code such as `de`. A regional code such as `de-CH` counts as `de`. |
| `created_from`, `created_to` | string | No | Dates, best as `YYYY-MM-DD`. `created_to` can't be before `created_from`. |
| `sort` | string | No | `relevance`, `newest` or `oldest`. Default: `relevance` with a query, otherwise `newest`. |
| `limit` | integer | No | 1 to 100. Default 20. |

## Retrieve and delete
`GET /v1/files/{id}` returns the [file object](#file-object) plus `content`, the extracted text. `DELETE /v1/files/{id}` deletes the file and returns `204 No Content`.

```bash
curl https://api.peakprivacy.ch/v1/files/2e039d08-f01f-4f96-bd9a-ddaaf81be082 \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Accept: application/json"

curl -X DELETE https://api.peakprivacy.ch/v1/files/2e039d08-f01f-4f96-bd9a-ddaaf81be082 \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Accept: application/json"
```

## Use a file in a completion
The completions endpoints don't load uploaded files. Retrieve the extracted text and send it in `messages`:

```python
file = requests.get(f"https://api.peakprivacy.ch/v1/files/{file_id}", headers=headers).json()

completion = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": f"Document:\n{file['content']}\n\nWhich expenses need a receipt?"}],
)
```

`headers` and `file_id` come from the Python upload example, `client` from [Chat completions](/docs/entwickler/chat-completions#example). For the complete flow with search, prompt and sources, see [Answer questions from your documents](/docs/entwickler/documents). A document longer than the model's context window makes the request fail, see [The model rejects the request](/docs/entwickler/errors#model-rejected).

## Errors
- `400` for a rejected file (unsupported type, too large) or a search without `query` and `meta_filters`. The body lists the failing fields.
- `500` with `{"error": "Something Went Wrong!"}` if a `meta[]` part isn't valid JSON. Nothing is stored.
- `404` for a file id that doesn't exist or belongs to another account.
- A failed upload stores nothing, so you can send it again. See [Errors and limits](/docs/entwickler/errors).

## See also
- [Chat completions](/docs/entwickler/chat-completions)
- [Errors and limits](/docs/entwickler/errors#retry)
- [Authentication](/docs/entwickler/authentication)
