<a id="confluent-cli-connect"></a>

# Connect Confluent CLI to Confluent Cloud Cluster

This topic includes the steps to connect your Confluent CLI instance to a
Confluent Cloud cluster. You must have a valid Confluent Cloud [user
account](/cloud/current/access-management/user-account.html) and an [install](install.md#cli-install)
of the Confluent CLI to complete the following steps.

<!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->
1. If you’re running Windows Subsystem for Linux (WSL), install
   [xdg-open-wsl](https://github.com/cpbotha/xdg-open-wsl).
2. In your terminal, start autocomplete by entering the following command:
   ```text
   confluent shell
   ```
3. Log in to Confluent Cloud using the `confluent login` command:
   ```text
   login --save
   ```

   Note that with the `--save` flag, Confluent will log you back in after your
   login session token expires (if inactive for one hour).
4. Set the Confluent Cloud [environment](/cloud/current/using/environments.html).
   - Get the environment ID.
     ```text
     environment list
     ```

     Your output should resemble:
     ```text
     Current |    ID       |      Name
     --------+-------------+-----------------
     *       | env-5q432q  | dev
             | env-4q985q  | prod
             | env-2q345q  | jdoe-gcp-env
             | env-9q012q  | jdoe-aws-env
     ```
   - Set the environment using the ID (`<env-id>`).
     ```text
     environment use <env-id>
     ```

     Your output should resemble:
     ```text
     Now using env-4q985q as the default (active) environment.
     ```
5. Set the cluster to use.
   - Get the cluster ID.
     ```text
     kafka cluster list
     ```

     Your output should resemble:
     ```text
           ID      |       Name        | Provider |   Region    | Durability | Status
     +-------------+-------------------+----------+-------------+------------+--------+
         ekg-rr8v7 | dev-aws-oregon    | aws      | us-west-2   | LOW        | UP
         ekg-q2j96 | prod              | gcp      | us-central1 | LOW        | UP
     ```
   - Set the cluster using the ID (`<cluster-id>`). This is the cluster where
     the commands are run.
     ```text
     kafka cluster use <cluster-id>
     ```
6. Create an API key and secret and save them locally. This is required to produce or
   consume to your topic.

   From the Confluent CLI, type the following command:
   ```text
   api-key create --resource <resource-id>
   ```

   Your output should resemble:
   ```text
   Save the API key and secret. The secret is not retrievable later.
   +---------+------------------------------------------------------------------+
   | API Key | LD35EM2YJTCTRQRM                                                 |
   | Secret  | 67JImN+9vk+Hj3eaj2/UcwUlbDNlGGC3KAIOy5JNRVSnweumPBUpW31JWZSBeawz |
   +---------+------------------------------------------------------------------+
   ```
7. (Optional) Store the API secret locally with `confluent api-key store <key>
   <secret>`. When you create an API key with the CLI, it is stored locally.
   However, when you create an API key using the UI, API, or with the CLI on
   another machine, the secret is not available for CLI use until you store it.
   This is required because secrets can’t be retrieved after creation.
   ```text
   api-key store <api-key> <api-secret> --resource <resource-id>
   ```
8. Set the API key to use for the Confluent CLI commands:
   ```text
   api-key use <api-key>
   ```

You can now run the Confluent CLI commands against the specified cluster.
