How to Use Environment Variables in Power Apps Code App Data Sources
How to Use Environment Variables in Power Apps Code App Data Sources
Tired of hardcoding connection strings and dataset IDs every time you move a Power Apps solution between environments? Environment variables are the answer — and once you start using them, you'll wonder how you ever managed without them. In this step-by-step guide, we'll show you exactly how to wire up environment variables in your Power Apps code app data sources, so your solutions move cleanly from development to test to production without a single manual edit.
What Are Environment Variables in Power Apps?
Environment variables are solution-aware components in Microsoft Power Platform that store configuration values — such as connection strings, dataset IDs, or API endpoints — outside of your app's code. Instead of hardcoding values that change between environments, you reference an environment variable and let the platform swap in the right value automatically at runtime.
This approach is a best practice for any Dynamics 365 implementation or Power Apps project that spans multiple environments (development, test, production).
Why Use Environment Variables in Code App Data Sources?
- No hardcoding: Avoid embedding dataset IDs or connection details directly in your app logic.
- Seamless environment transitions: Move solutions between environments without manual reconfiguration.
- Centralized management: Update a value once and have it reflected across every app and flow that references it.
- ALM-friendly: Environment variables travel with your solution, making Application Lifecycle Management (ALM) significantly smoother.
Prerequisites
Before you begin, make sure you have the following in place:
- A Power Apps environment with the necessary permissions to create and edit solutions.
- A code app (canvas app or custom page) already created within a solution.
- At least one data source (e.g., a SharePoint list, Dataverse table, or SQL dataset) that your app connects to.
- Access to the Power Platform maker portal.
Step 1: Create an Environment Variable in Your Solution
Environment variables must be created inside a solution to be solution-aware and portable.
- Navigate to make.powerapps.com and open your target solution.
- Click New > More > Environment Variable.
- Provide a descriptive Display Name (e.g., Dataset Connection URL) and set the Data Type to the appropriate type (e.g., Text, Secret, or Data Source).
- Enter a Default Value for your development environment.
- Click Save.
Tip: Use the Secret data type for sensitive values such as API keys or passwords. Secrets are stored in Azure Key Vault automatically.
Step 2: Reference the Environment Variable in Your Code App Data Source
Once your environment variable is created, you can reference it within your code app's data source configuration.
- Open your code app in the Power Apps editor.
- Navigate to the Data panel and select the data source you want to configure.
- In the data source connection settings, use the environment variable reference syntax instead of a hardcoded value.
- Save and publish your app.
For canvas apps and custom pages, you can also reference environment variables directly in formulas using the Environment function or by configuring them at the connector level, depending on your data source type.
Step 3: Set Environment-Specific Values
The real power of environment variables comes when you set different values for each environment.
- Export your solution and import it into the target environment (test or production).
- During import, Power Platform will prompt you to provide values for any environment variables that do not have a current value set in that environment.
- Enter the environment-specific value (e.g., the production dataset URL).
- Complete the import.
Your app will now automatically use the correct data source for each environment — no code changes required. This is a cornerstone of good Power Platform governance and ALM consulting.
Step 4: Update Environment Variable Values Without Redeployment
One of the most valuable features of environment variables is the ability to update values without redeploying your app.
- In the target environment, navigate to your solution.
- Locate the environment variable and click to edit it.
- Update the Current Value (not the default value — the current value is environment-specific).
- Save the change. Your app will pick up the new value automatically at runtime.
Common Mistakes to Avoid
- Editing the Default Value instead of the Current Value: The default value travels with your solution. Always set environment-specific values as the Current Value.
- Hardcoding alongside environment variables: Mixing hardcoded values with environment variables defeats the purpose. Audit your app for any remaining hardcoded connection details.
- Not creating variables inside a solution: Environment variables created outside a solution cannot be packaged and transported with your app.
- Skipping the Secret type for sensitive data: Always use the Secret data type for credentials and API keys.
Automating Environment Variable Management with Power Automate
For teams managing multiple environments at scale, you can automate the setting and updating of environment variable values using Power Automate flows. This is particularly useful in CI/CD pipelines where values need to be injected programmatically during deployment.
Need Help Implementing This in Your Organisation?
Environment variables are just one piece of a well-architected Power Platform solution. If you're looking to establish robust ALM practices, improve governance, or accelerate your Power Apps development, our team at CRMONCE is here to help. Explore our full range of Power Platform services or get in touch with our experts today.
Ready to Build Smarter Power Apps Solutions?
Whether you're just getting started with environment variables or looking to overhaul your entire ALM strategy, our certified Power Platform consultants can guide you every step of the way.