Skip to content

Commit c7f20b4

Browse files
authored
docs: Add docs for passwords available through env variables (#297)
1 parent 93acd33 commit c7f20b4

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

docs/06-concepts/07-configuration.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,21 @@ These can be separately declared for each run mode in the corresponding yaml fil
7171

7272
### Secrets
7373

74-
Secrets are declared in the `passwords.yaml` file. The password file is structured with a common `shared` section, any secret put here will be used in all run modes. The other sections are the names of the run modes followed by respective key/value pairs.
74+
Secrets are declared in the `passwords.yaml` file. The password file is structured with a common `shared` section, any secret put here will be used in all run modes. The other sections are the names of the run modes followed by respective key/value pairs. You can also define custom secrets using [environment variables](#2-via-environment-variables).
75+
76+
#### Built-in Secrets
77+
78+
The following table shows the built-in secrets that Serverpod uses for its core functionality. These can be configured either through environment variables or by adding the corresponding key in a respective run mode or shared section in the passwords file. These are separate from any custom passwords you might define.
7579

7680
| Environment variable | Passwords file | Default | Description |
7781
| --------------------------- | -------------- | ------- | ----------------------------------------------------------------- |
7882
| SERVERPOD_DATABASE_PASSWORD | database | - | The password for the database |
7983
| SERVERPOD_SERVICE_SECRET | serviceSecret | - | The token used to connect with insights must be at least 20 chars |
8084
| SERVERPOD_REDIS_PASSWORD | redis | - | The password for the Redis server |
8185

82-
#### Secrets for first party packages
86+
#### Secrets for First Party Packages
87+
88+
The following secrets are used by official Serverpod packages:
8389

8490
- [serverpod_cloud_storage_gcp](https://pub.dev/packages/serverpod_cloud_storage_gcp): Google Cloud Storage
8591
- [serverpod_cloud_storage_s3](https://pub.dev/packages/serverpod_cloud_storage_s3): Amazon S3
@@ -91,6 +97,60 @@ Secrets are declared in the `passwords.yaml` file. The password file is structur
9197
| SERVERPOD_AWS_ACCESS_KEY_ID | AWSAccessKeyId | - | The access key ID for AWS authentication for serverpod_cloud_storage_s3 |
9298
| SERVERPOD_AWS_SECRET_KEY | AWSSecretKey | - | The secret key for AWS authentication for serverpod_cloud_storage_s3 |
9399

100+
#### Custom Secrets
101+
102+
You can define your own custom secrets in two ways.
103+
104+
##### 1. Via Passwords File
105+
106+
Add your custom secrets directly to the passwords file under the `shared` section (available in all run modes) or under specific run mode sections.
107+
108+
```yaml
109+
shared:
110+
myCustomSharedSecret: 'secret_key'
111+
stripeApiKey: 'sk_test_123...'
112+
113+
development:
114+
database: 'development_password'
115+
redis: 'development_password'
116+
serviceSecret: 'development_service_secret'
117+
twilioApiKey: 'dev_twilio_key'
118+
119+
production:
120+
database: 'production_password'
121+
redis: 'production_password'
122+
serviceSecret: 'production_service_secret'
123+
twilioApiKey: 'prod_twilio_key'
124+
```
125+
126+
##### 2. Via Environment Variables
127+
128+
You can also define custom passwords using environment variables with the `SERVERPOD_PASSWORD_` prefix. For example, `SERVERPOD_PASSWORD_myApiKey` will be available as `myApiKey` (the prefix is stripped). These environment variables will override any passwords defined in the passwords file if the name (after stripping the prefix) matches. Like the `shared` section in the passwords file, these environment variables are available in all run modes.
129+
130+
| Environment variable format | Description |
131+
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
132+
| SERVERPOD_PASSWORD\_\* | Custom password that will be available in the Session.passwords map. The prefix `SERVERPOD_PASSWORD_` will be stripped from the key name. |
133+
134+
**Example:**
135+
136+
To define a custom password through an environment variable:
137+
138+
```bash
139+
export SERVERPOD_PASSWORD_stripeApiKey=sk_test_123...
140+
```
141+
142+
**Accessing Custom Passwords:**
143+
144+
You can then access any custom password (whether defined in the passwords file or via environment variables) in your endpoint code through the `Session.passwords` map:
145+
146+
```dart
147+
Future<void> processPayment(Session session, PaymentData data) async {
148+
final stripeApiKey = session.passwords['stripeApiKey'];
149+
// Use the API key to make requests to Stripe
150+
...
151+
}
152+
```
153+
94154
### Config file example
95155

96156
The config file should be named after the run mode you start the server in and it needs to be placed inside the `config` directory in the root of the server project. As an example, you have the `config/development.yaml` that will be used when running in the `development` run mode.
@@ -151,11 +211,13 @@ development:
151211
database: 'development_password'
152212
redis: 'development_password'
153213
serviceSecret: 'development_service_secret'
214+
twilioApiKey: 'dev_twilio_key'
154215
155216
production:
156217
database: 'production_password'
157218
redis: 'production_password'
158219
serviceSecret: 'production_service_secret'
220+
twilioApiKey: 'prod_twilio_key'
159221
```
160222

161223
### Dart config object example

0 commit comments

Comments
 (0)