Skip to content

Commit ff39d10

Browse files
docs: Improve Actors environment variables docs
Signed-off-by: Patrik Braborec <[email protected]>
1 parent 2386bef commit ff39d10

File tree

1 file changed

+74
-22
lines changed

1 file changed

+74
-22
lines changed

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

Lines changed: 74 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 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+
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.
@@ -68,6 +83,41 @@ 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 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+
71121
## Access environment variables
72122

73123
You can access environment variables in your code as follows:
@@ -78,7 +128,17 @@ You can access environment variables in your code as follows:
78128
In Node.js, use the `process.env` object:
79129

80130
```js
81-
console.log(process.env.APIFY_USER_ID);
131+
import { Actor } from 'apify';
132+
133+
await Actor.init();
134+
135+
// get api_token
136+
const api_token = process.env.api_token
137+
138+
// print api_token to console
139+
console.log(api_token);
140+
141+
await Actor.exit();
82142
```
83143

84144
</TabItem>
@@ -88,15 +148,25 @@ In Python, use the `os.environ` dictionary:
88148

89149
```python
90150
import os
91-
print(os.environ['APIFY_USER_ID'])
151+
print(os.environ['api_token'])
152+
153+
from apify import Actor
154+
155+
async def main():
156+
async with Actor:
157+
# get api_token
158+
userId = os.environ['api_token']
159+
160+
# print api_token to console
161+
print(userId)
92162
```
93163

94164
</TabItem>
95165
</Tabs>
96166

97-
## Use the `Configuration` class
167+
## Access system environment variables
98168

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:
100170

101171
<Tabs groupId="main">
102172
<TabItem value="JavaScript" label="JavaScript">
@@ -135,24 +205,6 @@ async def main():
135205
</TabItem>
136206
</Tabs>
137207

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-
156208
## Build-time environment variables
157209

158210
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

Comments
 (0)