cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AlternatiValue
New Member

Environment Variables or Key Vault from Code (Preview)

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

1 ACCEPTED SOLUTION

Accepted Solutions
muralidharan
Resolver III
Resolver III

@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.

 

 

View solution in original post

4 REPLIES 4
AlternatiValue
New Member

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.

 

AlternatiValue
New Member

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

 

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

 

This also did not work.

muralidharan
Resolver III
Resolver III

@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
New Member

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.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (3,087)