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

# Create Order

## Overview

Create a new order for the authenticated user. The order is automatically associated with the tenant specified in the X-Tenant-ID header.

## Authorization

* User must be authenticated with a valid Bearer token
* User must be a member of the tenant
* Requires X-Tenant-ID header

## Tenant Scoping

Orders are automatically associated with the tenant from the X-Tenant-ID header. The system ensures that:

* Products in the order belong to the same tenant
* The delivery address belongs to the authenticated user
* All data is isolated per tenant

## Request Body

| Field                       | Type    | Required | Description                                                |
| --------------------------- | ------- | -------- | ---------------------------------------------------------- |
| address\_id                 | integer | Yes      | ID of the delivery address (must exist in user\_addresses) |
| payment\_type               | integer | Yes      | Payment method identifier                                  |
| total\_amount               | number  | Yes      | Total order amount (must be >= 0)                          |
| order\_items                | array   | Yes      | Array of order items (minimum 1 item)                      |
| order\_items\[].product\_id | integer | Yes      | Product ID (must exist in products table)                  |
| order\_items\[].qty         | integer | Yes      | Quantity (must be >= 1)                                    |
| order\_items\[].amount      | number  | Yes      | Item amount (must be >= 0)                                 |
| order\_items\[].size        | string  | No       | Product size/variant                                       |
| order\_items\[].notes       | string  | No       | Special instructions for this item                         |

## Example Usage

```bash theme={null}
curl -X POST \
  https://faisalshop.mvp-apps.ae/api/v2/admin/orders \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'X-Tenant-ID: 123' \
  -H 'Content-Type: application/json' \
  -d '{
    "address_id": 2,
    "payment_type": 1,
    "total_amount": 99.99,
    "order_items": [
      {
        "product_id": 10,
        "qty": 2,
        "amount": 49.99,
        "size": "Large",
        "notes": "Extra packaging please"
      },
      {
        "product_id": 15,
        "qty": 1,
        "amount": 50.00,
        "size": "Medium"
      }
    ]
  }'
```

```javascript theme={null}
const orderData = {
  address_id: 2,
  payment_type: 1,
  total_amount: 99.99,
  order_items: [
    {
      product_id: 10,
      qty: 2,
      amount: 49.99,
      size: 'Large',
      notes: 'Extra packaging please'
    },
    {
      product_id: 15,
      qty: 1,
      amount: 50.00,
      size: 'Medium'
    }
  ]
};

const response = await axios.post('/api/v2/admin/orders', orderData, {
  headers: {
    Authorization: `Bearer ${token}`,
    'X-Tenant-ID': tenantId
  }
});

console.log(response.data);
```

## Success Response

```json theme={null}
{
  "success": true,
  "message": "Order created successfully",
  "data": {
    "id": 42,
    "user_id": 5,
    "tenant_id": "123",
    "address_id": 2,
    "payment_type": 1,
    "total_amount": "99.99",
    "created_at": "2025-11-16T10:30:00.000000Z",
    "updated_at": "2025-11-16T10:30:00.000000Z",
    "items": [
      {
        "id": 85,
        "order_id": 42,
        "product_id": 10,
        "size": "Large",
        "qty": 2,
        "amount": "49.99",
        "notes": "Extra packaging please"
      }
    ],
    "user": {
      "id": 5,
      "name": "John Doe",
      "email": "john@example.com"
    },
    "address": {
      "id": 2,
      "street": "123 Main St",
      "city": "Dubai"
    }
  }
}
```

## Error Responses

### Validation Error (422)

```json theme={null}
{
  "success": false,
  "errors": {
    "address_id": ["The address id field is required."],
    "order_items": ["The order items must have at least 1 items."],
    "order_items.0.product_id": ["The selected product id is invalid."]
  }
}
```

### Server Error (500)

```json theme={null}
{
  "success": false,
  "message": "Failed to create order",
  "error": "Database connection error"
}
```

## Notes

* The order is created within a database transaction for data integrity
* If any part of the order creation fails, the entire transaction is rolled back
* All order items are validated before creation
* The authenticated user is automatically set as the order owner
