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
+74-22Lines changed: 74 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 Actors
18
+
19
+
There are two ways how you can set up environment variables for Actors:
20
+
21
+
-[Set up environment variables in `actor.json`](#set-up-environment-variables-in-actoractorjson)
22
+
-[Set up environment variables in Apify Console](#set-up-environment-variables-in-apify-console)
23
+
24
+
:::info Environment variables overwrite
25
+
26
+
After setting up variables in Apify Console, remove the `environmentVariables` from `.actor/actor.json`. Otherwise, variables from `.actor/actor.json` will override variables in Apify Console.
27
+
28
+
:::
29
+
30
+
See how you can [access environment variables in Actors](#actor-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.
@@ -68,6 +83,41 @@ 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 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
+
"MYSQL_PASSWORD": "@mySecretPassword"
99
+
}
100
+
}
101
+
```
102
+
103
+
## Set up environment variables in Apify Console
104
+
105
+
Actor owners can define custom environment variables to pass additional configuration to their Actors. To set custom variables:
106
+
107
+
1. Go to your Actor's **Source** page in the Apify Console
108
+
109
+
1. Navigate to the **Environment variables** section.
110
+
111
+
1. Add your custom variables.
112
+
113
+
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.
114
+
115
+
:::info Build-time variables
116
+
117
+
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.
118
+
119
+
:::
120
+
71
121
## Access environment variables
72
122
73
123
You can access environment variables in your code as follows:
@@ -78,7 +128,17 @@ You can access environment variables in your code as follows:
78
128
In Node.js, use the `process.env` object:
79
129
80
130
```js
81
-
console.log(process.env.APIFY_USER_ID);
131
+
import { Actor } from'apify';
132
+
133
+
awaitActor.init();
134
+
135
+
// get api_token
136
+
constapi_token=process.env.api_token
137
+
138
+
// print api_token to console
139
+
console.log(api_token);
140
+
141
+
awaitActor.exit();
82
142
```
83
143
84
144
</TabItem>
@@ -88,15 +148,25 @@ In Python, use the `os.environ` dictionary:
88
148
89
149
```python
90
150
import os
91
-
print(os.environ['APIFY_USER_ID'])
151
+
print(os.environ['api_token'])
152
+
153
+
from apify import Actor
154
+
155
+
asyncdefmain():
156
+
asyncwith Actor:
157
+
# get api_token
158
+
userId = os.environ['api_token']
159
+
160
+
# print api_token to console
161
+
print(userId)
92
162
```
93
163
94
164
</TabItem>
95
165
</Tabs>
96
166
97
-
## Use the `Configuration` class
167
+
## Access system environment variables
98
168
99
-
For more convenient access to Actor configuration, use the [`Configuration`](/sdk/js/reference/class/Configuration) class
169
+
For more convenient access to Actor system variables and other configuration, use the [`Configuration`](/sdk/js/reference/class/Configuration) class as follows:
100
170
101
171
<TabsgroupId="main">
102
172
<TabItemvalue="JavaScript"label="JavaScript">
@@ -135,24 +205,6 @@ async def main():
135
205
</TabItem>
136
206
</Tabs>
137
207
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
208
## Build-time environment variables
157
209
158
210
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:
0 commit comments