The CLI is designed for a variety of secret management applications ranging from local development to CI/CD and production scenarios.
Local development
Staging, production & all other use cases
In the following steps, we explore how to use the Infisical CLI to fetch back environment variables from Infisical
and inject them into your local development process.
If you prefer learning by watching, you can follow along our step-by-step video tutorial here.
1
Log in with the CLI
Start by running the infisical login command to authenticate with Infisical.
Copy
Ask AI
infisical login
If you are in a containerized environment such as WSL 2 or Codespaces, run infisical login -i to avoid browser based login
2
Initialize Infisical for your project
Next, navigate to your project and initialize Infisical.
Copy
Ask AI
# navigate to your projectcd /path/to/project# initialize infisicalinfisical init
The infisical init command creates a .infisical.json file, containing local project settings, at the location where the command is executed.
The .infisical.json file does not contain any sensitive data, so you may commit it to your git repository.
3
Inject environment variables
Finally, pass environment variables from Infisical into your application.
Feed secrets to your application
Feed secrets via custom aliases (advanced)
Copy
Ask AI
infisical run --env=dev --path=/apps/firefly -- [your application start command] # e.g. npm run dev# example with node (nodemon)infisical run --env=staging --path=/apps/spotify -- nodemon index.js# example with flaskinfisical run --env=prod --path=/apps/backend -- flask run# example with spring boot - maveninfisical run --env=dev --path=/apps/ -- ./mvnw spring-boot:run --quiet
Custom aliases can utilize secrets from Infisical. Suppose there is a custom alias yd in custom.sh that runs yarn dev and needs the secrets provided by Infisical.
Copy
Ask AI
#!/bin/shyd() { yarn dev}
To make the secrets available from Infisical to yd, you can run the following command:
Copy
Ask AI
infisical run --env=prod --path=/apps/reddit --command="source custom.sh && yd"
In the following steps, we explore how to use the Infisical CLI in a non-local development scenario
to fetch back environment variables and export them to a file.
1
Create a machine identity and obtain credentials for it
Follow the steps listed here to create a machine identity and obtain a client ID and client secret for it.
2
Obtain a machine identity access token
Run the following command to authenticate with Infisical using the client ID and client secret credentials from step 1 and set the INFISICAL_TOKEN environment variable to the retrieved access token.
Copy
Ask AI
export INFISICAL_TOKEN=$(infisical login --method=universal-auth --client-id=<identity-client-id> --client-secret=<identity-client-secret> --silent --plain) # --plain flag will output only the token, so it can be fed to an environment variable. --silent will disable any update messages.
The CLI is configured to look out for the INFISICAL_TOKEN environment variable, so going forward any command used will be authenticated.Alternatively, assuming you have an access token on hand, you can also pass it directly to the CLI using the --token flag in conjunction with other CLI commands.
Keep in mind that the machine identity access token has a limited lifetime. It is recommended to use it only for the duration of the task at hand.
You can refresh the token if needed.
3
Export environment variables back into a file
Finally, export the environment variables from Infisical to a file of choice.
Copy
Ask AI
# export variables to a .env file (with export keyword)infisical export --format=dotenv-export > .env# export variables to a YAML fileinfisical export --format=yaml > secrets.yaml
Starting with CLI version v0.4.0, you can now choose to log in via Infisical Cloud (US/EU) or your own self-hosted instance by simply running infisical login and following the on-screen instructions — no need to manually set the INFISICAL_API_URL environment variable.For versions prior to v0.4.0, the CLI defaults to US Cloud. To connect to EU Cloud or a self-hosted instance, set the INFISICAL_API_URL environment variable to https://eu.infisical.com or your custom URL.
Important: If you’re not using interactive login, you must configure the domain for all CLI commands.The CLI defaults to US Cloud (https://app.infisical.com). To connect to EU Cloud (https://eu.infisical.com) or a self-hosted instance, you must configure the domain in one of the following ways:
Use the INFISICAL_API_URL environment variable
Use the --domain flag on every command
Use Environment Variable (Recommended)
Use --domain Flag
The easiest way to ensure all CLI commands use the correct domain is to set
the INFISICAL_API_URL environment variable. This applies the domain
setting globally to all commands:
Copy
Ask AI
# Linux/MacOSexport INFISICAL_API_URL="https://your-domain.infisical.com"# Windows PowerShellsetx INFISICAL_API_URL "https://your-domain.infisical.com"
Once set, all subsequent CLI commands will automatically use this domain:
Copy
Ask AI
# Login with the domaininfisical login --method=universal-auth --client-id=<client-id> --client-secret=<client-secret> --silent --plain# All other commands will also use the same domain automaticallyinfisical secrets --projectId <id> --env dev
The --domain flag can be used to set the domain for a single command. This
applies the domain setting to the command only:
Copy
Ask AI
# Login with domaininfisical login --domain="https://your-domain.infisical.com" --method=universal-auth --client-id=<client-id> --client-secret=<client-secret> --silent --plain# All subsequent commands must also include --domaininfisical secrets --domain="https://your-domain.infisical.com" --projectId=<id> --env=dev
If you use --domain during login but forget to include it on subsequent commands, you may encounter authentication errors.
The Infisical CLI supports custom HTTP headers for requests to servers protected by authentication services such as Cloudflare Access. Configure these headers using the INFISICAL_CUSTOM_HEADERS environment variable:
Copy
Ask AI
# Syntax: headername1=headervalue1 headername2=headervalue2export INFISICAL_CUSTOM_HEADERS="Access-Client-Id=your-client-id Access-Client-Secret=your-client-secret"# Execute Infisical commands after setting the environment variableinfisical secrets
This functionality enables secure interaction with Infisical instances that require specific authentication headers.
Your terminal keeps a history with the commands you run. When you create Infisical secrets directly from your terminal, they’ll stay there for a while.For security and privacy concerns, we recommend you to configure your terminal to ignore those specific Infisical commands.
Ignore commands
Unix/Linux
Windows
$HOME/.profile is pretty common but, you could place it under $HOME/.profile.d/infisical.sh or any profile file run at login
Can I connect the CLI to my self-hosted or non-US Cloud Infisical instance?
Yes. The CLI is set to connect to Infisical US Cloud by default, but if you’re using EU Cloud or a self-hosted instance you can configure the domain for all CLI commands.
Beginning with CLI version V0.4.0, you can choose between logging in through Infisical US Cloud, EU Cloud, or your own self-hosted instance. Simply execute the infisical login command and follow the on-screen instructions.
You can point the CLI to the self-hosted Infisical instance by exporting the environment variable INFISICAL_API_URL in your terminal.
Linux/MacOs
Windows Powershell
Copy
Ask AI
# Set the API URLexport INFISICAL_API_URL="https://your-self-hosted-infisical.com"# For EU Cloudexport INFISICAL_API_URL="https://eu.infisical.com"# Remove the settingunset INFISICAL_API_URL
Copy
Ask AI
# Set the API URLsetx INFISICAL_API_URL "https://your-self-hosted-infisical.com"# For EU Cloudsetx INFISICAL_API_URL "https://eu.infisical.com"# Remove the settingsetx INFISICAL_API_URL ""# NOTE: Once set, please restart powershell for the change to take effect
If you prefer not to use an environment variable, you must include the --domain flag on every CLI command you run:
Copy
Ask AI
# Login with domaininfisical login --domain="https://your-domain.infisical.com" --method=oidc-auth --jwt $JWT# All subsequent commands must also include --domaininfisical secrets --domain="https://your-self-hosted-infisical.com" --projectId <id> --env devinfisical export --domain="https://your-self-hosted-infisical.com" --format=dotenv-export
Best Practice: Use INFISICAL_API_URL environment variable (Method 2) to avoid having to remember the --domain flag on every command. This is especially important in CI/CD pipelines and automation scripts.
Can I use the CLI with service tokens?
To use Infisical for non local development scenarios, please create a service token. The service token will allow you to authenticate and interact with Infisical. Once you have created a service token with the required permissions, you’ll need to feed the token to the CLI.
Copy
Ask AI
infisical export --token=<service-token> infisical secrets --token=<service-token> infisical run --token=<service-token> -- npm run dev