# Files and images

> Upload a file once and use it in messages, or send a file or an image inside the request.

## What it is for

Some questions need a document or a picture. Upload a file to `/v1/files`, then refer to it by id in any message. A file or an image can also go inside the message itself, read for that request only and not stored.

## Upload and use a file

```bash
curl https://antarya.ai/v1/files \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -F file=@agreement.pdf

curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {
        "role": "user",
        "content": [
          {"type": "file", "file_id": "file_01"},
          {"type": "text", "text": "Summarise the termination clause in this agreement."}
        ]
      }
    ]
  }'
```

```python
from antarya import Antarya

client = Antarya()

file = client.files.create(file=open("agreement.pdf", "rb"))

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "file", "file_id": file.id},
            {"type": "text", "text": "Summarise the termination clause in this agreement."},
        ],
    }],
)
print(reply.content[0].text)
```

```typescript
import fs from "node:fs";
import Antarya from "antarya";

const client = new Antarya();

const file = await client.files.create({ file: fs.createReadStream("agreement.pdf") });

const reply = await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "file", file_id: file.id },
      { type: "text", text: "Summarise the termination clause in this agreement." },
    ],
  }],
});
console.log(reply.content[0].text);
```

```bash
antarya --file agreement.pdf "Summarise the termination clause in this agreement."
```

Output:

```
{
  "id": "req_07",
  "content": [
    {"type": "text", "text": "Either party can end the agreement with 30 days of written notice..."}
  ],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 1204, "output_tokens": 61}
}
```

## Send a file without storing it

A file part can also hold the file itself as base64 text, with its name and media type. It is read for that request only and is not stored, so there is nothing to delete afterwards. The same types and the 10 MB limit apply.

```python
import base64

with open("agreement.pdf", "rb") as f:
    data = base64.b64encode(f.read()).decode()

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "file", "source": {"type": "base64", "filename": "agreement.pdf", "media_type": "application/pdf", "data": data}},
            {"type": "text", "text": "Summarise the termination clause in this agreement."},
        ],
    }],
)
```

```typescript
import fs from "node:fs";

const data = fs.readFileSync("agreement.pdf").toString("base64");

const reply = await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "file", source: { type: "base64", filename: "agreement.pdf", media_type: "application/pdf", data } },
      { type: "text", text: "Summarise the termination clause in this agreement." },
    ],
  }],
});
```

## Send an image inline

An image part holds the picture as base64 text, with its media type.

```python
import base64

with open("signature-page.png", "rb") as f:
    data = base64.b64encode(f.read()).decode()

reply = client.chat.create(
    messages=[{
        "role": "user",
        "content": [
            {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": data}},
            {"type": "text", "text": "Who signed this page?"},
        ],
    }],
)
```

```typescript
import fs from "node:fs";

const data = fs.readFileSync("signature-page.png").toString("base64");

const reply = await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "image", source: { type: "base64", media_type: "image/png", data } },
      { type: "text", text: "Who signed this page?" },
    ],
  }],
});
```

## Manage files

`GET /v1/files` lists your files, `GET /v1/files/{id}` describes one and `DELETE /v1/files/{id}` removes it. Delete a file when you no longer need it.

## Parameters

- `file` (file, required): The file to upload, sent as multipart form data to POST /v1/files.
- `messages.content` (array, optional): A list of parts, to put a file, image or text in one message.
  - `messages.content.type` (text, required): "text", "image" or "file".
  - `messages.content.file_id` (text, optional): The id returned by an upload. Used with the "file" part.
  - `messages.content.source` (object, optional): Data sent inside the message, with type "base64", a media_type and the data. Used with the "image" part, and with the "file" part in place of file_id, where it also needs a filename.

## Limits

A file can be up to 10 MB and an account can store up to 100 MB during early access. A file over either limit gets a 413.

An image over 10 MB gets a 413. More than 20 images in a request, or an image that is not PNG, JPEG or WebP, gets a 400 `invalid_request`.

| Limit | Value |
|---|---|
| File size | 10 MB a file |
| File types | txt, md, csv, json, pdf, docx, doc, xlsx |
| Image types | PNG, JPEG and WebP |
| Image size | 10 MB each, up to 20 images in a request |
| Stored per account | 100 MB in total |

## Next steps

- [Counting tokens](https://antarya.ai/docs/guides/counting-tokens.md): See what a file adds to a request before you send it.
- [Batch](https://antarya.ai/docs/guides/batch.md): Run the same question over many files.
- [CLI](https://antarya.ai/docs/cli.md): Attach files from your terminal.
