> ## 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.

# Create Organization

> Create a new organization and automatically switch to it. The user becomes the owner of the new organization.

**Requirements:**
- User must be authenticated
- User must have `create/organization` permission

**Behavior:**
- If user has an existing owned organization with hubs, hub fields are optional (will copy from source)
- If user has no owned organization, hub fields (hubName, hubLat, hubLng) are required
- If `copyDataFromFirstOrg` is true, copies configuration, hubs, and flows from user's first owned organization
- Creates default flows from templates
- Generates sample data (vehicles, data sources, tasks) via background job

**Note:** After creation, the previous access token is revoked. You must use the new token returned in the response.



## OpenAPI

````yaml /openapi/public/openapi-auth.json post /organization
openapi: 3.0.0
info:
  title: MileApp API - Auth
  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: Multi-Organization
    description: >-
      Multi-Organization feature allows users to belong to multiple
      organizations using the same email address. This enables seamless
      switching between organizations without requiring separate login
      credentials. When a user logs in with an email associated with multiple
      organizations, they can select which organization to access and switch
      between them with password verification only.


      **Related Guide:** [User
      Management](https://doc.mile.app/pages/setting/user-management/introduction-to-user-management)
paths:
  /organization:
    post:
      tags:
        - Multi-Organization
      summary: Create Organization
      description: >-
        Create a new organization and automatically switch to it. The user
        becomes the owner of the new organization.


        **Requirements:**

        - User must be authenticated

        - User must have `create/organization` permission


        **Behavior:**

        - If user has an existing owned organization with hubs, hub fields are
        optional (will copy from source)

        - If user has no owned organization, hub fields (hubName, hubLat,
        hubLng) are required

        - If `copyDataFromFirstOrg` is true, copies configuration, hubs, and
        flows from user's first owned organization

        - Creates default flows from templates

        - Generates sample data (vehicles, data sources, tasks) via background
        job


        **Note:** After creation, the previous access token is revoked. You must
        use the new token returned in the response.
      operationId: createOrganization
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required:
                - organizationName
              properties:
                organizationName:
                  type: string
                  minLength: 3
                  maxLength: 100
                  description: Name for the new organization
                hubName:
                  type: string
                  maxLength: 100
                  description: >-
                    Hub name. Required if user has no existing owned
                    organization with hubs.
                hubLat:
                  type: number
                  minimum: -90
                  maximum: 90
                  description: >-
                    Hub latitude. Required if user has no existing owned
                    organization with hubs.
                hubLng:
                  type: number
                  minimum: -180
                  maximum: 180
                  description: >-
                    Hub longitude. Required if user has no existing owned
                    organization with hubs.
                copyDataFromFirstOrg:
                  type: boolean
                  default: false
                  description: >-
                    Whether to copy data from user's first organization where
                    they have 'owner' role. When enabled, copies: (1)
                    Organization configuration (language, currency, settings),
                    (2) All hubs the user has access to, (3) Custom roles with
                    system permissions only (custom module permissions are
                    excluded), (4) All flows. Note: Default roles (owner, admin,
                    planner, field user) always use standard templates
                    regardless of this setting.
              example:
                organizationName: My New Company
                hubName: Main Warehouse
                hubLat: -6.2088
                hubLng: 106.8456
                copyDataFromFirstOrg: false
        required: true
      responses:
        '200':
          description: >-
            Organization created successfully - returns new access token and
            user data
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                    description: Operation success status
                  message:
                    type: string
                    description: Success message
                  token:
                    type: string
                    description: New access token for the created organization
                  expires_in:
                    type: integer
                    description: Token expiration time in seconds
                  data:
                    type: object
                    description: User data for the new organization
                    additionalProperties: true
                  isHubSet:
                    type: boolean
                    description: Always true for new organizations
                  organizations:
                    type: array
                    description: Updated list of all user's organizations
                    items:
                      type: object
                      properties:
                        userId:
                          type: string
                        organizationId:
                          type: string
                        organizationName:
                          type: string
                        organizationLogo:
                          type: string
                        roleName:
                          type: string
                        isCurrent:
                          type: boolean
                example:
                  status: true
                  message: Organization created successfully
                  token: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...
                  expires_in: 31536000
                  data:
                    _id: 507f1f77bcf86cd799439099
                    name: John Doe
                    email: john@example.com
                    organizationId: 507f1f77bcf86cd799439100
                    hubId:
                      - 507f1f77bcf86cd799439102
                  isHubSet: true
                  organizations:
                    - userId: 507f1f77bcf86cd799439011
                      organizationId: 507f1f77bcf86cd799439022
                      organizationName: Acme Corp
                      organizationLogo: null
                      roleName: owner
                      isCurrent: false
                    - userId: 507f1f77bcf86cd799439099
                      organizationId: 507f1f77bcf86cd799439100
                      organizationName: My New Company
                      organizationLogo: null
                      roleName: owner
                      isCurrent: true
        '400':
          description: Bad request - Validation error or hub fields required
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                example:
                  status: false
                  message: Hub name is required when you have no existing organization
        '403':
          description: Forbidden - User doesn't have create/organization permission
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                  message:
                    type: string
                example:
                  status: false
                  message: >-
                    You do not have permission to create a new organization.
                    Please contact your administrator.
      deprecated: false
      security:
        - bearerAuth: []
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: Use a valid Bearer token to authenticate.

````