Environments and migrations > Configuring the CLI

    Configuring the CLI

    First of all, you need to install the DatoCMS CLI with the following command:

    npm install @datocms/cli

    You can verify everything is correctly installed by running the help command of the CLI:

    npx datocms --help

    Setting up a CLI profile

    Although not strictly necessary, we strongly suggest to generate a datocms.config.js configuration file in the root of your website/app project to avoid having to repeat a number of options for every command you run.

    You can configure the profile with the profile:set command — and re-run the same command at a later time if you need to make some change:

    $ npx datocms profile:set
    Config file not present in "datocms.config.json", will be created from scratch
    Requested to configure profile "default"
    * Level of logging to use for the profile (NONE, BASIC, BODY, BODY_AND_HEADERS) [NONE]:
    * Directory where script migrations will be stored [./migrations]:
    * API key of the DatoCMS model used to store migration data [schema_migration]:
    * Path of the file to use as migration script template (optional):
    Writing "datocms.config.json"... done

    Most of the time, you can just simply confirm the default values suggested by the CLI. Once done, add the config file to your Git repository:

    git add datocms.config.json
    git commit -m "Add datocms.config.json file"
    Need to manage multiple DatoCMS projects from the same repo?

    You can setup additional profiles with the datocms profile:set <NEW_PROFILE_NAME> command.

    When you have multiple profiles, you can specify the profile to use to run a command with the --profile flag (or exposing a DATOCMS_PROFILE environment variable).

    Specify a DatoCMS API token

    Before working with migrations, you also need to set up the API token to use to perform all the operations.

    You can either pass it as a flag to every command, ie.:

    $ npx datocms migrations:run --api-token=<YOUR-API-TOKEN> [...]

    Or expose it as an environment variable:

    $ npx datocms migrations:run [...]

    The CLI also loads environment variables from a .env file, so you can also place the token there — but make sure not to commit the file to your repo!

    $ echo '.env' >> .gitignore
    $ echo 'DATOCMS_API_TOKEN=<YOUR-API-TOKEN>' >> .env