Skip to main content

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

FieldTypeRequiredDescription
address_idintegerYesID of the delivery address (must exist in user_addresses)
payment_typeintegerYesPayment method identifier
total_amountnumberYesTotal order amount (must be >= 0)
order_itemsarrayYesArray of order items (minimum 1 item)
order_items[].product_idintegerYesProduct ID (must exist in products table)
order_items[].qtyintegerYesQuantity (must be >= 1)
order_items[].amountnumberYesItem amount (must be >= 0)
order_items[].sizestringNoProduct size/variant
order_items[].notesstringNoSpecial instructions for this item

Example Usage

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"
      }
    ]
  }'
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

{
  "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": "[email protected]"
    },
    "address": {
      "id": 2,
      "street": "123 Main St",
      "city": "Dubai"
    }
  }
}

Error Responses

Validation Error (422)

{
  "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)

{
  "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