# create-resource

Learn how to create Croct resources.

This action creates Croct resources such as [components](/explanation/component), [slots](/explanation/slot), [audiences](/explanation/audience/introduction), and experiences in the workspace.

> **Provisioning only**
>
> Creating a resource only provisions it in the workspace. To add it to the project, use [`add-component`](/reference/cli/templates/actions/add-component) for components and [`add-slot`](/reference/cli/templates/actions/add-slot) for slots.

Before creating a resource, the action checks whether a similar one already exists based on its ID and definition. When a match is found, the existing resource is reused instead of creating a duplicate.

This makes the action idempotent — running the same template multiple times does not consume additional workspace quota or produce redundant resources.

Because the matched resource may have a different ID or version than the one specified in the template, capturing the [`result`](#result-prop) is important when subsequent actions depend on precise identifiers.

## Example

The following template creates a component, a slot, an audience, and an experience that personalizes the slot for logged-in users. It also captures the slot ID for use in a subsequent [`add-slot`](/reference/cli/templates/actions/add-slot) action:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "Personalized banner",
  "description": "Creates a personalized banner for logged-in users.",
  "actions": [
    {
      "name": "create-resource",
      "resources": {
        "components": {
          "top-bar": {
            "name": "Top bar",
            "schema": {
              "type": "structure",
              "attributes": {
                "message": {
                  "type": {
                    "type": "text"
                  },
                  "label": "Message",
                  "position": 0
                }
              }
            }
          }
        },
        "slots": {
          "home-banner": {
            "name": "Home banner",
            "component": "top-bar",
            "content": {
              "en": {
                "type": "structure",
                "attributes": {
                  "message": {
                    "type": "text",
                    "value": {
                      "type": "static",
                      "value": "Welcome to our site!"
                    }
                  }
                }
              }
            }
          }
        },
        "audiences": {
          "logged-in-users": {
            "name": "Logged-in users",
            "criteria": "user is identified"
          }
        },
        "experiences": [
          {
            "name": "Logged-in banner",
            "audiences": ["logged-in-users"],
            "slots": ["home-banner"],
            "content": {
              "default": {
                "home-banner": {
                  "en": {
                    "type": "structure",
                    "attributes": {
                      "message": {
                        "type": "text",
                        "value": {
                          "type": "static",
                          "value": "Welcome back!"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      },
      "result": {
        "slots": {
          "home-banner": {
            "id": "bannerSlotId",
            "version": "bannerSlotVersion"
          }
        }
      }
    },
    {
      "name": "add-slot",
      "slots": ["${this.bannerSlotId}@${this.bannerSlotVersion}"]
    }
  ]
}
```

To apply this template, run:

```sh
croct use template.json5
```

All resources are provisioned in the workspace. Logged-in users see "Welcome back!" in the banner, while other visitors see the slot's default content. The [`result`](#result-prop) captures the slot ID and version so that [`add-slot`](/reference/cli/templates/actions/add-slot) references the exact resource, regardless of whether it was newly created or matched from an existing one.

## Properties

These are the supported properties:

- `name`: `string`

  The action identifier. Always `create-resource` for this action.

- `resources`: `object`

  The resources to create in the workspace.

  At least one resource type must be specified.

  - `components`: `object` (optional)

    A map of [component](/explanation/component) IDs to component definitions.

    - `name`: `string`

      The display name shown in the interface.

    - `description`: `string` (optional)

      A brief description of the component.

      Shown in the interface to help editors understand the component's purpose.

    - `schema`: `ContentSchema` (see [ContentSchema](/reference/content/schema/introduction))

      The schema defining the component's structure.

      See [Content schema](/reference/content/schema/introduction) for more information.

  - `slots`: `object` (optional)

    A map of [slot](/explanation/slot) IDs to slot definitions.

    - `name`: `string`

      The display name shown in the interface.

    - `component`: `string`

      The ID of the component this slot uses.

      Must reference a component defined in this template or already existing in the workspace.

    - `content`: `Record<string, Content>` (see [Content](/reference/content/definition/introduction))

      A map of locale codes to content values.

      Each locale must exist in the workspace. For example, `en` or `pt-br`.

      See [Content definition](/reference/content/definition/introduction) for more information.

  - `audiences`: `object` (optional)

    A map of [audience](/explanation/audience/introduction) IDs to audience definitions.

    - `name`: `string`

      The display name shown in the interface.

    - `criteria`: `string`

      A CQL expression that defines who belongs to the audience.

      See [CQL](/reference/cql/introduction) for more information.

  - `experiences`: `Array<object>` (optional)

    A list of [experience](/explanation/content/experience-content) definitions.

    - `name`: `string`

      The display name shown in the interface.

    - `draft`: `boolean` (optional) (default: false)

      Whether the experience starts as a draft.

      Draft experiences are not published and can be partially defined. When `false`, the experience is published immediately.

    - `audiences`: `Array<string>`

      The audience IDs this experience targets.

      Each ID must reference an audience defined in this template or already existing in the workspace.

    - `slots`: `Array<string>`

      The slot IDs this experience personalizes.

      Each ID must reference a slot defined in this template or already existing in the workspace.

    - `content`: `object`

      The personalized content for the experience.

      - `default`: `Record<string, Record<string, Content>>` (optional) (see [Content](/reference/content/definition/introduction))

        The catch-all content shown when no segmented content matches.

        Structured as a map of slot IDs to a map of locale codes to [content values](/reference/content/definition/introduction).

      - `segmented`: `Array<object>` (optional)

        A list of audience-specific content overrides.

        Each entry targets one or more audiences with dedicated content.

        - `audiences`: `Array<string>`

          The audience IDs this content applies to.

          Users matching any of these audiences see this content.

        - `content`: `Record<string, Record<string, Content>>` (see [Content](/reference/content/definition/introduction))

          The content for this audience group.

          Structured as a map of slot IDs to a map of locale codes to [content values](/reference/content/definition/introduction).

    - `experiment`: `object` (optional)

      An A/B [experiment](/explanation/content/experiment-content) to run within this experience.

      When specified, the experience splits traffic between variants to measure which performs better.

      - `name`: `string`

        The display name shown in the interface.

      - `traffic`: `number`

        The percentage of traffic to include in the experiment.

        Expressed as a decimal between 0 and 1. For example, `0.5` means 50% of visitors participate in the experiment, while the remaining 50% see the experience's default content.

      - `variants`: `Array<object>`

        The list of experiment variants.

        At least two variants are required.

        - `name`: `string`

          The display name shown in the interface.

        - `content`: `object`

          The personalized content for this variant.

          - `default`: `Record<string, Record<string, Content>>` (optional) (see [Content](/reference/content/definition/introduction))

            The catch-all content shown when no segmented content matches.

            Structured as a map of slot IDs to a map of locale codes to [content values](/reference/content/definition/introduction).

          - `segmented`: `Array<object>` (optional)

            A list of audience-specific content overrides.

            Each entry targets one or more audiences with dedicated content.

            - `audiences`: `Array<string>`

              The audience IDs this content applies to.

              Users matching any of these audiences see this content.

            - `content`: `Record<string, Record<string, Content>>` (see [Content](/reference/content/definition/introduction))

              The content for this audience group.

              Structured as a map of slot IDs to a map of locale codes to [content values](/reference/content/definition/introduction).

        - `allocation`: `number`

          The relative traffic weight for this variant.

          The ratio between variants determines how traffic is split. For example, values of `2` and `1` give the first variant twice the traffic of the second.

        - `baseline`: `boolean` (optional) (default: false)

          Whether this is the control variant.

          The baseline serves as the reference for measuring the performance of other variants.

      - `goalId`: `string` (optional)

        The identifier of the goal used to measure the experiment's success.

      - `crossDevice`: `boolean` (optional) (default: false)

        Whether to keep variant assignment consistent across devices.

        When enabled, identified users see the same variant across all their devices. This relies on the user ID for identification, so it only applies to logged-in users.

- `result`: `object` (optional)

  Variables to capture the IDs and versions of created or matched resources.

  The structure mirrors `resources`, mapping each resource key to variable names. Because duplicate detection may match an existing resource, the captured values can differ from the IDs specified in the template.

  - `components`: `object` (optional)

    A map of component IDs to variable names.

    - `id`: `string` (optional)

      The variable name to store the resolved component ID.

    - `version`: `string` (optional)

      The variable name to store the resolved major version number.

  - `slots`: `object` (optional)

    A map of slot IDs to variable names.

    - `id`: `string` (optional)

      The variable name to store the resolved slot ID.

    - `version`: `string` (optional)

      The variable name to store the resolved major version number.

  - `audiences`: `object` (optional)

    A map of audience IDs to variable names.

    Each value is the variable name to store the resolved audience ID.

  - `experiences`: `object` (optional)

    A map of zero-based indexes to variable names.

    Each value is the variable name to store the experience ID.

  - `experiments`: `object` (optional)

    A map of zero-based indexes to variable names.

    Each value is the variable name to store the experiment ID.

## Explore

- [Add component](/reference/cli/templates/actions/add-component): Learn how to install the created component.
- [Add slot](/reference/cli/templates/actions/add-slot): Learn how to add the created slot to the project.
- [Create API key](/reference/cli/templates/actions/create-api-key): Learn how to create an API key for the resource.
- [Integrate Croct](/reference/cli/templates/actions/integrate-croct): Learn how to set up the Croct integration.
