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

# Update Booking

> Updates an existing booking

## Description

This endpoint allows you to update an existing booking. You can modify any booking field, including schedules, status, customer information, and notes.

## Authentication

<ParamField header="X-API-Key" type="string" required>
  Your LatePoint API Key with write permissions
</ParamField>

## Path Parameters

<ParamField path="id" type="integer" required>
  Unique ID of the booking to update
</ParamField>

## Request Body

### Booking Fields

<ParamField body="service_id" type="integer">
  Service ID (requires availability verification if changed)
</ParamField>

<ParamField body="agent_id" type="integer">
  Agent ID (requires availability verification if changed)
</ParamField>

<ParamField body="location_id" type="integer">
  Location ID
</ParamField>

<ParamField body="start_date" type="string">
  New booking date (format: YYYY-MM-DD)
</ParamField>

<ParamField body="start_time" type="string">
  New start time (format: HH:MM in 24-hour format)
</ParamField>

<ParamField body="duration" type="integer">
  Custom duration in minutes
</ParamField>

<ParamField body="status" type="string">
  Booking status

  **Possible values:**

  * `pending` - Pending
  * `approved` - Approved
  * `cancelled` - Cancelled
  * `completed` - Completed
  * `no_show` - No show
</ParamField>

### Customer Information

<ParamField body="customer_id" type="integer">
  Change to a different customer (customer ID)
</ParamField>

<ParamField body="customer.first_name" type="string">
  Update customer first name
</ParamField>

<ParamField body="customer.last_name" type="string">
  Update customer last name
</ParamField>

<ParamField body="customer.email" type="string">
  Update customer email
</ParamField>

<ParamField body="customer.phone" type="string">
  Update customer phone
</ParamField>

### Additional Fields

<ParamField body="price" type="string">
  Custom price for this booking
</ParamField>

<ParamField body="notes" type="string">
  Public booking notes
</ParamField>

<ParamField body="send_notification" type="boolean" default="false">
  Whether to send notification to customer about changes
</ParamField>

## Response

### Successful Response (200 OK)

<ResponseField name="status" type="string">
  Response status ("success")
</ResponseField>

<ResponseField name="data" type="object">
  Updated booking object with all fields
</ResponseField>

## Examples

### Change Booking Status

<CodeGroup>
  ```bash cURL theme={null}
  curl -X PUT "http://latepoint-dev.local/wp-json/latepoint-api/v1/bookings/16" \
    -H "X-API-Key: lp_n1k6BVf3h7JRyjXkWMSoXi0BBZYRaOLL4QohDPQJ" \
    -H "Content-Type: application/json" \
    -d '{
      "start_time": "15:30",
      "duration": 90,
      "status": "approved",
      "notes": "Tiempo extendido por solicitud del cliente"
    }'
  ```

  ```javascript JavaScript theme={null}
  const bookingId = 123;
  const updateData = {
    status: 'approved',
    send_notification: true
  };

  const response = await fetch(`http://latepoint-dev.local/wp-json/latepoint-api/v1/bookings/${bookingId}`, {
    method: 'PUT',
    headers: {
      'X-API-Key': 'lp_n1k6BVf3h7JRyjXkWMSoXi0BBZYRaOLL4QohDPQJ',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(updateData)
  });

  const result = await response.json();
  console.log(result);
  ```

  ```python Python theme={null}
  import requests
  import json

  booking_id = 123
  update_data = {
      'status': 'approved',
      'send_notification': True
  }

  response = requests.put(
      f'http://latepoint-dev.local/wp-json/latepoint-api/v1/bookings/{booking_id}',
      headers={
          'X-API-Key': 'lp_n1k6BVf3h7JRyjXkWMSoXi0BBZYRaOLL4QohDPQJ',
          'Content-Type': 'application/json'
      },
      data=json.dumps(update_data)
  )

  result = response.json()
  print(result)
  ```

  ```php PHP theme={null}
  <?php
  $booking_id = 123;
  $update_data = [
      'status' => 'approved',
      'send_notification' => true
  ];

  $curl = curl_init();

  curl_setopt_array($curl, [
      CURLOPT_URL => "http://latepoint-dev.local/wp-json/latepoint-api/v1/bookings/{$booking_id}",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_CUSTOMREQUEST => 'PUT',
      CURLOPT_POSTFIELDS => json_encode($update_data),
      CURLOPT_HTTPHEADER => [
          'X-API-Key: lp_n1k6BVf3h7JRyjXkWMSoXi0BBZYRaOLL4QohDPQJ',
          'Content-Type: application/json'
      ]
  ]);

  $response = curl_exec($curl);
  $result = json_decode($response, true);

  curl_close($curl);
  print_r($result);
  ?>
  ```
</CodeGroup>

## Error Codes

<ResponseExample>
  ```json Error 404 - Booking Not Found theme={null}
  {
    "status": "error",
    "error": {
      "code": "booking_not_found",
      "message": "Booking with ID 123 was not found"
    }
  }
  ```

  ```json Error 409 - Schedule Conflict theme={null}
  {
    "status": "error",
    "error": {
      "code": "time_slot_unavailable",
      "message": "The new time slot is not available",
      "details": {
        "requested_time": "2024-01-25 16:00",
        "agent_id": 3,
        "conflicting_booking_id": 156,
        "available_slots": [
          "2024-01-25 17:00",
          "2024-01-25 18:00"
        ]
      }
    }
  }
  ```

  ```json Error 422 - Validation Failed theme={null}
  {
    "status": "error",
    "error": {
      "code": "validation_failed",
      "message": "The provided data failed validation",
      "details": {
        "start_date": "Date cannot be in the past",
        "customer.email": "Invalid email",
        "status": "Invalid status for this booking"
      }
    }
  }
  ```

  ```json Error 400 - Invalid Data theme={null}
  {
    "status": "error",
    "error": {
      "code": "invalid_request",
      "message": "Invalid input data",
      "details": {
        "service_id": "The specified service does not exist",
        "agent_id": "The specified agent does not exist"
      }
    }
  }
  ```
</ResponseExample>

## Validations

### Automatic Validations

1. **Availability**: If you change date, time or agent, availability is verified
2. **Valid States**: Only allows valid status transitions
3. **Dates**: Does not allow changing to past dates
4. **Unique Email**: Verifies that email is not in use by another customer
5. **Permissions**: Verifies that you have permissions to modify the booking

### Status Transition Rules

| Current Status | Allowed States                         |
| -------------- | -------------------------------------- |
| `pending`      | `approved`, `cancelled`                |
| `approved`     | `completed`, `cancelled`, `no_show`    |
| `cancelled`    | `pending`, `approved`                  |
| `completed`    | `cancelled` (special permissions only) |
| `no_show`      | `approved`, `cancelled`                |

## Common Use Cases

### 1. Confirm Pending Booking

```javascript theme={null}
// Confirm a booking that was pending
async function confirmBooking(bookingId) {
  const response = await fetch(`/wp-json/latepoint-api/v1/bookings/${bookingId}`, {
    method: 'PUT',
    headers: {
      'X-API-Key': 'your_api_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      status: 'approved',
      send_notification: true
    })
  });
  
  if (response.ok) {
    const result = await response.json();
    console.log('Booking confirmed:', result.data.booking_code);
    return result.data;
  }
  
  throw new Error('Error confirming booking');
}
```

### 2. Reschedule with Availability Check

```javascript theme={null}
// Reschedule a booking by checking availability first
async function rescheduleBooking(bookingId, newDate, newTime, agentId, serviceId) {
  // First check availability
const availabilityResponse = await fetch(
`/wp-json/latepoint-api/v1/availability?service_id=${serviceId}&agent_id=${agentId}&date=${newDate}&duration=${duration}`
);
  
  const availability = await availabilityResponse.json();
  
  if (!availability.data.available_slots.includes(newTime)) {
    throw new Error('The requested time slot is not available');
  }
  
  // Proceed with rescheduling
  const response = await fetch(`/wp-json/latepoint-api/v1/bookings/${bookingId}`, {
    method: 'PUT',
    headers: {
      'X-API-Key': 'your_api_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      start_date: newDate,
      start_time: newTime,
      send_notification: true,
      notes: 'Rescheduled by customer request'
    })
  });
  
  return await response.json();
}
```
