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

> Add a new facility to your organization

# Create Facility

Create a new facility in your organization. The system will track energy consumption and calculate CO2e emissions for the facility.

<Note>
  **Address or Country Required**: You must provide either an `address` (from which the country is geocoded) or a `country` code. If both are provided, the geocoded country from the address takes precedence.
</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="name" type="string" required>
  Name of the facility

  **Example:** `"Madrid Office"`
</ParamField>

<ParamField body="type" type="string" required>
  Type of facility

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

<ParamField body="country" type="string">
  ISO 3166-1 country code. Required if `address` is not provided.

  **Examples:** `"ES"`, `"FR"`, `"DE"`, `"US"`
</ParamField>

<ParamField body="address" type="string">
  Physical address of the facility. If provided, the country is automatically geocoded from the address.

  **Example:** `"Calle Gran Vía 1, Madrid, Spain"`
</ParamField>

<ParamField body="logistic_factor" type="number" default="0.8">
  Logistic factor for scope 2 calculation in logistics reports (0 to 1)

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

<ParamField body="categories" type="array[string]">
  Consumption categories enabled for this facility

  **Available values:** `heat`, `electricity`, `water`, `recharge`

  **Example:** `["heat", "electricity", "water"]`
</ParamField>

<ParamField body="cups_list" type="array[string]">
  List of CUPS (electricity supply point) codes

  **Example:** `["ES0021000000000001AA"]`
</ParamField>

## Response

Returns the created facility object with HTTP 201.

<ResponseField name="id" type="string">
  Facility UUID
</ResponseField>

<ResponseField name="name" type="string">
  Facility name
</ResponseField>

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

<ResponseField name="country" type="string">
  ISO country code (e.g. `ES`, `FR`, `DE`)
</ResponseField>

<ResponseField name="type" type="string">
  Facility type (e.g. `office`, `warehouse`, `factory`)
</ResponseField>

<ResponseField name="categories" type="array[string] | null">
  Enabled consumption categories: `heat`, `electricity`, `water`, `recharge`
</ResponseField>

<ResponseField name="cups_list" type="array[string] | null">
  List of CUPS codes
</ResponseField>

<ResponseField name="status" type="string">
  Facility status: `active` or `archived`
</ResponseField>

<ResponseField name="co2e" type="number | null">
  Total CO2e emissions (tCO2e)
</ResponseField>

<ResponseField name="co2e_biomass" type="number | null">
  CO2e emissions from biomass sources
</ResponseField>

<ResponseField name="logistic_factor" type="number | null">
  Logistic factor for scope 2 (0 to 1)
</ResponseField>

<ResponseField name="facility_purpose_type" type="string | null">
  Purpose type of the facility
</ResponseField>

<ResponseField name="created_at" type="datetime">
  Creation timestamp (ISO 8601)
</ResponseField>

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

<ResponseField name="invoices_in_review_count" type="integer">
  Number of invoices in `review` status
</ResponseField>

<ResponseField name="categories_without_data" type="integer">
  Number of configured categories with no invoices
</ResponseField>

<ResponseField name="water_type" type="string | null">
  Water type of the waste water treatment line (only for `waste_water_facilities`)
</ResponseField>

<ResponseField name="methane_burned" type="boolean | null">
  Whether methane is burned (only for `waste_water_facilities`)
</ResponseField>

<ResponseField name="wwt_line" type="object | null">
  Waste water treatment line config (only for `waste_water_facilities`)

  <Expandable title="WWT Line Object">
    <ResponseField name="id" type="string">Line UUID</ResponseField>
    <ResponseField name="line_code" type="string">Line code identifier</ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="sludge_line" type="object | null">
  Sludge treatment line config (only for `waste_water_facilities`)

  <Expandable title="Sludge Line Object">
    <ResponseField name="id" type="string">Line UUID</ResponseField>
    <ResponseField name="line_code" type="string">Line code identifier</ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="wwd_line" type="object | null">
  Water discharge line config (only for `waste_water_facilities`)

  <Expandable title="WWD Line Object">
    <ResponseField name="id" type="string">Line UUID</ResponseField>
    <ResponseField name="line_code" type="string">Line code identifier</ResponseField>
  </Expandable>
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST "https://api.dcycle.io/v1/facilities" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "Madrid Office",
      "type": "office",
      "country": "ES",
      "address": "Calle Gran Vía 1, Madrid",
      "logistic_factor": 0.8,
      "categories": ["heat", "electricity", "water"]
    }'
  ```

  ```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 = {
      "name": "Madrid Office",
      "type": "office",
      "country": "ES",
      "address": "Calle Gran Vía 1, Madrid",
      "logistic_factor": 0.8,
      "categories": ["heat", "electricity", "water"]
  }

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

  facility = response.json()
  print(f"Facility created: {facility['id']}")
  ```

  ```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'
  };

  axios.post('https://api.dcycle.io/v1/facilities', {
    name: "Madrid Office",
    type: "office",
    country: "ES",
    address: "Calle Gran Vía 1, Madrid",
    logistic_factor: 0.8,
    categories: ["heat", "electricity", "water"]
  }, { headers })
  .then(response => console.log(`Created: ${response.data.id}`));
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "Madrid Office",
  "type": "office",
  "country": "ES",
  "address": "Calle Gran Vía 1, Madrid",
  "status": "active",
  "co2e": null,
  "co2e_biomass": null,
  "logistic_factor": 0.8,
  "categories": ["heat", "electricity", "water"],
  "cups_list": null,
  "facility_purpose_type": "facilities",
  "invoices_in_review_count": 0,
  "categories_without_data": 0,
  "water_type": null,
  "methane_burned": null,
  "wwt_line": null,
  "sludge_line": null,
  "wwd_line": null,
  "created_at": "2024-11-24T10:30:00Z",
  "updated_at": "2024-11-24T10:30:00Z"
}
```

## Common Errors

### 401 Unauthorized

**Cause:** Missing or invalid API key

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Invalid API key for organization", "code": "INVALID_API_KEY"}
```

### 403 Forbidden

**Cause:** The authenticated user is not a member of the organization

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Logged User is not Member of Organization", "code": "LOGGED_USER_NOT_MEMBER"}
```

### 422 Validation Error

**Cause:** Neither address nor country provided

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "Either address or country is required."
}
```

**Cause:** Invalid address

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "Invalid address."
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="List Facilities" icon="list" href="/api-reference/facilities/list">
    Retrieve all facilities
  </Card>

  <Card title="Update Facility" icon="pencil" href="/api-reference/facilities/update">
    Modify facility details
  </Card>
</CardGroup>
