Skip to content

Commit 9c14e10

Browse files
docs: Improve Actors environment variables docs
1 parent c7109d4 commit 9c14e10

File tree

1 file changed

+80
-22
lines changed

1 file changed

+80
-22
lines changed

sources/platform/actors/development/programming_interface/environment_variables.md

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ import TabItem from '@theme/TabItem';
1414

1515
---
1616

17+
## How to use environment variables in an Actor
18+
19+
You can set up environment variables for your Actor in two ways:
20+
21+
- [Set up environment variables in `actor.json`](#set-up-environment-variables-in-actorjson)
22+
- [Set up environment variables in Apify Console](#set-up-environment-variables-in-apify-console)
23+
24+
:::info Environment variable precedence
25+
26+
Your local `.actor/actor.json` file overrides variables set in Apify Console. To use Console variables, remove the `environmentVariables` key from the local file.
27+
28+
:::
29+
30+
Check out how you can [access environment variables in Actors](#access-environment-variables).
31+
1732
## System environment variables
1833

1934
Apify sets several system environment variables for each Actor run. These variables provide essential context and information about the Actor's execution environment.
@@ -53,7 +68,7 @@ Here's a table of key system environment variables:
5368
| `APIFY_DEDICATED_CPUS` | Number of CPU cores reserved for the actor, based on allocated memory. |
5469
| `APIFY_DISABLE_OUTDATED_WARNING` | Controls the display of outdated version warnings. Set to `1` to suppress notifications about updates. |
5570
| `APIFY_WORKFLOW_KEY` | Identifier used for grouping related runs and API calls together. |
56-
| `APIFY_META_ORIGIN` | Specifies how an Actor run was started. Possible values are [here](/platform/actors/running/runs-and-builds#origin) |
71+
| `APIFY_META_ORIGIN` | Specifies how an Actor run was started. Possible values are in [Runs and builds](/platform/actors/running/runs-and-builds#origin) documentation. |
5772
| `APIFY_SDK_LATEST_VERSION` | Specifies the most recent release version of the Apify SDK for JavaScript. Used for checking for updates. |
5873
| `APIFY_INPUT_SECRETS_KEY_FILE` | Path to the secret key used to decrypt [Secret inputs](/platform/actors/development/actor-definition/input-schema/secret-input). |
5974
| `APIFY_INPUT_SECRETS_KEY_PASSPHRASE` | Passphrase for the input secret key specified in `APIFY_INPUT_SECRETS_KEY_FILE`. |
@@ -68,6 +83,47 @@ All date-related variables use the UTC timezone and are in [ISO 8601](https://en
6883
:::
6984
<!-- vale Microsoft.RangeFormat = YES -->
7085

86+
## Set up environment variables in `actor.json`
87+
88+
Actor owners can define custom environment variables in `.actor/actor.json`. All keys from `environmentVariables` will be set as environment variables into the Apify platform after you push Actor to Apify.
89+
90+
```json
91+
{
92+
"actorSpecification": 1,
93+
"name": "dataset-to-mysql",
94+
"version": "0.1",
95+
"buildTag": "latest",
96+
"environmentVariables": {
97+
"MYSQL_USER": "my_username",
98+
}
99+
}
100+
```
101+
102+
:::info Git-workflow with actor.json
103+
104+
Be aware that 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.
105+
106+
:::
107+
108+
## Set up environment variables in Apify Console
109+
110+
Actor owners can define custom environment variables to pass additional configuration to their Actors. To set custom variables:
111+
112+
1. Go to your Actor's **Source** page in the Apify Console
113+
114+
1. Navigate to the **Environment variables** section.
115+
116+
1. Add your custom variables.
117+
118+
For sensitive data like API keys or passwords, enable the **Secret** option. This will encrypt the value and redact it from logs to prevent accidental exposure.
119+
120+
:::info Build-time variables
121+
122+
Once you start a build, you cannot change its environment variables. To use different variables, you must create a new build.
123+
124+
Learn more in [Builds](../builds_and_runs/builds.md).
125+
:::
126+
71127
## Access environment variables
72128

73129
You can access environment variables in your code as follows:
@@ -78,7 +134,17 @@ You can access environment variables in your code as follows:
78134
In Node.js, use the `process.env` object:
79135

80136
```js
81-
console.log(process.env.APIFY_USER_ID);
137+
import { Actor } from 'apify';
138+
139+
await Actor.init();
140+
141+
// get MYSQL_USER
142+
const mysql_user = process.env.MYSQL_USER
143+
144+
// print MYSQL_USER to console
145+
console.log(mysql_user);
146+
147+
await Actor.exit();
82148
```
83149

84150
</TabItem>
@@ -88,7 +154,17 @@ In Python, use the `os.environ` dictionary:
88154

89155
```python
90156
import os
91-
print(os.environ['APIFY_USER_ID'])
157+
print(os.environ['MYSQL_USER'])
158+
159+
from apify import Actor
160+
161+
async def main():
162+
async with Actor:
163+
# get MYSQL_USER
164+
mysql_user = os.environ['MYSQL_USER']
165+
166+
# print MYSQL_USER to console
167+
print(mysql_user)
92168
```
93169

94170
</TabItem>
@@ -135,24 +211,6 @@ async def main():
135211
</TabItem>
136212
</Tabs>
137213

138-
## Custom environment variables
139-
140-
Actor owners can define custom environment variables to pass additional configuration to their Actors. To set custom variables:
141-
142-
1. Go to your Actor's **Source** page in the Apify Console
143-
144-
1. Navigate to the **Environment variables** section.
145-
146-
1. Add your custom variables.
147-
148-
For sensitive data like API keys or passwords, enable the **Secret** option. This encrypt the value and redacts it from logs to prevent accidental exposure.
149-
150-
:::info Build-time variables
151-
152-
Custom environment variables are set during the Actor's build process and cannot be changed for existing builds. For more information, check out the [Builds](../builds_and_runs/builds.md) page.
153-
154-
:::
155-
156214
## Build-time environment variables
157215

158216
You can also use environment variables during the Actor's build process. In this case, they function as Docker build arguments. To use them in your Dockerfile, include `ARG` instruction:
@@ -162,7 +220,7 @@ ARG MY_BUILD_VARIABLE
162220
RUN echo $MY_BUILD_VARIABLE
163221
```
164222

165-
:::caution Insecure build variables
223+
:::caution Variables set during the build
166224

167225
Build-time environment variables are not suitable for secrets, as they are not encrypted.
168226

0 commit comments

Comments
 (0)