Skip to main content

Notifications

Community site session details

Community site session details

Session Id : pCurhw6rNg2LLN4/WjGVxw
Power Automate - Using Connectors
Answered

Environment Variables or Key Vault from Code (Preview)

Like (0) ShareShare
ReportReport
Posted on 13 Sep 2022 00:20:55 by 8

Hi,

 

I need to add a custom header to requests to a third-party API using a C# function that they provided (I'm simplifying). I have it working in a Power Automate custom connector using the Code (Preview) section with certain values hard-coded, but I'd like to move those values to either environment variables or Azure key vault. 

 

Has anyone had any success accessing either of those from the Code (Preview) section of a customer connector?

 

I really think that I saw a MS doc that said, at this time, that custom connectors do not have access to the environment variables table and that it would require a separate API call. Unfortunately, I cannot seem to find that MS doc.

 

Thanks

  • AlternatiValue Profile Picture
    8 on 15 Sep 2022 at 16:33:13
    Re: Environment Variables or Key Vault from Code (Preview)

    Thank you, very much, @muralidharan 

     

    I was able to reproduce your example - your instructions are very good.

     

    I'm going to start working on adapting it to my specific need.

  • Verified answer
    Muralidharan Profile Picture
    340 on 13 Sep 2022 at 16:06:48
    Re: Environment Variables or Key Vault from Code (Preview)

    @AlternatiValue 

    I found a simple work around for your request.

     

    While creating a custom connector, we can defining Action definition, we can pass the environment variable indirectly by using querystring param or http body area (for POST ).
    This param value can be passed as Environment variables from Power Automate / Power Apps while invoking the custom connector Action.

     

     

     

     

    public class Script : ScriptBase
    {
     public override async Task<HttpResponseMessage> ExecuteAsync()
     {
     HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
     //Read all the querystring params
     var query = HttpUtility.ParseQueryString(this.Context.Request.RequestUri.Query);
     //Read the name querystring param
     var qname = Uri.UnescapeDataString(query.Get("name"));
     //send the query string value to the output.
     response.Content = CreateJsonContent("{'message':" + "'" + qname + "'}");
     return response;
     }
    }​

     

     

     

    Connector details : 

     

    muralidharan_1-1663084981736.png

     

    Here name is the querystring param for the Get request and it is used in the Code area. 

     

    In the Power Automate side, I passed the querystring value from Environment variable. 

    muralidharan_2-1663085087795.png

    The output is like, 

    muralidharan_3-1663085125652.png

     

    Hope this helps you.

     

     

  • AlternatiValue Profile Picture
    8 on 13 Sep 2022 at 15:18:36
    Re: Environment Variables or Key Vault from Code (Preview)

    I also tried a native C# function for the env:

     

    var myEnvVar = Environment.GetEnvironmentVariable("DemoKey");

     

    This also did not work.

  • AlternatiValue Profile Picture
    8 on 13 Sep 2022 at 14:42:41
    Re: Environment Variables or Key Vault from Code (Preview)

    I tried an experiment using the instructions here: Use environment variables in solution custom connectors (preview) | Microsoft Docs


    As that page describes, I created an environment variable within a solution followed by a new Connector in the same solution. When I use a simplistic reference in Code (Preview):

     

    var myEnvVar = @environmentVariables("DemoKey");


     
    I get the following when I try to create the connector:
     

    Failed to provision compute for custom code. Request failed with error 'Code: BadRequest. Message: Script.cs(36,24): error CS0103: The name 'environmentVariables' does not exist in the current context.'. The tracking Id is


     
    I don't find this to be totally unexpected, but I'd still like to know if there is a way to do this.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Using Connectors

#1
DBO_DV Profile Picture

DBO_DV 25 Super User 2025 Season 1

#2
CU09051456-0 Profile Picture

CU09051456-0 22

#3
Michael E. Gernaey Profile Picture

Michael E. Gernaey 12 Super User 2025 Season 1

Overall leaderboard

Featured topics