From 37a8b56d1413d4b84081bb9fbf36ddd09add472d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hanu=C5=A1?= Date: Sun, 22 Nov 2026 10:45:57 +0100 Subject: [PATCH 1/2] docs: describe the new env var model for Actors --- .../actor_definition/actor_json.md | 11 +-- .../environment_variables.md | 97 +++++++++++++------ 2 files changed, 70 insertions(+), 38 deletions(-) diff --git a/sources/platform/actors/development/actor_definition/actor_json.md b/sources/platform/actors/development/actor_definition/actor_json.md index 1a3493f883..2a3d52d35f 100644 --- a/sources/platform/actors/development/actor_definition/actor_json.md +++ b/sources/platform/actors/development/actor_definition/actor_json.md @@ -6,7 +6,7 @@ slug: /actors/development/actor-definition/actor-json sidebar_position: 1 --- -Your main Actor configuration is in the `.actor/actor.json` file at the root of your Actor's directory. This file links your local development project to an Actor on the Apify platform. It should include details like the Actor's name, version, build tag, and environment variables. Make sure to commit this file to your Git repository. +Your main Actor configuration is in the `.actor/actor.json` file at the root of your Actor's directory. This file links your local development project to an Actor on the Apify platform. It should include details like the Actor's name, version, and build tag. Make sure to commit this file to your Git repository. For example, the `.actor/actor.json` file can look like this: @@ -30,10 +30,6 @@ import TabItem from '@theme/TabItem'; "defaultMemoryMbytes": "get(input, 'startUrls.length', 1) * 1024", "minMemoryMbytes": 256, "maxMemoryMbytes": 4096, - "environmentVariables": { - "MYSQL_USER": "my_username", - "MYSQL_PASSWORD": "@mySecretPassword" - }, "usesStandbyMode": false, "dockerfile": "./Dockerfile", "readme": "./ACTOR.md", @@ -66,7 +62,7 @@ import TabItem from '@theme/TabItem'; :::note Deployment metadata -Actor `name`, `version`, `buildTag`, and `environmentVariables` are currently only used when you deploy your Actor using the [Apify CLI](/cli) and not when deployed, for example, via GitHub integration. There, it serves for informative purposes only. +Actor `name`, `version`, `buildTag`, and `applyEnvVarsToBuild` are currently only used when you deploy your Actor using the [Apify CLI](/cli) and not when deployed, for example, via GitHub integration. There, it serves for informative purposes only. ::: @@ -81,7 +77,8 @@ Path values are relative to the `.actor/` directory, so `../` points to the Acto | `version` | Required | The version of the Actor, specified in the format `[Number].[Number]`, e.g., `0.1`, `0.3`, `1.0`, `1.3`, etc. | | `buildTag` | Optional | The tag name to apply to a successful build of the Actor. If not specified, builds of version `0.0` get the `latest` tag, and builds of other versions get no tag. For details, see [Actor builds](../builds_and_runs/builds.md). | | `meta` | Optional | Metadata object containing additional information about the Actor. Currently supports `templateId` field to identify the template from which the Actor was created. | -| `environmentVariables` | Optional | A map of environment variables to be used during local development. These variables will also be applied to the Actor when deployed on the Apify platform. For more details, see the [environment variables](/cli/docs/vars) section of the Apify CLI documentation. | +| `environmentVariables` | Optional | _Deprecated_ - A map of environment variables for local runs with `apify run`. `apify push` doesn't upload them to the Apify platform. Keep local values in a `.env` file instead, and run `apify env migrate` to move them there. Read more about [custom environment variables](/actors/development/programming-interface/environment-variables#custom-environment-variables). | +| `applyEnvVarsToBuild` | Optional | If `true`, `apify push` makes the custom environment variables of the Actor version also available to the build process. If omitted, the setting stored on the platform is kept. Read more about [build-time environment variables](/actors/development/programming-interface/environment-variables#build-time-environment-variables). | | `dockerfile` | Optional | The path to the Dockerfile to be used for building the Actor on the platform. If not specified, the system will search for Dockerfiles in the `.actor/Dockerfile` and `Dockerfile` paths, in that order. Refer to the [Dockerfile](./docker.md) section for more information. | | `dockerContextDir` | Optional | The path to the directory to be used as the Docker context when building the Actor. The path is relative to the location of the `actor.json` file. This property is useful for monorepos containing multiple Actors. Refer to the [Actor monorepos](../deployment/source_types.md#actor-monorepos) section for more details. | | `readme` | Optional | The path to the README file to be used on the platform. If not specified, the system will look for README files in the `.actor/README.md` and `README.md` paths, in that order of preference. For details, see [Create an Actor README](/actors/publishing/actor-readme). | diff --git a/sources/platform/actors/development/programming_interface/environment_variables.md b/sources/platform/actors/development/programming_interface/environment_variables.md index b0677e88ca..a76fcecab4 100644 --- a/sources/platform/actors/development/programming_interface/environment_variables.md +++ b/sources/platform/actors/development/programming_interface/environment_variables.md @@ -1,6 +1,6 @@ --- title: Actor environment variables -description: Learn how Actors receive runtime context from system environment variables set by the Apify platform and custom variables you define. +description: Access the system environment variables that the Apify platform sets for Actor runs, and manage custom variables on the platform and in .env files. slug: /actors/development/programming-interface/environment-variables sidebar_position: 3 sidebar_label: Environment variables @@ -12,7 +12,7 @@ import TabItem from '@theme/TabItem'; Actor runs get their environment variables from two sources: - [System environment variables](#system-environment-variables) - set automatically by the Apify platform for each Actor run. -- [Custom environment variables](#custom-environment-variables) - defined by the Actor owner, either in `.actor/actor.json` or in Apify Console. +- [Custom environment variables](#custom-environment-variables) - defined by the Actor owner on the Actor version, and in a `.env` file for local runs. By default, both kinds of variables are passed only to the Actor **run**, not to the **build**. For variables passed to the build process (Docker build arguments), see [Build-time environment variables](#build-time-environment-variables). @@ -24,7 +24,7 @@ Apify sets several system environment variables for each Actor run. They come in :::info Runtime only -System variables apply only to Actor runs and are never passed to builds - not even when **Apply environment variables also to the build process** is enabled in **Code** > **Environment variables**. That option forwards only your custom variables. +System variables apply only to Actor runs and are never passed to builds - not even when **Apply environment variables also to the build process** is enabled in **Source** > **Code** > **Environment variables**. That option forwards only your custom variables. ::: @@ -90,49 +90,84 @@ Variables prefixed with `APIFY_` are Apify-platform-specific extensions that go ## Custom environment variables -Actor owners can define custom environment variables to pass additional configuration to their Actors. +Actor owners define custom environment variables to pass configuration and secrets to their Actors. Each environment has one source of truth: -- [Define in `actor.json`](#define-in-actorjson) -- [Define in Apify Console](#define-in-apify-console) +- Apify platform - the variables stored on the Actor version, which you set in [Apify Console](#set-in-apify-console), with the [Apify CLI](#set-with-the-apify-cli), or with the [Apify MCP server](#set-with-the-apify-mcp-server) +- Local runs - the [`.env` file](#local-variables-in-env) in the Actor directory -Your local `.actor/actor.json` file overrides variables set in Apify Console. -To use Console variables, remove the `environmentVariables` key from the local file. +Deploying code doesn't change the platform variables. Neither `apify push` nor a [Git repository source](/actors/development/deployment/source-types) sends them. Each build copies the variables of its Actor version, so after you change a variable, [build the Actor](/actors/development/builds-and-runs/builds) again for new runs to get the new value. -### Define in `actor.json` +### Set in Apify Console -Actor owners can define custom environment variables in [`.actor/actor.json`](/actors/development/actor-definition/actor-json). All keys from `environmentVariables` will be set as environment variables into the Apify platform after you push Actor to Apify. +1. Open your Actor in [Apify Console](https://console.apify.com/actors) and go to **Source** > **Code** > **Environment variables**. +1. Add your variables. Enable **Secret** for sensitive values. +1. Save the changes and build the Actor. -```json -{ - "actorSpecification": 1, - "name": "dataset-to-mysql", - "version": "0.1", - "buildTag": "latest", - "environmentVariables": { - "MYSQL_USER": "my_username" - } -} +### Set with the Apify CLI + +The `apify env` commands of the [Apify CLI](/cli) manage the variables of the Actor defined in `.actor/actor.json`, on the version from its `version` field. To target another Actor or version, use the `--actor` and `--version` flags. + +| Command | Description | +| --- | --- | +| `apify env ls` | List the variables of the Actor version. Secret values stay hidden. | +| `apify env set [--secret]` | Create or update one variable. The value comes from a prompt or from standard input, never from an argument. | +| `apify env rm ` | Delete one variable. | +| `apify env push` | Upload the variables from `.env`. Shows the changes and asks before it applies them. New variables are secret unless you pass `--plain`, and `--prune` also deletes variables that aren't in `.env`. | +| `apify env pull` | Write the platform variables into `.env`. Secret variables get an empty value. | +| `apify env diff` | Compare `.env` with the platform, and exit with code 1 when they differ. Use it in pre-commit hooks and CI. | + +To set a secret without putting its value in the command, let the CLI prompt for it, or pipe it to standard input: + +```bash +apify env set MYSQL_PASSWORD --secret +printf '%s' "$MYSQL_PASSWORD" | apify env set MYSQL_PASSWORD --secret ``` -:::info Git-workflow with actor.json +For all options, read the [environment variables guide](/cli/docs/vars) of the Apify CLI. -If you define `environmentVariables` in `.actor/actor.json`, it only works with [Apify CLI](/cli). If you use a Git workflow for Actor development, the environment variables will not be set from `.actor/actor.json` and you need to define them in Apify Console. +### Set with the Apify MCP server -::: +The [Apify MCP server](/mcp#tool-selection) lets AI agents manage the variables with the tools of its `env-vars` category. The category is off by default, so add it to the `tools` parameter of the server URL, for example `https://mcp.apify.com?tools=actors,docs,env-vars`. + +| Tool | Description | +| --- | --- | +| `get-actor-env-var-list` | List the variables of an Actor version, with the values of plain variables | +| `set-actor-env-var` | Create or update one variable, plain or secret | +| `delete-actor-env-var` | Delete one variable | -### Define in Apify Console +The server never returns secret values and keeps the values you set out of its logs. -To set custom variables in Apify Console: +### Local variables in `.env` -1. Go to your Actor's **Source** page in Apify Console. +For local runs, keep the values in a `.env` file in the Actor directory, next to `.actor/`: -1. Navigate to the **Environment variables** section. +```text +MYSQL_USER=my_username +MYSQL_PASSWORD=my_password +``` -1. Add your custom variables. +- `apify run` loads `.env`. Its values are literal and override variables from your shell and the ones the CLI sets for the run. +- `apify push` never uploads `.env` or `.env.*` files, except `.env.example`. +- `apify init` and `apify create` add `.env` to `.gitignore`. + +To fill `.env` from the platform, run `apify env pull`. To upload it, run `apify env push`. + +### Move from `environmentVariables` in `actor.json` + +Earlier versions of the Apify CLI uploaded `environmentVariables` from `.actor/actor.json` on every `apify push`, which replaced the variables set in Apify Console. `environmentVariables`, `@secret` references, and the `apify secrets` command are deprecated. The values apply to local runs only, and `apify push` warns when `actor.json` contains them. + +To move an existing project, run: + +```bash +# Moves the values into .env and resolves @secret references +apify env migrate +# Uploads .env to the Actor version +apify env push +``` ### Secure environment variables -For sensitive data such as API keys or passwords, enable the **Secret** option when defining a variable in Apify Console. Secret values are encrypted at rest and redacted from Actor run logs to prevent accidental exposure. +For sensitive data such as API keys or passwords, mark the variable as secret: enable **Secret** in Apify Console, or pass `--secret` to `apify env set`. Secret values are encrypted at rest and redacted from Actor run logs. They're write-only: once saved, Apify Console, the Apify API, the Apify CLI, and the Apify MCP server never return them. :::caution Visibility of environment variables in public Actors @@ -240,11 +275,11 @@ ARG MY_BUILD_VARIABLE RUN echo $MY_BUILD_VARIABLE ``` -To pass your user-defined environment variables to the build, enable **Apply environment variables also to the build process** in your Actor's **Code** > **Environment variables** section in Apify Console. +To pass your custom environment variables to the build, enable **Apply environment variables also to the build process** in **Source** > **Code** > **Environment variables** in Apify Console. With the Apify CLI, set `"applyEnvVarsToBuild": true` in `.actor/actor.json`, or pass `--apply-env-vars-to-build` to `apify push`. If you use neither, `apify push` keeps the setting stored on the platform. :::caution Only user-defined variables are passed to the build -Even with **Apply environment variables also to the build process** enabled, only the variables you define in the Actor's **Environment variables** section are forwarded to the build. The Apify [system environment variables](#system-environment-variables) (such as `ACTOR_RUN_ID`, `APIFY_TOKEN`, or `ACTOR_DEFAULT_DATASET_ID`) are never available at build time, since the build is not associated with a specific run. +Even with **Apply environment variables also to the build process** enabled, only the custom variables stored on the Actor version are forwarded to the build. The Apify [system environment variables](#system-environment-variables) (such as `ACTOR_RUN_ID`, `APIFY_TOKEN`, or `ACTOR_DEFAULT_DATASET_ID`) are never available at build time, since the build is not associated with a specific run. ::: From 650dc0d2bfb000321f9b69f861a9edad94f1eb5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hanu=C5=A1?= Date: Sun, 22 Nov 2026 10:47:53 +0100 Subject: [PATCH 2/2] docs: tighten env var CLI and MCP details Describe the version fallback and --dir targeting, empty-value handling in apify env push and pull, that the MCP tools parameter replaces the default selection, what apify env migrate does to actor.json, and the MCP way to mark a variable secret. --- .../programming_interface/environment_variables.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sources/platform/actors/development/programming_interface/environment_variables.md b/sources/platform/actors/development/programming_interface/environment_variables.md index a76fcecab4..9ee604dd61 100644 --- a/sources/platform/actors/development/programming_interface/environment_variables.md +++ b/sources/platform/actors/development/programming_interface/environment_variables.md @@ -105,15 +105,15 @@ Deploying code doesn't change the platform variables. Neither `apify push` nor a ### Set with the Apify CLI -The `apify env` commands of the [Apify CLI](/cli) manage the variables of the Actor defined in `.actor/actor.json`, on the version from its `version` field. To target another Actor or version, use the `--actor` and `--version` flags. +The `apify env` commands of the [Apify CLI](/cli) manage the variables of the Actor defined in `.actor/actor.json`, on the version from its `version` field. If `version` is missing, they use version `0.0`. To target another Actor or version, use the `--actor` and `--version` flags. In a monorepo, pick the Actor directory with `--dir`. | Command | Description | | --- | --- | | `apify env ls` | List the variables of the Actor version. Secret values stay hidden. | | `apify env set [--secret]` | Create or update one variable. The value comes from a prompt or from standard input, never from an argument. | | `apify env rm ` | Delete one variable. | -| `apify env push` | Upload the variables from `.env`. Shows the changes and asks before it applies them. New variables are secret unless you pass `--plain`, and `--prune` also deletes variables that aren't in `.env`. | -| `apify env pull` | Write the platform variables into `.env`. Secret variables get an empty value. | +| `apify env push` | Upload the variables from `.env`. Shows the changes and asks before it applies them. New variables are secret unless you pass `--plain`, keys with an empty value are skipped, and `--prune` also deletes variables that aren't in `.env`. | +| `apify env pull` | Write the platform variables into `.env`. Secret variables get an empty value, or keep the value already in `.env`. | | `apify env diff` | Compare `.env` with the platform, and exit with code 1 when they differ. Use it in pre-commit hooks and CI. | To set a secret without putting its value in the command, let the CLI prompt for it, or pipe it to standard input: @@ -127,7 +127,7 @@ For all options, read the [environment variables guide](/cli/docs/vars) of the A ### Set with the Apify MCP server -The [Apify MCP server](/mcp#tool-selection) lets AI agents manage the variables with the tools of its `env-vars` category. The category is off by default, so add it to the `tools` parameter of the server URL, for example `https://mcp.apify.com?tools=actors,docs,env-vars`. +The [Apify MCP server](/mcp#tool-selection) lets AI agents manage the variables with the tools of its `env-vars` category. The category is off by default. The `tools` parameter of the server URL replaces the default selection, so list `env-vars` with the other tools you use, for example `https://mcp.apify.com?tools=actors,docs,env-vars`. | Tool | Description | | --- | --- | @@ -159,7 +159,7 @@ Earlier versions of the Apify CLI uploaded `environmentVariables` from `.actor/a To move an existing project, run: ```bash -# Moves the values into .env and resolves @secret references +# Moves the values into .env, resolves @secret references, and removes environmentVariables from actor.json apify env migrate # Uploads .env to the Actor version apify env push @@ -167,7 +167,7 @@ apify env push ### Secure environment variables -For sensitive data such as API keys or passwords, mark the variable as secret: enable **Secret** in Apify Console, or pass `--secret` to `apify env set`. Secret values are encrypted at rest and redacted from Actor run logs. They're write-only: once saved, Apify Console, the Apify API, the Apify CLI, and the Apify MCP server never return them. +For sensitive data such as API keys or passwords, mark the variable as secret: enable **Secret** in Apify Console, pass `--secret` to `apify env set`, or set `isSecret` with the `set-actor-env-var` MCP tool. Secret values are encrypted at rest and redacted from Actor run logs. They're write-only: once saved, Apify Console, the Apify API, the Apify CLI, and the Apify MCP server never return them. :::caution Visibility of environment variables in public Actors