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

# Create Commuting Period

> Add a new commuting period for an employee to track transportation emissions

# Create Commuting Period

Create a new commuting period for an employee. This defines how the employee commutes during a specific date range, enabling CO2e emissions calculations.

<Note>
  **Transport Type Rules**: Different transport types require different field combinations. See the [transport validation rules](#transport-validation-rules) below.
</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>

### Body Parameters

<ParamField body="employee_id" type="string" required>
  UUID of the employee this period belongs to

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

<ParamField body="start_date" type="date" required>
  Period start date (YYYY-MM-DD)

  **Example:** `"2024-01-01"`
</ParamField>

<ParamField body="end_date" type="date" required>
  Period end date (YYYY-MM-DD). Must be after start\_date.

  **Example:** `"2024-12-31"`
</ParamField>

<ParamField body="commuting_type" type="string" required>
  Type of commute

  **Available values:**

  * `in_itinere` - Home to work commute (daily commuting)
  * `in_labore` - Travel during work hours

  **Example:** `"in_itinere"`
</ParamField>

<ParamField body="transport_type" type="string" required>
  Mode of transportation

  **Available values:** `car`, `bus`, `train`, `metro`, `tram`, `motorbike`, `bicycle`, `walking`, `telecommuting`, `electric_kick_scooter`, `trolleybus`

  **Example:** `"car"`
</ParamField>

<ParamField body="total_km" type="number">
  One-way distance in kilometers. Required for all transport types except `telecommuting`.

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

<ParamField body="weekly_travels" type="array[integer]">
  Days of the week the employee commutes (0=Monday, 6=Sunday). Required for all transport types except `telecommuting`.

  **Example:** `[0, 1, 2, 3, 4]` (Monday to Friday)
</ParamField>

<ParamField body="daily_trips" type="integer" required>
  Number of round trips per day (usually 1)

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

<ParamField body="vehicle_size" type="string">
  Vehicle size (required for `car` transport type)

  **Available values:** `small`, `medium`, `large`

  **Example:** `"medium"`
</ParamField>

<ParamField body="fuel_type" type="string">
  Fuel type. Required for `car`, `bus`, `motorbike`. Optional for `train`, `metro`, `bicycle`, `trolleybus`.

  **Available values:** `petrol`, `diesel`, `electric`, `hybrid`, `lpg`, `natural_gas`, `not_fuel_based`, `do_not_know`

  **Example:** `"petrol"`
</ParamField>

<ParamField body="renewable_energy" type="string">
  For electric vehicles/transport - whether powered by renewable energy

  **Available values:** `yes`, `no`, `do_not_know`

  **Example:** `"yes"`
</ParamField>

<ParamField body="carpool" type="boolean" required>
  Whether the employee carpools. Must be `true` or `false` for `car`, must be `false` for other transport types.

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

<ParamField body="situation" type="string" required>
  Period status

  **Available values:** `active`, `inactive`, `terminated`

  **Example:** `"active"`
</ParamField>

<ParamField body="response_medium" type="string" required>
  How the commuting data was collected

  **Available values:** `manual`, `qr`, `form`

  **Example:** `"manual"`
</ParamField>

## Transport Validation Rules

When providing transport fields, all related fields must be provided together:

### Car

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "car",
  "vehicle_size": "small|medium|large",  // required
  "fuel_type": "petrol|diesel|electric|hybrid|lpg|natural_gas|do_not_know",  // required
  "carpool": true|false,  // required
  "renewable_energy": "yes|no|do_not_know"  // only if fuel_type is "electric"
}
```

### Bus

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "bus",
  "vehicle_size": null,
  "fuel_type": "diesel|natural_gas|petrol|lpg|do_not_know",  // required
  "carpool": false,
  "renewable_energy": null
}
```

### Train / Metro / Trolleybus

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "train|metro|trolleybus",
  "vehicle_size": null,
  "fuel_type": "electric|do_not_know",  // optional
  "carpool": false,
  "renewable_energy": "yes|no|do_not_know"  // only if fuel_type is "electric"
}
```

### Motorbike

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "motorbike",
  "vehicle_size": null,
  "fuel_type": "diesel|petrol|electric|do_not_know",  // required
  "carpool": false,
  "renewable_energy": "yes|no|do_not_know"  // only if fuel_type is "electric"
}
```

### Bicycle

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "bicycle",
  "vehicle_size": null,
  "fuel_type": "electric|not_fuel_based|do_not_know",  // optional
  "carpool": false,
  "renewable_energy": "yes|no|do_not_know"  // only if fuel_type is "electric"
}
```

### Walking / Tram / Electric Kick Scooter / Telecommuting

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "transport_type": "walking|tram|electric_kick_scooter|telecommuting",
  "vehicle_size": null,
  "fuel_type": null,
  "carpool": false,
  "renewable_energy": null
}
```

## Response

<ResponseField name="id" type="string">
  Unique identifier (UUID)
</ResponseField>

<ResponseField name="employee_id" type="string">
  Employee UUID
</ResponseField>

<ResponseField name="co2e" type="number">
  Calculated CO2 equivalent emissions in kg for the period
</ResponseField>

All other fields mirror the request body.

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST "https://api.dcycle.io/v1/employee-historic" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "employee_id": "550e8400-e29b-41d4-a716-446655440000",
      "start_date": "2024-01-01",
      "end_date": "2024-12-31",
      "commuting_type": "in_itinere",
      "transport_type": "car",
      "vehicle_size": "medium",
      "fuel_type": "petrol",
      "renewable_energy": null,
      "total_km": 15,
      "weekly_travels": [0, 1, 2, 3, 4],
      "daily_trips": 1,
      "carpool": false,
      "situation": "active",
      "response_medium": "manual"
    }'
  ```

  ```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"
  }

  payload = {
      "employee_id": "550e8400-e29b-41d4-a716-446655440000",
      "start_date": "2024-01-01",
      "end_date": "2024-12-31",
      "commuting_type": "in_itinere",
      "transport_type": "car",
      "vehicle_size": "medium",
      "fuel_type": "petrol",
      "renewable_energy": None,
      "total_km": 15,
      "weekly_travels": [0, 1, 2, 3, 4],
      "daily_trips": 1,
      "carpool": False,
      "situation": "active",
      "response_medium": "manual"
  }

  response = requests.post(
      "https://api.dcycle.io/v1/employee-historic",
      headers=headers,
      json=payload
  )

  period = response.json()
  print(f"Period created: {period['id']}")
  print(f"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 payload = {
    employee_id: '550e8400-e29b-41d4-a716-446655440000',
    start_date: '2024-01-01',
    end_date: '2024-12-31',
    commuting_type: 'in_itinere',
    transport_type: 'car',
    vehicle_size: 'medium',
    fuel_type: 'petrol',
    renewable_energy: null,
    total_km: 15,
    weekly_travels: [0, 1, 2, 3, 4],
    daily_trips: 1,
    carpool: false,
    situation: 'active',
    response_medium: 'manual'
  };

  axios.post(
    'https://api.dcycle.io/v1/employee-historic',
    payload,
    { headers }
  )
  .then(response => {
    const period = response.data;
    console.log(`Period created: ${period.id}`);
    console.log(`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": "car",
  "vehicle_size": "medium",
  "fuel_type": "petrol",
  "renewable_energy": null,
  "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": 1245.5,
  "created_at": "2024-11-24T10:30:00Z",
  "updated_at": "2024-11-24T10:30:00Z"
}
```

## Common Errors

### 422 Validation Error

**Cause:** Invalid field combinations

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["body"],
      "msg": "When providing transport_type 'car', vehicle_size must also be provided",
      "type": "value_error"
    }
  ]
}
```

**Solution:** Ensure all required fields for the transport type are provided.

### 404 Not Found

**Cause:** Employee not found

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "code": "EMPLOYEE_NOT_FOUND",
  "detail": "Employee with id=UUID('...') not found"
}
```

## Use Cases

### Create Car Commute

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
car_period = {
    "employee_id": employee_id,
    "start_date": "2024-01-01",
    "end_date": "2024-12-31",
    "commuting_type": "in_itinere",
    "transport_type": "car",
    "vehicle_size": "medium",
    "fuel_type": "diesel",
    "renewable_energy": None,
    "total_km": 20,
    "weekly_travels": [0, 1, 2, 3, 4],
    "daily_trips": 1,
    "carpool": False,
    "situation": "active",
    "response_medium": "manual"
}
```

### Create Public Transit Commute

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
train_period = {
    "employee_id": employee_id,
    "start_date": "2024-01-01",
    "end_date": "2024-12-31",
    "commuting_type": "in_itinere",
    "transport_type": "train",
    "vehicle_size": None,
    "fuel_type": "electric",
    "renewable_energy": "yes",
    "total_km": 30,
    "weekly_travels": [1, 2, 3],  # Tue, Wed, Thu
    "daily_trips": 1,
    "carpool": False,
    "situation": "active",
    "response_medium": "manual"
}
```

### Create Remote Worker Period

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
remote_period = {
    "employee_id": employee_id,
    "start_date": "2024-01-01",
    "end_date": "2024-12-31",
    "commuting_type": "in_itinere",
    "transport_type": "telecommuting",
    "vehicle_size": None,
    "fuel_type": None,
    "renewable_energy": None,
    "total_km": None,
    "weekly_travels": [],  # Empty = no commuting
    "daily_trips": 0,
    "carpool": False,
    "situation": "active",
    "response_medium": "manual"
}
```

### Create Carpool Commute

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
carpool_period = {
    "employee_id": employee_id,
    "start_date": "2024-01-01",
    "end_date": "2024-12-31",
    "commuting_type": "in_itinere",
    "transport_type": "car",
    "vehicle_size": "large",
    "fuel_type": "petrol",
    "renewable_energy": None,
    "total_km": 25,
    "weekly_travels": [0, 1, 2, 3, 4],
    "daily_trips": 1,
    "carpool": True,  # Emissions divided by 3
    "situation": "active",
    "response_medium": "manual"
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Update Commuting Period" icon="pencil" href="/api-reference/employees/commuting-periods/update">
    Modify period details
  </Card>

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

  <Card title="List Periods" icon="list" href="/api-reference/employees/commuting-periods/list">
    View all periods
  </Card>

  <Card title="Employee Commuting Guide" icon="book" href="/guides/emissions/scope-3-category-7-employee-commuting">
    Complete commuting tutorial
  </Card>
</CardGroup>
