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

# Update Commuting Period

> Modify an existing commuting period

# Update Commuting Period

Update an existing commuting period. You can modify transport details, dates, or other attributes. The CO2e will be recalculated automatically.

<Note>
  **Transport Field Groups**: When updating transport-related fields (`transport_type`, `vehicle_size`, `fuel_type`, `renewable_energy`, `carpool`), you must provide all five fields together.
</Note>

## Request

### Headers

<ParamField header="x-api-key" type="string" required>
  Your API key for authentication

  **Example:** `sk_live_1234567890abcdef`
</ParamField>

<ParamField header="x-organization-id" type="string" required>
  Your organization UUID

  **Example:** `a8315ef3-dd50-43f8-b7ce-d839e68d51fa`
</ParamField>

### Path Parameters

<ParamField path="employee_historic_id" type="string" required>
  The unique identifier (UUID) of the commuting period

  **Example:** `660e8400-e29b-41d4-a716-446655440000`
</ParamField>

### Body Parameters

All parameters are optional. Only include fields you want to update.

<ParamField body="start_date" type="date">
  Period start date. Must provide `end_date` as well.
</ParamField>

<ParamField body="end_date" type="date">
  Period end date. Must provide `start_date` as well.
</ParamField>

<ParamField body="commuting_type" type="string">
  Type: `in_itinere` or `in_labore`
</ParamField>

<ParamField body="transport_type" type="string">
  Mode of transport. When provided, must include all transport fields.
</ParamField>

<ParamField body="vehicle_size" type="string">
  Vehicle size for cars. Part of transport field group.
</ParamField>

<ParamField body="fuel_type" type="string">
  Fuel type. Part of transport field group.
</ParamField>

<ParamField body="renewable_energy" type="string">
  Renewable energy source. Part of transport field group.
</ParamField>

<ParamField body="carpool" type="boolean">
  Carpooling status. Part of transport field group.
</ParamField>

<ParamField body="total_km" type="number">
  One-way distance in kilometers.
</ParamField>

<ParamField body="weekly_travels" type="array[integer]">
  Days of week for commuting.
</ParamField>

<ParamField body="daily_trips" type="integer">
  Round trips per day.
</ParamField>

<ParamField body="situation" type="string">
  Period status: `active`, `inactive`, `terminated`
</ParamField>

## Response

Returns the updated commuting period with recalculated CO2e.

<ResponseField name="id" type="string">
  Commuting period UUID
</ResponseField>

<ResponseField name="employee_id" type="string">
  Parent employee UUID
</ResponseField>

<ResponseField name="start_date" type="date">
  Period start date (YYYY-MM-DD)
</ResponseField>

<ResponseField name="end_date" type="string | null">
  Period end date (YYYY-MM-DD)
</ResponseField>

<ResponseField name="commuting_type" type="string">
  `in_itinere` (home-to-office) or `in_labore` (work-related travel)
</ResponseField>

<ResponseField name="transport_type" type="string | null">
  Mode of transport (e.g. `car`, `train`, `bicycle`, `bus`)
</ResponseField>

<ResponseField name="vehicle_size" type="string | null">
  Vehicle size category
</ResponseField>

<ResponseField name="fuel_type" type="string | null">
  Fuel type
</ResponseField>

<ResponseField name="renewable_energy" type="string | null">
  Renewable energy source
</ResponseField>

<ResponseField name="carpool" type="boolean">
  Whether the employee carpools
</ResponseField>

<ResponseField name="total_km" type="number | null">
  One-way distance in kilometers
</ResponseField>

<ResponseField name="weekly_travels" type="array[integer] | null">
  Days of the week the employee commutes (0=Monday, 6=Sunday)
</ResponseField>

<ResponseField name="daily_trips" type="integer">
  Round trips per commuting day
</ResponseField>

<ResponseField name="situation" type="string | null">
  Period status: `active`, `inactive`, `terminated`
</ResponseField>

<ResponseField name="origin" type="string | null">
  Origin address
</ResponseField>

<ResponseField name="destination" type="string | null">
  Destination address
</ResponseField>

<ResponseField name="response_medium" type="string | null">
  How the data was collected (e.g. `manual`, `survey`, `csv`)
</ResponseField>

<ResponseField name="co2e" type="number | null">
  Calculated CO2 equivalent emissions in kg
</ResponseField>

<ResponseField name="created_at" type="string | null">
  ISO 8601 creation timestamp
</ResponseField>

<ResponseField name="updated_at" type="string | null">
  ISO 8601 last update timestamp
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X PATCH "https://api.dcycle.io/v1/employee-historic/660e8400-e29b-41d4-a716-446655440000" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "transport_type": "train",
      "vehicle_size": null,
      "fuel_type": "electric",
      "renewable_energy": "yes",
      "carpool": false
    }'
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import requests
  import os

  headers = {
      "x-api-key": os.getenv("DCYCLE_API_KEY"),
      "x-organization-id": os.getenv("DCYCLE_ORG_ID"),
      "Content-Type": "application/json"
  }

  period_id = "660e8400-e29b-41d4-a716-446655440000"

  # Change from car to train
  payload = {
      "transport_type": "train",
      "vehicle_size": None,
      "fuel_type": "electric",
      "renewable_energy": "yes",
      "carpool": False
  }

  response = requests.patch(
      f"https://api.dcycle.io/v1/employee-historic/{period_id}",
      headers=headers,
      json=payload
  )

  period = response.json()
  print(f"Updated transport: {period['transport_type']}")
  print(f"New CO2e: {period['co2e']} kg")
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const axios = require('axios');

  const headers = {
    'x-api-key': process.env.DCYCLE_API_KEY,
    'x-organization-id': process.env.DCYCLE_ORG_ID,
    'Content-Type': 'application/json'
  };

  const periodId = '660e8400-e29b-41d4-a716-446655440000';

  // Change from car to train
  const payload = {
    transport_type: 'train',
    vehicle_size: null,
    fuel_type: 'electric',
    renewable_energy: 'yes',
    carpool: false
  };

  axios.patch(
    `https://api.dcycle.io/v1/employee-historic/${periodId}`,
    payload,
    { headers }
  )
  .then(response => {
    const period = response.data;
    console.log(`Updated transport: ${period.transport_type}`);
    console.log(`New CO2e: ${period.co2e} kg`);
  });
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "660e8400-e29b-41d4-a716-446655440000",
  "employee_id": "550e8400-e29b-41d4-a716-446655440000",
  "start_date": "2024-01-01",
  "end_date": "2024-12-31",
  "commuting_type": "in_itinere",
  "transport_type": "train",
  "vehicle_size": null,
  "fuel_type": "electric",
  "renewable_energy": "yes",
  "total_km": 15,
  "weekly_travels": [0, 1, 2, 3, 4],
  "daily_trips": 1,
  "carpool": false,
  "situation": "active",
  "origin": null,
  "destination": null,
  "response_medium": "manual",
  "co2e": 312.4,
  "created_at": "2024-01-15T10:30:00Z",
  "updated_at": "2024-11-24T14:45:00Z"
}
```

## Common Errors

### 422 Validation Error

**Cause:** Partial transport field update

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body"],
      "msg": "When providing any transport field, all fields must be provided. Missing: vehicle_size, carpool",
      "type": "value_error"
    }
  ]
}
```

**Solution:** Include all transport fields: `transport_type`, `vehicle_size`, `fuel_type`, `renewable_energy`, `carpool`.

## Use Cases

### Update Distance

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Just update the distance
payload = {"total_km": 20}
```

### Change Working Days

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Switch to hybrid (3 days/week)
payload = {"weekly_travels": [1, 2, 3]}
```

### Update Date Range

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Both dates required together
payload = {
    "start_date": "2024-07-01",
    "end_date": "2024-12-31"
}
```

### Switch Transport Mode

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# All transport fields required together
payload = {
    "transport_type": "bicycle",
    "vehicle_size": None,
    "fuel_type": "not_fuel_based",
    "renewable_energy": None,
    "carpool": False
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Create Period" icon="plus" href="/api-reference/employees/commuting-periods/create">
    Add new period
  </Card>

  <Card title="Delete Period" icon="trash" href="/api-reference/employees/commuting-periods/delete">
    Remove period
  </Card>
</CardGroup>
