# create-api-key

Learn how to create an API key.

This action creates an API key for the configured application and stores the secret in a variable.

> **Prerequisite**
>
> The project must be [initialized](/reference/cli/commands/init) with an application linked to the target environment.

## Example

The following template creates a development API key and writes it to an environment file:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "API key setup",
  "description": "Creates an API key and saves it to .env.",
  "actions": [
    {
      "name": "create-api-key",
      "keyName": "Development key",
      "environment": "development",
      "permissions": ["resource_read_access"],
      "result": "apiKey"
    },
    {
      "name": "write-file",
      "path": ".env.local",
      "content": "CROCT_API_KEY=${this.apiKey}\n"
    }
  ]
}
```

To apply this template, run:

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

## Properties

These are the supported properties:

- `name`: `string`

  The action identifier. Always `create-api-key` for this action.

- `keyName`: `string`

  The display name shown in the dashboard for this API key.

- `environment`: `string`

  The environment to create the key for.

  Valid values are `development` and `production`.

- `permissions`: `Array<string>`

  The list of permissions to grant to the API key:

  | Permission             | Description                                         |
  | ---------------------- | --------------------------------------------------- |
  | `resource_read_access` | Allows reading resources such as content and slots. |
  | `token_issue`          | Allows issuing authentication tokens.               |
  | `data_export`          | Allows exporting analytics and event data.          |

- `result`: `string`

  The variable name to store the generated API key secret.

## Explore

- [Create resource](/reference/cli/templates/actions/create-resource): Learn how to create components and slots in a workspace.
- [Write file](/reference/cli/templates/actions/write-file): Learn how to write the API key to a config file.
- [Integrate Croct](/reference/cli/templates/actions/integrate-croct): Learn how to set up the Croct integration.
- [Define](/reference/cli/templates/actions/define): Learn how to store the key in a variable for reuse.
