Environment Variables
Environment variables serve the purpose of storing secrets like the database password, the app secret, or an API key outside of your application codebase.
In addition, environment variables can be used to obtain different configurations depending on the environment, such as the use of a secret to use an API or database identifiers.
Introduction
Normally, environment variables are accessible via Dart's Platform class, but this is said to be immutable, so we can't add data relating to our application to it.
So we're providing an Environment class to access the various environment variables in your application.
The Environment class is a class built using the contents of your application's environment variables, accessible from the Platform class in the dart:io library.
The Environment class follows the following implementation contract.
abstract interface class EnvContract {
Map<String, String> get list;
T get<T>(EnvSchema variable);
void validate(List<EnvSchema> values);
}
We recommend that you use an .env environment file to store your environment variables as far as possible, although you can inject your variables using your CLI.
dart run src/main.dart --dart-define=MY_VARIABLE=Hello World
See more in Dart's documentation.
Minimal Configuration
DART_ENV=development
TOKEN=Nzg1ODgxOTk1NDc2ODYwOTc5.GW2skT._c5UijrPvZYNz-UU0_EfxTTPt2ZhCbFWxLk9N4
DISCORD_REST_API_VERSION=10
DISCORD_WS_VERSION=10
INTENT=3276799
LOG_LEVEL=TRACE
token
The TOKEN variable is a secret key that is used to authenticate the application with the Discord API.
TOKEN is used for Discord Websocket and API authentications, read more.
TOKEN is private and should not be shared with anyone.
http_version
The DISCORD_REST_API_VERSION variable is the version of the HTTP protocol that the application will use to communicate with the Discord API.
DISCORD_REST_API_VERSION defines the version of the HTTP protocol that the application will use to communicate with the Discord API, read more.
wss_version
The DISCORD_WS_VERSION variable is the version of the Websocket protocol that the application will use to communicate with the Discord API.
DISCORD_WS_VERSION defines the version of the Websocket protocol that the application will use to communicate with the Discord API, read more.
intent
The INTENT variable is the intent that the application will use to receive events from the Discord API.
INTENT defines the intent that the application will use to receive events from the Discord API, read more.
log_level
The LOG_LEVEL variable is the level of logging that the application will use to log messages.
const logLevels = {
'TRACE': logging.Level.FINEST,
'FATAL': logging.Level.SHOUT,
'ERROR': logging.Level.SEVERE,
'WARN': logging.Level.WARNING,
'INFO': logging.Level.INFO,
};
The trace level is the lowest level, used in particular for debugging or log reporting by a third party such as loki.
dart_env
The DART_ENV variable is a string that determines whether the application will use partial reload.
Validate Environment
To validate your application's environment variables and ensure their integrity, you can use the validate method of the Environment class.
The validate function takes as parameters the values of an enum implementing the EnvSchema interface.
void main() {
env.validate(MyEnum.values);
}
enum MyEnum implements EnvSchema {
myVariable('MY_VARIABLE', required: true);
@override
final String key;
@override
final bool required;
const MyEnum(this.key, {this.required = true});
}
Read Environment Variables
To read an environment variable, you can use the get method of the Environment class.
As explained above, the get method takes as a parameter an enum implementing the EnvSchema interface.
void main() {
final value = env.get(MyEnum.myVariable);
print(value); // print('Hello World');
}
enum MyEnum implements EnvSchema {
myVariable('MY_VARIABLE', required: true);
@override
final String key;
@override
final bool required;
const MyEnum(this.key, {this.required = true});
}
MY_VARIABLE=Hello World