# File

Upload files (base64) and attach/detach them to records

::: info
Har bir javob umumiy konvertda qaytadi — `code`, `message`, `messages`, `status_code`, `time` — foydali yuk esa `data` ichida. Quyidagi jadvallar faqat `data` ichini tavsiflaydi.
:::

## POST /file/upload

Upload a file as base64. Stores it in object storage and returns the storage key (a string in `data`). Use that key as the file field (e.g. `path`, `image`) when creating/updating a record, or pass it to /file/attach. base64-only — no remote URL fetching (no SSRF).

**Kerakli ruxsat:** yo‘q — ruxsat e’lon qilgan ilova uchun yopiq · [ruxsatlar qanday ishlaydi](https://docs.bito.uz/api/permissions.md)

**So‘rov namunasi**

::: code-group

```bash [curl]
curl -X POST 'https://api.bito.uz/integration-api/integration/api/v2/file/upload' \
  -H "api-key: $BITO_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "filename": "receipt.pdf",
  "content_base64": "JVBERi0xLjQKJeLjz9MK..."
}'
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/file/upload', {
  method: 'POST',
  headers: { 'api-key': process.env.BITO_API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    "filename": "receipt.pdf",
    "content_base64": "JVBERi0xLjQKJeLjz9MK..."
  }),
})
const { code, data } = await res.json()
```

```python [Python]
import os
import requests

res = requests.request(
    "POST",
    "https://api.bito.uz/integration-api/integration/api/v2/file/upload",
    headers={"api-key": os.environ["BITO_API_KEY"]},
    json={
        "filename": "receipt.pdf",
        "content_base64": "JVBERi0xLjQKJeLjz9MK...",
    },
)
print(res.json())
```

```php [PHP]
<?php
$body = <<<'JSON'
{
  "filename": "receipt.pdf",
  "content_base64": "JVBERi0xLjQKJeLjz9MK..."
}
JSON;

$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/file/upload');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => ['api-key: ' . getenv('BITO_API_KEY'), 'Content-Type: application/json'],
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_RETURNTRANSFER => true,
]);
$response = json_decode(curl_exec($ch), true);
```

:::

**So‘rov tanasi**

| Maydon | Tur | Majburiy | Tavsif |
|---|---|---|---|
| `filename` | `string` | Ha | Original file name including extension. The extension is used to derive the storage key and (if mimetype is omitted) the content type. |
| `content_base64` | `string` | Ha | File bytes encoded as base64 (no data: URI prefix). Max ~20MB after decoding. base64-only by design — the API never fetches a remote URL, so there is no SSRF surface. |
| `mimetype` | `string` | Yo‘q | Optional MIME type. If omitted it is inferred from the filename extension, falling back to application/octet-stream. |

```json
{
  "filename": "receipt.pdf",
  "content_base64": "JVBERi0xLjQKJeLjz9MK...",
  "mimetype": "application/pdf"
}
```

**Javob**

```json
{
  "code": 0,
  "message": "Success",
  "status_code": 200,
  "data": "/uploads/665f.../integration/file-abc123.pdf",
  "time": "2025-05-01T12:00:00Z"
}
```

## POST /file/attach

Attach an already-uploaded file (its storage key) to an existing record. Body: `path` (the key) plus exactly one entity FK id (e.g. transaction_id, invoice_id). The referenced record must exist and the file must already be uploaded.

**Kerakli ruxsat:** yo‘q — ruxsat e’lon qilgan ilova uchun yopiq · [ruxsatlar qanday ishlaydi](https://docs.bito.uz/api/permissions.md)

**So‘rov namunasi**

::: code-group

```bash [curl]
curl -X POST 'https://api.bito.uz/integration-api/integration/api/v2/file/attach' \
  -H "api-key: $BITO_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "path": "/uploads/665f.../integration/file-abc123.pdf"
}'
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/file/attach', {
  method: 'POST',
  headers: { 'api-key': process.env.BITO_API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    "path": "/uploads/665f.../integration/file-abc123.pdf"
  }),
})
const { code, data } = await res.json()
```

```python [Python]
import os
import requests

res = requests.request(
    "POST",
    "https://api.bito.uz/integration-api/integration/api/v2/file/attach",
    headers={"api-key": os.environ["BITO_API_KEY"]},
    json={
        "path": "/uploads/665f.../integration/file-abc123.pdf",
    },
)
print(res.json())
```

```php [PHP]
<?php
$body = <<<'JSON'
{
  "path": "/uploads/665f.../integration/file-abc123.pdf"
}
JSON;

$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/file/attach');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => ['api-key: ' . getenv('BITO_API_KEY'), 'Content-Type: application/json'],
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_RETURNTRANSFER => true,
]);
$response = json_decode(curl_exec($ch), true);
```

:::

**So‘rov tanasi**

| Maydon | Tur | Majburiy | Tavsif |
|---|---|---|---|
| `path` | `string` | Ha | Storage key returned by /file/upload. |
| `type` | `string` | Yo‘q | Optional. Inferred from the file extension when omitted.. Mumkin qiymatlar: `image`, `video`, `audio`, `file` |
| `name` | `string` | Yo‘q | Optional display name. |
| `note` | `string` | Yo‘q | Optional note. |
| `transaction_id` | `string` | Yo‘q | Attach to a finance transaction. |
| `invoice_id` | `string` | Yo‘q | Attach to an invoice. |
| `task_id` | `string` | Yo‘q | Attach to a task. |
| `purchase_id` | `string` | Yo‘q | Attach to a purchase. |
| `supplier_id` | `string` | Yo‘q | Attach to a supplier. |
| `trade_id` | `string` | Yo‘q |  |
| `customer_id` | `string` | Yo‘q |  |
| `sale_order_id` | `string` | Yo‘q |  |
| `discount_id` | `string` | Yo‘q |  |
| `expected_product_id` | `string` | Yo‘q |  |
| `income_id` | `string` | Yo‘q |  |
| `revision_id` | `string` | Yo‘q |  |
| `write_off_id` | `string` | Yo‘q |  |
| `purchase_request_id` | `string` | Yo‘q |  |
| `purchase_order_id` | `string` | Yo‘q |  |
| `purchase_return_id` | `string` | Yo‘q |  |
| `production_route_sheet_id` | `string` | Yo‘q |  |
| `production_operation_id` | `string` | Yo‘q |  |
| `production_step_id` | `string` | Yo‘q |  |
| `production_tech_map_id` | `string` | Yo‘q |  |
| `production_task_id` | `string` | Yo‘q |  |
| `person_id` | `string` | Yo‘q |  |
| `employee_id` | `string` | Yo‘q |  |
| `kpi_id` | `string` | Yo‘q |  |
| `product_id` | `string` | Yo‘q |  |
| `transfer_request_id` | `string` | Yo‘q |  |
| `internal_transfer_id` | `string` | Yo‘q |  |
| `organizational_transfer_id` | `string` | Yo‘q |  |

```json
{
  "path": "/uploads/665f.../integration/file-abc123.pdf",
  "type": "image",
  "name": "string",
  "note": "string",
  "transaction_id": "string",
  "invoice_id": "string",
  "task_id": "string",
  "purchase_id": "string",
  "supplier_id": "string",
  "trade_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "customer_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "sale_order_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "discount_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "expected_product_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "income_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "revision_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "write_off_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "purchase_request_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "purchase_order_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "purchase_return_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "production_route_sheet_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "production_operation_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "production_step_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "production_tech_map_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "production_task_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "person_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "employee_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "kpi_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "product_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "transfer_request_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "internal_transfer_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "organizational_transfer_id": "64b7f4d9e2b3b6a0b7c5d7ff"
}
```

**Javob**

| Maydon | Tur | Tavsif |
|---|---|---|
| `_id` | `string` |  |
| `path` | `string` |  |
| `type` | `string` |  |
| `size` | `number` |  |

```json
{
  "code": 0,
  "message": "Success",
  "status_code": 200,
  "data": {
    "_id": "string",
    "path": "string",
    "type": "string",
    "size": 0
  },
  "time": "2025-05-01T12:00:00Z"
}
```

## DELETE /file/attach/{id}

Soft-delete an attachment by its ObjectId (detach a file from a record). Returns the deleted attachment id.

**Kerakli ruxsat:** yo‘q — ruxsat e’lon qilgan ilova uchun yopiq · [ruxsatlar qanday ishlaydi](https://docs.bito.uz/api/permissions.md)

**So‘rov namunasi**

::: code-group

```bash [curl]
curl -X DELETE 'https://api.bito.uz/integration-api/integration/api/v2/file/attach/6712f0a9c4d1e87b3a0f9911' \
  -H "api-key: $BITO_API_KEY"
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/file/attach/6712f0a9c4d1e87b3a0f9911', {
  method: 'DELETE',
  headers: { 'api-key': process.env.BITO_API_KEY },
})
const { code, data } = await res.json()
```

```python [Python]
import os
import requests

res = requests.request(
    "DELETE",
    "https://api.bito.uz/integration-api/integration/api/v2/file/attach/6712f0a9c4d1e87b3a0f9911",
    headers={"api-key": os.environ["BITO_API_KEY"]},
)
print(res.json())
```

```php [PHP]
<?php
$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/file/attach/6712f0a9c4d1e87b3a0f9911');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    CURLOPT_HTTPHEADER => ['api-key: ' . getenv('BITO_API_KEY')],
    CURLOPT_RETURNTRANSFER => true,
]);
$response = json_decode(curl_exec($ch), true);
```

:::

**Yo‘l parametrlari**

| Parametr | Tur | Majburiy | Tavsif |
|---|---|---|---|
| `id` | `string` | Ha | Attachment ObjectId to detach |

**So‘rov tanasi**

So‘rov tanasi talab qilinmaydi.

**Javob**

```json
{
  "code": 0,
  "message": "Success",
  "status_code": 200,
  "data": "string",
  "time": "2025-05-01T12:00:00Z"
}
```

---

*Bu sahifa OpenAPI spetsifikatsiyasidan yaratilgan.*
