# Set Up Salesforce CLI for Code Extension

Salesforce CLI, the Code Extension plugin, and the Data Custom Code Python SDK work together as your local toolchain for Code Extension. Use this setup to develop and test custom Python scripts locally against a sandbox and then deploy them to Data 360 for execution.

| Edition Table                                                                                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Available in: **Developer**, **Enterprise**, **Performance**, and **Unlimited** Editions. See [Data 360 edition availability](https://help.salesforce.com/s/articleView?id=sf.c360_a_dc_editions.htm&type=5). |

| User Permissions Needed                                          |                                                        |
| ---------------------------------------------------------------- | ------------------------------------------------------ |
| To set up and use Salesforce CLI for code extension development: | Permission set: <ul><li>Data Cloud Architect</li></ul> |

## Prerequisites

- **Local machine**:
  - Operating system: macOS, Linux, or Windows
  - Python 3.11
    - macOS and Linux: We recommend using [`pyenv`](https://github.com/pyenv/pyenv) to install Python 3.11.
    - Windows: Install Python 3.11 from [Python.org](https://www.python.org/downloads/), the Microsoft Store, or use [pyenv-win](https://github.com/pyenv-win/pyenv-win). Depending on your install, run Python as `py -3.11` or `python` when creating a virtual environment.
  - Azul Zulu OpenJDK 17.x
  - Docker Desktop. On Windows, enable the WSL 2 backend for Dev Containers and other container workflows. See [Docker Desktop on Windows](https://docs.docker.com/desktop/setup/install/windows-install/).
- **Data 360:** Sandbox or org access with appropriate permission sets. See [Create a Data 360 Sandbox](https://help.salesforce.com/s/articleView?id=data.c360\_a_data_cloud_sandbox_create.htm\&type=5). Enable Code Extension in Feature Manager. See [Enable Data 360 Features in Feature Manager](https://help.salesforce.com/s/articleView?id=data.c360\_a_feature_manager.htm\&language=en_US\&type=5).

## Steps to Set Up Salesforce CLI in Your Local Machine

1. Install Salesforce CLI version `2.130.9` or later. See [Install Salesforce CLI](https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm).

2. Confirm that your CLI version is `2.130.9` or later.

   ```bash
   sf version
   ```

   If you have an earlier version, update CLI. See [Update Salesforce CLI](https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_update_cli.htm), and then run `sf version` to confirm.

3. On Windows, install [Node.js LTS](https://nodejs.org/en/download) and verify the installation.

   ```bash
   node --version
   npm --version
   ```

4. Install the Code Extension plugin.

   ```bash
   sf plugins install @salesforce/plugin-data-code-extension
   ```

5. Make sure that the installed Code Extension plugin version is `0.1.5` or later. If you have an earlier version, update the plugin to the latest available version.

   ```bash
   sf plugins
   sf plugins update
   ```

6. Install the Data Custom Code Python SDK.

   **macOS and Linux**

   ```bash
   python3.11 -m pip install salesforce-data-customcode
   ```

   **Windows**

   ```powershell
   py -3.11 -m pip install salesforce-data-customcode
   ```

7. (Optional) Upgrade the Data Custom Code Python SDK to the latest available version.

   **macOS and Linux**

   ```bash
   python3.11 -m pip install --upgrade salesforce-data-customcode
   ```

   **Windows**

   ```
   py -3.11 -m pip install --upgrade salesforce-data-customcode
   ```

8. Confirm the SDK installation.
   - macOS and Linux: `python3.11 -m pip show salesforce-data-customcode`
   - Windows: `py -3.11 -m pip show salesforce-data-customcode`

9. Log in to your org. From a terminal, run one of the commands with the flags for your environment. The CLI opens your default browser for Salesforce login. After you log in, the CLI saves the session on your computer.
   - Production or Developer Edition

     ```bash
     sf org login web --alias <myorg>
     ```

   - Sandbox

     ```bash
     sf org login web --alias <mysandbox> --instance-url https://test.salesforce.com
     ```

   - Custom domain

     ```bash
     sf org login web --alias <myorg> --instance-url https://mycompany.my.salesforce.com
     ```

10. After you log in, confirm which orgs the CLI is connected to and which alias you must use for code extension commands.

    ```bash
    sf org list
    sf org display --target-org <myorg>
    ```

    For example, `sf org display --target-org myorg` shows the connection status of your target org `myorg`.

    :::note
    Running the `sf org login` command is a one-time setup per environment or profile. Rerun this command only if your credentials or environment details change, or to set up a new profile.
    :::

11. Create a code extension script package.

    ```bash
    sf data-code-extension script init --package-dir <my_package>
    ```

    Replace `<my_package>` with the directory name for your project (for example, `my_package`).

    The code extension script package sets up the required directory structure, configuration files, and dependencies for writing, testing, and deploying code extension.

    ```
    my_package/
    ├── Dockerfile
    ├── requirements.txt
    ├── requirements-dev.txt
    ├── payload/
    │   ├── config.json
    │   └── entrypoint.py
    ├── examples/
    │   └── employee_hierarchy/
    │       ├── employee_data.csv
    │       └── entrypoint.py
    ├── jupyterlab.sh
    ├── .devcontainer/
    └── README.md
    ```

    This table summarizes the directories and files in the package.

    | Directory or File              | Description                                                                                                                                                                                                   |
    | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | `Dockerfile`                   | Used for containerized builds and deployments. Don't modify this file.                                                                                                                                        |
    | `requirements.txt`             | List the Python packages your script needs for deployment.                                                                                                                                                    |
    | `requirements-dev.txt`         | Python packages for local development and testing only. Don't modify this file.                                                                                                                               |
    | `payload/`                     | Contains the script and configuration that the CLI deploys to Data 360.                                                                                                                                       |
    | `payload/config.json`          | Data 360 deployment configuration for your code extension package.                                                                                                                                            |
    | `payload/entrypoint.py`        | Main Python file where you implement batch data transform logic.                                                                                                                                              |
    | `examples/`                    | Example scripts and sample data you can copy or learn from.                                                                                                                                                   |
    | `examples/employee_hierarchy/` | Sample that builds a directed acyclic graph (DAG) from employee hierarchy data. Upload `employee_data.csv`, create the DLOs the sample expects, then copy or run `entrypoint.py` as described in the example. |
    | `jupyterlab.sh`                | Shell script to start JupyterLab for interactive development.                                                                                                                                                 |
    | `.devcontainer/`               | Visual Studio Code Dev Container configuration for a consistent local environment.                                                                                                                            |
    | `README.md`                    | Package overview, setup notes, and how to run the sample in a container or locally.                                                                                                                           |

## Next Step

- [Write and Validate Custom Scripts](/docs/data/data-cloud-code-ext/guide/author-custom-script.md)
