Skip to main content

Notifications

Community site session details

Community site session details

Session Id : kgYKhPk4Mmt/QxQszsqLrE
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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,702 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,015 Most Valuable Professional

Leaderboard

Featured topics

Loading started