> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mileapp.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Import data source from file

> This endpoint allows users to upload a file (CSV, XLS, or XLSX) containing data source records for import. The file is validated and processed in the background via queue. The import process supports multiple import types: datasource, task, visit, and route. After upload, the import is queued for processing and can be monitored via the import-monitoring endpoints.

**Background Processing:**
- Queue Name: `readingImportDatasource`
- Processing is chunked for large files
- Status updates are tracked in real-time

**File Requirements:**
- Max Size: 20MB
- Supported Formats: CSV, XLS, XLSX
- File structure must match the data type schema

**Important Notes:**
- For datasource import: `dataTypeId` is required
- For datasource with hub-specific data: `hubId` is required (depends on `commonData` setting)
- Failed records are exported to a separate file for review
- Template file is generated based on data type schema



## OpenAPI

````yaml /openapi/public/openapi-importexport.json post /data/import
openapi: 3.0.0
info:
  title: MileApp API - ImportExport
  version: 3.0.0
  description: MileApp API Documentation - RESTful API for field operations management.
servers:
  - url: https://apiweb.mile.app/api/v3
security:
  - bearerAuth: []
tags:
  - name: Data Import
    description: >-
      The Data Import module allows users to upload and import data source
      records from Excel or CSV files. Import operations are processed in the
      background via queue system for scalability and can handle large datasets.
      The import process validates data against data type schemas, tracks
      processing status in real-time, and generates detailed reports including
      failed records for review. All import activities can be monitored through
      import-monitoring endpoints with 30-day data retention.
  - name: Export Task
    description: >-
      The Export Task module provides functionality to export task data and data
      source records to various formats (XLSX, JSON). It supports both direct
      export for small datasets and background processing for large exports.
      Users can customize exports using export configurations, filter data by
      multiple criteria, and monitor export operations through export logs.
      Export files are stored securely with 30-day retention period.
  - name: Export Config
    description: >-
      The Export Config module is used to save and manage the specific fields
      that a user wants included in export results. This allows users to
      customize their exported data by selecting only the necessary fields for
      each export, ensuring that the output contains exactly the information
      they need.


      **To see the details of export config object, please follow [this
      link](#tag/export_config_model)**
  - name: Import Config
    description: >-
      The Import Config module saves and manages column mapping presets for bulk
      import features across MileApp. Users can store field-to-Excel-column
      mappings so they can be reused across multiple imports. Each config is
      linked to a specific resource and contains the mapping between its fields
      and Excel header columns.
paths:
  /data/import:
    post:
      tags:
        - Data Import
      summary: Import data source from file
      description: >-
        This endpoint allows users to upload a file (CSV, XLS, or XLSX)
        containing data source records for import. The file is validated and
        processed in the background via queue. The import process supports
        multiple import types: datasource, task, visit, and route. After upload,
        the import is queued for processing and can be monitored via the
        import-monitoring endpoints.


        **Background Processing:**

        - Queue Name: `readingImportDatasource`

        - Processing is chunked for large files

        - Status updates are tracked in real-time


        **File Requirements:**

        - Max Size: 20MB

        - Supported Formats: CSV, XLS, XLSX

        - File structure must match the data type schema


        **Important Notes:**

        - For datasource import: `dataTypeId` is required

        - For datasource with hub-specific data: `hubId` is required (depends on
        `commonData` setting)

        - Failed records are exported to a separate file for review

        - Template file is generated based on data type schema
      operationId: ImportData
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  description: >-
                    The file containing data to import. Supported formats: CSV,
                    XLS, XLSX. Maximum file size: 20MB.

                    **Example:** `datasource_import.xlsx`
                  type: string
                  format: binary
                dataTypeId:
                  description: >-
                    The identifier for the data type. Required for datasource
                    import type. Use [GET /data-types
                    API](#tag/Data-Type/operation/getDataType) to get the list
                    of Data Type IDs.

                    **Example:** `63db2fb476bc679c540369c4`
                  type: string
                hubId:
                  description: >-
                    The identifier for the hub. Required if the data type has
                    `commonData` set to false (hub-specific data). Use [GET
                    /hubs API](#tag/Hub/operation/getHubs) to get the list of
                    Hub IDs.

                    **Example:** `63c63ab1b6972d3150348172`
                  type: string
              required:
                - file
                - dataTypeId
        required: true
      responses:
        '200':
          description: Success - File uploaded and import process queued
          content:
            application/json:
              schema:
                type: object
                title: ImportDataResponse
                properties:
                  status:
                    type: boolean
                    description: Status of response.
                  message:
                    description: Message of API response.
                    type: string
                  data:
                    description: Import monitoring record created for this import.
                    type: object
                    properties:
                      _id:
                        type: string
                        description: Import monitoring ID
                      organizationId:
                        type: string
                        description: Organization ID
                      inputType:
                        type: string
                        description: >-
                          The input type of the component (e.g., string, number,
                          date, etc.)
                      hubId:
                        type: string
                        description: Hub ID (if applicable)
                      filename:
                        type: string
                        description: Original uploaded filename
                      totalRows:
                        type: integer
                        description: Total rows in the file
                      totalValid:
                        type: integer
                        description: >-
                          Total valid rows (initially 0, updated during
                          processing)
                      totalInvalid:
                        type: integer
                        description: >-
                          Total invalid rows (initially 0, updated during
                          processing)
                      status:
                        type: string
                        description: Import status
                        enum:
                          - onprocess
                          - success
                          - partially
                          - failed
                      dataTypeId:
                        type: string
                        description: Data type ID (for datasource import)
                      dataTypeName:
                        type: string
                        description: Data type name (for datasource import)
                      fileTemplate:
                        type: string
                        description: URL to download template file
                      createdBy:
                        type: string
                        description: Email of user who initiated the import
                      createdTime:
                        type: string
                        description: Timestamp when import was created
                      updatedTime:
                        type: string
                        description: Timestamp when import was last updated
                example:
                  status: true
                  message: Import process has been queued successfully
                  data:
                    _id: 65f8a4c2d9e3b1234567890a
                    organizationId: 63c61d865347e356d05e3052
                    type: string
                    hubId: 63c63ab1b6972d3150348172
                    filename: outlets_import.xlsx
                    totalRows: 150
                    totalValid: 0
                    totalInvalid: 0
                    status: onprocess
                    dataTypeId: 63db2fb476bc679c540369c4
                    dataTypeName: OutletDetail
                    fileTemplate: >-
                      https://staticdev.mile.app/dev-mile/templates/template_OutletDetail.xlsx
                    totalChunks: 3
                    processedChunks: 0
                    createdBy: john.doe@mile.app
                    createdTime: '2024-03-18 10:30:45'
                    updatedTime: '2024-03-18 10:30:45'
        '400':
          description: Bad Request - Validation error
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                  failedCode:
                    type: string
              examples:
                response:
                  value:
                    status: false
                    message: The file field is required.
                    failedCode: import-001
        '401':
          description: Unauthorized - Invalid or missing authentication token
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                  failedCode:
                    type: string
              examples:
                response:
                  value:
                    status: false
                    message: Unauthorized
                    failedCode: auth-001
        '403':
          description: Forbidden - User does not have permission to import data
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                  failedCode:
                    type: string
              examples:
                response:
                  value:
                    status: false
                    message: You do not have permission to perform this action
                    failedCode: auth-003
        '422':
          description: Unprocessable Entity - Invalid file format or data type not found
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                  failedCode:
                    type: string
              examples:
                response:
                  value:
                    status: false
                    message: 'The file must be a file of type: csv, xls, xlsx.'
                    failedCode: import-002
        '500':
          description: Server error
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                  failedCode:
                    type: string
              examples:
                response:
                  value:
                    status: false
                    message: Internal server error
                    failedCode: import-045
      deprecated: false
      security:
        - bearerAuth: []
      x-code-samples:
        - lang: cURL
          source: |-
            curl -X POST "https://apiweb.mile.app/api/v3/data/import" \
              -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
              -F "file=@/path/to/datasource_import.xlsx" \
              -F "dataTypeId=63db2fb476bc679c540369c4" \
              -F "hubId=63c63ab1b6972d3150348172"
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: Use a valid Bearer token to authenticate.

````