# Files

> Send a file B7R reads back: ask for one signed address, PUT the file to it, then name the upload in the call that uses it.

Source: https://docs.b7r.app/files

A request body is JSON of at most 256 KB, so a file never travels in it. Three steps instead: ask B7R for an upload with `POST /v1/uploads`, `PUT` the file to the address it answers, then send the upload's `upload_id` in the call that uses the file.

- The address is on B7R's own storage in Saudi Arabia. It takes one `PUT` of the whole file, with the `content-type` header the answer gives; a second `PUT` to it is refused.
- B7R reads the file back and decides its type from its bytes and its size from its length. A file whose bytes are not the type you declared, or whose length is not `size_bytes`, is refused by the call that uses it.
- Two deadlines. Start the `PUT` before `upload.expires_at`, when the address expires: it can be about a minute, so send the file right after you have the answer (a late `PUT` is refused; ask again). Use the upload by `expires_at`, an hour after you asked. An upload is used once, and deleted three hours after you asked whether it was used or not.
- The answer is never stored: a lost answer cannot be read again. Ask again; the first upload is never used and is deleted with the rest.
- An upload is used up by the call that saves it (a product create or change). A call refused or cut off before it saves leaves the upload usable, so a retry can name it again until `expires_at`.
- Each upload counts against your uploads an hour, `uploads_per_hour` in [`GET /v1/me?include=limits`](https://docs.b7r.app/keys-and-access#read-your-account-and-key). Past it the answer is `429 rate_limited` with `Retry-After`. When B7R holds too many uploads from every account together, the answer is `503 service_unavailable` with `Retry-After`: try again then.

## Ask for an upload

`POST /v1/uploads`

Any key may call it, but each `purpose` needs its own area of the key (the table below): without it the answer is `403 area_forbidden` naming the area, before anything is made. A purpose is taken from the day its feature is live; before that it answers `400 invalid_request` with `invalid_value` on `purpose`.

The type and size must be ones the purpose takes, or the answer is `400 invalid_request` naming `content_type` (with the types it takes in `valid_values`) or `size_bytes`.

Key: Any valid key. Send `Idempotency-Key` and `Content-Type: application/json`.

Body:

- `purpose` (string, required): What the file is for. Each purpose needs its own area of the key, and is taken only once its release is live. One of `design`, `design_original`, `my_store_image`, `ticket_image`, `avatar`, `brand_logo`.
- `content_type` (string, required): What the file is. B7R reads the file back and decides its type from its bytes; a file of another type is refused when it is used. One of `image/png`, `image/jpeg`, `image/webp`, `application/pdf`.
- `size_bytes` (integer, required): The file's exact size in bytes. A file of another size is refused when it is used. At most 26214400.

Answer:

- `upload_id` (string (uuid), required): Name it in the call that uses the file.
- `upload` (object, required)
  - `method` (constant, required): Always `"PUT"`.
  - `url` (string (uri), required): Send the file here once, as the whole request body. The address works for one PUT and is never shown again.
  - `headers` (object, required)
    - `content-type` (string, required): The content type you declared. One of `image/png`, `image/jpeg`, `image/webp`, `application/pdf`.
  - `expires_at` (string, required): Start the PUT before this time: the address expires then. It is short (about a minute): PUT the file right after you have the answer.
- `expires_at` (string, required): Use the upload by this time, an hour after you asked: name its `upload_id` in the call that uses the file before then. After it the upload is never used and is deleted.

Errors: `unauthorized` (401), `area_forbidden` (403), `account_inactive` (403), `connection_disabled` (403), `https_required` (403), `invalid_request` (400), `idempotency_key_required` (400), `request_too_large` (413), `unsupported_media_type` (415), `rate_limited` (429), `internal_error` (500), `upstream_unavailable` (502), `api_unavailable` (503), `request_expired` (503), `service_unavailable` (503), `upstream_timeout` (504). See [Errors](https://docs.b7r.app/errors).

Request:

```bash
curl -X POST https://api.b7r.app/v1/uploads \
  -H "Authorization: Bearer $B7R_API_KEY" \
  -H "Idempotency-Key: $(uuidgen 2>/dev/null || node -e 'console.log(crypto.randomUUID())')" \
  -H "Content-Type: application/json" \
  -d '{
    "purpose": "design",
    "content_type": "image/png",
    "size_bytes": 1843200
  }'
```

```javascript
const res = await fetch("https://api.b7r.app/v1/uploads", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.B7R_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "purpose": "design",
    "content_type": "image/png",
    "size_bytes": 1843200
  }),
});
const data = await res.json();
```

```python
import os, requests, uuid

res = requests.post(
    "https://api.b7r.app/v1/uploads",
    headers={
        "Authorization": f"Bearer {os.environ['B7R_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "purpose": "design",
        "content_type": "image/png",
        "size_bytes": 1843200
    },
    timeout=30,
)
data = res.json()
```

Answer (201 Created):

```json
{
  "upload_id": "6f1c2a9e-4b7d-4c3a-9e2f-0a1b2c3d4e5f",
  "upload": {
    "method": "PUT",
    "url": "https://data.b7r.app/storage/v1/object/upload/sign/custom-api-uploads/2f9a6e1c-0d4b-4e8a-9c7f-1b2a3c4d5e6f/6f1c2a9e-4b7d-4c3a-9e2f-0a1b2c3d4e5f?token=sample",
    "headers": {
      "content-type": "image/png"
    },
    "expires_at": "2026-12-01T08:01:00.000Z"
  },
  "expires_at": "2026-12-01T09:00:00.000Z"
}
```

## Send the file

PUT the file to the address:

```javascript
import { readFile } from "node:fs/promises";

const file = await readFile("design.png");
const response = await fetch(upload.upload.url, {
  method: "PUT",
  headers: upload.upload.headers,
  body: file,
});
if (!response.ok) throw new Error(`upload failed: ${response.status}`);
```

```bash
curl -sS -X PUT "$UPLOAD_URL" \
  -H "content-type: image/png" \
  --data-binary @design.png
```

## Purposes

| Purpose | Area it needs | File types | Largest file |
| --- | --- | --- | --- |
| `design` | `products:write` | `image/png`, `image/jpeg`, `image/webp` | 25 MB |
| `design_original` | `products:write` | `application/pdf` | 25 MB |
| `my_store_image` | `my_store:write` | `image/png`, `image/jpeg`, `image/webp` | 5 MB |
| `ticket_image` | `tickets:write` | `image/png`, `image/jpeg`, `image/webp` | 5 MB |
| `avatar` | `settings:write` | `image/png`, `image/jpeg`, `image/webp` | 2 MB |
| `brand_logo` | `settings:write` | `image/png`, `image/jpeg`, `image/webp` | 2 MB |
