You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sources/platform/actors/development/programming_interface/environment_variables.md
+80-22Lines changed: 80 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,21 @@ import TabItem from '@theme/TabItem';
14
14
15
15
---
16
16
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
+
17
32
## System environment variables
18
33
19
34
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:
53
68
|`APIFY_DEDICATED_CPUS`| Number of CPU cores reserved for the actor, based on allocated memory. |
54
69
|`APIFY_DISABLE_OUTDATED_WARNING`| Controls the display of outdated version warnings. Set to `1` to suppress notifications about updates. |
55
70
|`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.|
57
72
|`APIFY_SDK_LATEST_VERSION`| Specifies the most recent release version of the Apify SDK for JavaScript. Used for checking for updates. |
58
73
|`APIFY_INPUT_SECRETS_KEY_FILE`| Path to the secret key used to decrypt [Secret inputs](/platform/actors/development/actor-definition/input-schema/secret-input). |
59
74
|`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
68
83
:::
69
84
<!-- vale Microsoft.RangeFormat = YES -->
70
85
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
+
71
127
## Access environment variables
72
128
73
129
You can access environment variables in your code as follows:
@@ -78,7 +134,17 @@ You can access environment variables in your code as follows:
78
134
In Node.js, use the `process.env` object:
79
135
80
136
```js
81
-
console.log(process.env.APIFY_USER_ID);
137
+
import { Actor } from'apify';
138
+
139
+
awaitActor.init();
140
+
141
+
// get MYSQL_USER
142
+
constmysql_user=process.env.MYSQL_USER
143
+
144
+
// print MYSQL_USER to console
145
+
console.log(mysql_user);
146
+
147
+
awaitActor.exit();
82
148
```
83
149
84
150
</TabItem>
@@ -88,7 +154,17 @@ In Python, use the `os.environ` dictionary:
88
154
89
155
```python
90
156
import os
91
-
print(os.environ['APIFY_USER_ID'])
157
+
print(os.environ['MYSQL_USER'])
158
+
159
+
from apify import Actor
160
+
161
+
asyncdefmain():
162
+
asyncwith Actor:
163
+
# get MYSQL_USER
164
+
mysql_user = os.environ['MYSQL_USER']
165
+
166
+
# print MYSQL_USER to console
167
+
print(mysql_user)
92
168
```
93
169
94
170
</TabItem>
@@ -135,24 +211,6 @@ async def main():
135
211
</TabItem>
136
212
</Tabs>
137
213
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
-
156
214
## Build-time environment variables
157
215
158
216
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
162
220
RUN echo $MY_BUILD_VARIABLE
163
221
```
164
222
165
-
:::caution Insecure build variables
223
+
:::caution Variables set during the build
166
224
167
225
Build-time environment variables are not suitable for secrets, as they are not encrypted.
0 commit comments