# Export records

Asynchronous export jobs and their status

::: info
Every response arrives in the shared envelope — `code`, `message`, `messages`, `status_code`, `time` — with the payload in `data`. The tables below describe what is inside `data`.
:::

## POST /export-records/create

Initiate an async export job for the specified page. The export runs in the background via BullMQ; poll get-by-id or get-paging to track progress. Returns the created record with state=pending.

**Required permission:** `export_records` · [how permissions work](https://docs.bito.uz/en/api/permissions.md)

**Example request**

::: code-group

```bash [curl]
curl -X POST 'https://api.bito.uz/integration-api/integration/api/v2/export-records/create' \
  -H "api-key: $BITO_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "page_name": "string",
  "query_params": {}
}'
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/export-records/create', {
  method: 'POST',
  headers: { 'api-key': process.env.BITO_API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    "page_name": "string",
    "query_params": {}
  }),
})
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/export-records/create",
    headers={"api-key": os.environ["BITO_API_KEY"]},
    json={
        "page_name": "string",
        "query_params": {},
    },
)
print(res.json())
```

```php [PHP]
<?php
$body = <<<'JSON'
{
  "page_name": "string",
  "query_params": {}
}
JSON;

$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/export-records/create');
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);
```

:::

**Request body**

| Field | Type | Required | Description |
|---|---|---|---|
| `page_name` | `string` | Yes | Bito page identifier to export (e.g. trade, products, purchase, customers). Must match a registered PageNames value. |
| `query_params` | `object` | Yes | Page-specific query parameters (filters, date range, etc.). Passed through to the per-page export cursor function after RBAC validation. Include ext_type ('xlsx' or 'csv') to control output format; defaults to xlsx. |
| `ext_type` | `string` | No | One of: `xlsx`, `csv`, `pdf`. Default: `"xlsx"` |

```json
{
  "page_name": "string",
  "query_params": {},
  "ext_type": "xlsx"
}
```

**Response**

| Field | Type | Description |
|---|---|---|
| `_id` | `string` | Export record ObjectId |
| `account_id` | `string` | Tenant identifier (equals boss ObjectId / dbName) |
| `user_id` | `string` | ObjectId of the user who initiated the export |
| `file_name` | `string` | Generated file name (e.g. trade-1718000000000.xlsx) |
| `file_path` | `string` | S3 / storage path to the exported file (available when state=completed) |
| `page_name` | `string` | Bito page identifier the export was requested for (e.g. trade, products, purchase) |
| `state` | `string` | Current state of the export job. Worker-driven: pending→in_progress→completed\|failed. Caller-cancellable: pending\|in_progress→canceled.. One of: `pending`, `in_progress`, `completed`, `failed`, `canceled` |
| `started_at` | `string (date-time)` | Timestamp when the export record was created |
| `completed_at` | `string (date-time)` | Timestamp when the export reached a terminal state (completed, failed, or canceled) |
| `progress` | `number` | Export completion percentage (0–100). Set to 100 on completed. |
| `query_params` | `object` | The validated query parameters used for this export (page-specific shape) |
| `user` | `object` | Denormalized user data (available in get-paging list) |
| `user.full_name` | `string` |  |
| `user.phone_number` | `string` |  |

```json
{
  "code": 0,
  "message": "Success",
  "status_code": 200,
  "data": {
    "_id": "string",
    "account_id": "string",
    "user_id": "string",
    "file_name": "string",
    "file_path": "string",
    "page_name": "string",
    "state": "pending",
    "started_at": "2025-05-01T12:00:00Z",
    "completed_at": "2025-05-01T12:00:00Z",
    "progress": 0,
    "query_params": {},
    "user": {
      "full_name": "string",
      "phone_number": "string"
    }
  },
  "time": "2025-05-01T12:00:00Z"
}
```

## GET /export-records/get-by-id/{id}

Fetch a single export record by ObjectId. Use this to poll state/progress after create.

**Required permission:** `export_records` · [how permissions work](https://docs.bito.uz/en/api/permissions.md)

**Example request**

::: code-group

```bash [curl]
curl -X GET 'https://api.bito.uz/integration-api/integration/api/v2/export-records/get-by-id/6712f0a9c4d1e87b3a0f9911' \
  -H "api-key: $BITO_API_KEY"
```

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

```python [Python]
import os
import requests

res = requests.request(
    "GET",
    "https://api.bito.uz/integration-api/integration/api/v2/export-records/get-by-id/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/export-records/get-by-id/6712f0a9c4d1e87b3a0f9911');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => ['api-key: ' . getenv('BITO_API_KEY')],
    CURLOPT_RETURNTRANSFER => true,
]);
$response = json_decode(curl_exec($ch), true);
```

:::

**Path parameters**

| Parameter | Type | Required | Description |
|---|---|---|---|
| `id` | `string` | Yes | Export record ObjectId |

**Request body**

No request body.

**Response**

Same response shape as [POST /export-records/create](#post-export-records-create).

## POST /export-records/get-paging

Paginated list of export records for the authenticated tenant. Filterable by user_id, page_name, state, date range, and employee list. Includes $lookup user {full_name, phone_number}.

**Required permission:** `export_records` · [how permissions work](https://docs.bito.uz/en/api/permissions.md)

**Example request**

::: code-group

```bash [curl]
curl -X POST 'https://api.bito.uz/integration-api/integration/api/v2/export-records/get-paging' \
  -H "api-key: $BITO_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "page": 1,
  "limit": 20
}'
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/export-records/get-paging', {
  method: 'POST',
  headers: { 'api-key': process.env.BITO_API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    "page": 1,
    "limit": 20
  }),
})
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/export-records/get-paging",
    headers={"api-key": os.environ["BITO_API_KEY"]},
    json={
        "page": 1,
        "limit": 20,
    },
)
print(res.json())
```

```php [PHP]
<?php
$body = <<<'JSON'
{
  "page": 1,
  "limit": 20
}
JSON;

$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/export-records/get-paging');
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);
```

:::

**Request body**

| Field | Type | Required | Description |
|---|---|---|---|
| `page` | `number` | Yes | Default: `1` |
| `limit` | `number` | Yes | Default: `20` |
| `user_id` | `string` | No | Filter by initiating user ObjectId |
| `page_name` | `string` | No | Filter by page name (e.g. trade, products) |
| `state` | `string` | No | Filter by export state. One of: `pending`, `in_progress`, `completed`, `failed`, `canceled` |
| `date_from` | `string (date)` | No | Filter records created on or after this date (ISO 8601) |
| `date_to` | `string (date)` | No | Filter records created on or before this date (ISO 8601) |
| `employees` | `string[]` | No | Filter by a list of user ObjectIds (alternative to user_id for multi-user filtering) |
| `organization_id` | `string` | No | Organization ObjectId. |
| `sort_by` | `string` | No | Field name to sort by. |
| `asc` | `integer` | No | Sort direction: `1` ascending, `-1` descending.. One of: `-1`, `1` |
| `ext_type` | `string` | No | One of: `xlsx`, `csv`, `pdf`. Default: `"xlsx"` |

```json
{
  "page": 1,
  "limit": 20,
  "user_id": "string",
  "page_name": "string",
  "state": "pending",
  "date_from": "string",
  "date_to": "string",
  "employees": [
    "string"
  ],
  "organization_id": "64b7f4d9e2b3b6a0b7c5d7ff",
  "sort_by": "string",
  "asc": -1,
  "ext_type": "xlsx"
}
```

**Response**

| Field | Type | Description |
|---|---|---|
| `total` | `number` |  |
| `list` | `object[]` |  |
| `list[]._id` | `string` | Export record ObjectId |
| `list[].account_id` | `string` | Tenant identifier (equals boss ObjectId / dbName) |
| `list[].user_id` | `string` | ObjectId of the user who initiated the export |
| `list[].file_name` | `string` | Generated file name (e.g. trade-1718000000000.xlsx) |
| `list[].file_path` | `string` | S3 / storage path to the exported file (available when state=completed) |
| `list[].page_name` | `string` | Bito page identifier the export was requested for (e.g. trade, products, purchase) |
| `list[].state` | `string` | Current state of the export job. Worker-driven: pending→in_progress→completed\|failed. Caller-cancellable: pending\|in_progress→canceled.. One of: `pending`, `in_progress`, `completed`, `failed`, `canceled` |
| `list[].started_at` | `string (date-time)` | Timestamp when the export record was created |
| `list[].completed_at` | `string (date-time)` | Timestamp when the export reached a terminal state (completed, failed, or canceled) |
| `list[].progress` | `number` | Export completion percentage (0–100). Set to 100 on completed. |
| `list[].query_params` | `object` | The validated query parameters used for this export (page-specific shape) |
| `list[].user` | `object` | Denormalized user data (available in get-paging list) |
| `list[].user.full_name` | `string` |  |
| `list[].user.phone_number` | `string` |  |

```json
{
  "code": 0,
  "message": "Success",
  "status_code": 200,
  "data": {
    "total": 0,
    "list": [
      {
        "_id": "string",
        "account_id": "string",
        "user_id": "string",
        "file_name": "string",
        "file_path": "string",
        "page_name": "string",
        "state": "pending",
        "started_at": "2025-05-01T12:00:00Z",
        "completed_at": "2025-05-01T12:00:00Z",
        "progress": 0,
        "query_params": {},
        "user": {
          "full_name": null,
          "phone_number": null
        }
      }
    ]
  },
  "time": "2025-05-01T12:00:00Z"
}
```

## PUT /export-records/set-state/{id}

Cancel a pending or in-progress export job. This is the only caller-driven state transition — only state=canceled is accepted. Worker-driven transitions (pending→in_progress→completed/failed) cannot be triggered via this endpoint.

**Required permission:** `export_records` · [how permissions work](https://docs.bito.uz/en/api/permissions.md)

**Example request**

::: code-group

```bash [curl]
curl -X PUT 'https://api.bito.uz/integration-api/integration/api/v2/export-records/set-state/6712f0a9c4d1e87b3a0f9911' \
  -H "api-key: $BITO_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "state": "canceled"
}'
```

```js [JavaScript]
const res = await fetch('https://api.bito.uz/integration-api/integration/api/v2/export-records/set-state/6712f0a9c4d1e87b3a0f9911', {
  method: 'PUT',
  headers: { 'api-key': process.env.BITO_API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    "state": "canceled"
  }),
})
const { code, data } = await res.json()
```

```python [Python]
import os
import requests

res = requests.request(
    "PUT",
    "https://api.bito.uz/integration-api/integration/api/v2/export-records/set-state/6712f0a9c4d1e87b3a0f9911",
    headers={"api-key": os.environ["BITO_API_KEY"]},
    json={
        "state": "canceled",
    },
)
print(res.json())
```

```php [PHP]
<?php
$body = <<<'JSON'
{
  "state": "canceled"
}
JSON;

$ch = curl_init('https://api.bito.uz/integration-api/integration/api/v2/export-records/set-state/6712f0a9c4d1e87b3a0f9911');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'PUT',
    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);
```

:::

**Path parameters**

| Parameter | Type | Required | Description |
|---|---|---|---|
| `id` | `string` | Yes | Export record ObjectId |

**Request body**

| Field | Type | Required | Description |
|---|---|---|---|
| `state` | `string` | Yes | Target state. Only 'canceled' is accepted via this endpoint. The record must currently be in 'pending' or 'in_progress' state; otherwise a WrongState error is returned.. One of: `canceled` |

```json
{
  "state": "canceled"
}
```

**Response**

Same response shape as [POST /export-records/create](#post-export-records-create).

---

*This page is generated from the OpenAPI spec.*
