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
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.
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 :
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.
The output is like,
Hope this helps you.
I also tried a native C# function for the env:
var myEnvVar = Environment.GetEnvironmentVariable("DemoKey");
This also did not work.
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.
DBO_DV
25
Super User 2025 Season 1
CU09051456-0
22
Michael E. Gernaey
12
Super User 2025 Season 1