{"record":{"id":"c61d8dfbcea3ed79","repo":"clockworklabs/SpacetimeDB","slug":"required-environment-key-is-missing-key","errorCode":null,"errorMessage":"required environment key is missing: {key}","messagePattern":"required environment key is missing: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings/src/rt.rs","lineNumber":955,"sourceCode":"    fn get(environment: &crate::Environment, key: &str) -> Self {\n        Self::from_environment(environment.get(key), key)\n    }\n}\n\n/// Required environment types supported by the optional-value implementation.\n/// Derived enums and `String` implement this; `Option<T>` deliberately does not.\n#[doc(hidden)]\npub trait RequiredEnvironmentValue: EnvironmentValue {}\n\nimpl EnvironmentValue for String {\n    const OPTIONAL: bool = false;\n\n    fn constraint() -> spacetimedb_lib::environment::EnvVarType {\n        spacetimedb_lib::environment::EnvVarType::String\n    }\n\n    fn from_environment(value: Option<String>, key: &str) -> Self {\n        value.unwrap_or_else(|| panic!(\"required environment key is missing: {key}\"))\n    }\n}\n\nimpl RequiredEnvironmentValue for String {}\n\nimpl<T: RequiredEnvironmentValue> EnvironmentValue for Option<T> {\n    const OPTIONAL: bool = true;\n\n    fn constraint() -> spacetimedb_lib::environment::EnvVarType {\n        T::constraint()\n    }\n\n    fn from_environment(value: Option<String>, key: &str) -> Self {\n        value.map(|value| T::from_environment(Some(value), key))\n    }\n}\n\nmod string_environment_value_sealed {","sourceCodeStart":937,"sourceCodeEnd":973,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/bindings/src/rt.rs#L937-L973","documentation":"RequiredEnvironmentValue::from_environment unwraps the Option<String> supplied by the host and panics if it is None. It marks an environment variable as mandatory: unlike plain EnvironmentValue, absence is a hard error rather than a fallback. The panic names the missing key so the developer knows which variable to set.","triggerScenarios":"Declaring a module config/env field of a type implementing RequiredEnvironmentValue (e.g. String) and publishing/running the module without that environment variable set in the database environment.","commonSituations":"Deploying a module to a new database and forgetting to run `spacetime env set KEY ...` / set the variable in the publisher config; renaming a key in code but not in the deployment environment.","solutions":["Set the missing environment variable for the database before publishing or calling the module (e.g. `spacetime set <db> <key> <value>` or the equivalent CLI env subcommand).","Use Option<T> as the field type if the variable is genuinely optional, which falls back to None instead of panicking.","Provide a default via a custom EnvironmentValue implementation with a default instead of RequiredEnvironmentValue.","Audit the module's declared env keys against the deployment environment during CI."],"exampleFix":"// before\n#[spacetimedb(env)]\napi_key: String, // panics if unset\n// after\n#[spacetimedb(env)]\napi_key: Option<String>,\nlet api_key = api_key.unwrap_or_else(|| \"dev-default\".into());","handlingStrategy":"validation","validationCode":"assert!(std::env::var(\"API_KEY\").is_ok() || db_env_get(\"API_KEY\").is_some(), \"API_KEY must be set before publish\");","typeGuard":null,"tryCatchPattern":"let key = Option::<String>::from_environment(host_value, \"API_KEY\");","preventionTips":["Document every required env key in module README/config","Use Option<T> for optional variables","Verify deployment env vars in CI before publishing","Keep key names in a single constants module"],"tags":["environment","panic","configuration","rust"],"backgroundTag":"missing-env-var","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-09-20T12:15:59.611Z","contentChangedAt":"2026-09-20T12:15:59.611Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}