Hello! Welcome back to our advanced Power Platform course.
In our last lesson, we focused on the Definition tab of the custom connector, defining the static "what" of our connector—its actions, triggers, and parameters. This is like defining the method signatures in an API library.
Today, we address a more dynamic challenge with the learning outcome: Implement policy templates to modify the behavior of a custom connector at runtime. Think of policies as lightweight middleware or HTTP interceptors that you can configure without writing any code. They allow you to dynamically alter the request before it's sent or the response after it's received. For a developer like yourself, this is a powerful concept for managing variability and adding consistent behavior across your connector's operations.
This lesson is crucial for your PL-400 preparation as it covers a key feature for building robust and manageable connectors. We will explore how to:
- Understand what policy templates are and how to apply them.
- Implement the
Set host URLpolicy to support Application Lifecycle Management (ALM) across different environments. - Use the
Set HTTP Headerpolicy to modify request headers dynamically. - Leverage environment variables within policies for a more advanced ALM strategy.
1. What are Policy Templates?
Policies are rules that modify the behavior of a custom connector at runtime. Instead of hard-coding values or logic into the connector's definition, you can use policies to intercept the API call and make changes. Microsoft provides a set of pre-built policy templates to handle common scenarios.
First, let's get the official definition and see how to access policies from the Microsoft Learn documentation. This will ground our understanding of the feature's purpose and location in the UI.
Read the introductory paragraphs and the section titled 'How to use a policy template'. Focus on the core concept: policies modify connector behavior at runtime. Note the steps to add a new policy from the 'Definition' page of the custom connector wizard.
As you just read, policies are configured on the same Definition tab where you create actions. You can create multiple policies, and they will be executed in the order they are listed.

2. Core Use Case: Dynamic Host URLs for ALM
One of the most powerful applications of policy templates is managing connections across different development environments (e.g., Dev, UAT, Production). Your backend API will likely have different URLs for each environment. Instead of creating three separate custom connectors, you can use one connector and dynamically set the host URL at runtime.
This video provides an excellent end-to-end demonstration of this exact scenario.
039 - Dynamically Changing Power Automate Custom Connector Values using Policy Templates
Watch this video from The Agent Frontier. The presenter, Kent Weare, clearly explains the problem of managing multiple environments and demonstrates how to solve it using the 'Set host URL' policy template.
Watch the video from the beginning (00:00:23) to 00:10:26. Pay close attention to these key steps: The concept of overriding the design-time host URL. How a new, 'internal' query parameter is added to the action to accept the dynamic URL. The configuration of the 'Set host URL' policy, using an expression to reference the new query parameter. The final test in a Power Automate flow, where he proves the dynamic routing works by first passing an incorrect URL and then the correct one.
To summarize the process you just saw:
- Modify the Action: Edit your action's definition and add a new parameter (e.g.,
new_url). This parameter will act as a placeholder for the dynamic URL you'll pass from your Power App or flow. You would typically set its visibility toadvanced. - Create the Policy: Create a new policy and select the
Set host URLtemplate. - Configure the Template: In the
URL templatefield, you provide an expression to tell the policy where to find the new URL. The video used@queryParameters('new_url'), which instructs the policy to get the value from thenew_urlquery parameter of the action. The format for the URL should behttps://followed by the value, for example:https://@{queryParameters('new_url')}. - Use in a Flow/App: When you use the custom connector's action in a flow, the
new_urlparameter will be available. You can then pass the environment-specific URL to this parameter, and the policy will ensure the request is routed correctly.
This pattern is fundamental for building enterprise-grade solutions that can be seamlessly deployed across environments.
3. Modifying Request and Response Headers
Another common requirement is to add or modify HTTP headers. This could be for passing a static API key, a dynamic correlation ID for tracing, or even manipulating headers in the response.
Let's look at a practical example using the Set HTTP Header template.
Power Platform | Working with Custom Connector Policies
This blog post by Rajeev Pentyala provides a clear, screenshot-driven example of using a policy to add an access token to a response header.
Read the sections 'Basics of Custom Connector Policies' and 'Example of using ‘Set HTTP Header’ policy template'. Observe how a policy is configured to add a new header to the response.
The example in the blog post adds a header to the response, but the principle is identical for adding one to the request.

Test your understanding!
You are building a custom connector to an internal logging service. Every API call must include a custom header X-Correlation-ID containing a unique GUID to trace the request through the system. You want the Power Automate flow that calls the connector to generate this GUID and pass it in.
How would you implement this using a policy template?
Show answer
- Add a Parameter: First, you would edit the action in the custom connector definition and add a new request parameter named
correlationId. You could make it required and place it in the header. - Create a Policy: Create a new policy using the
Set HTTP Headertemplate. - Configure the Policy:
- Header Name:
X-Correlation-ID - Value: Use an expression to reference the parameter you created, such as
@headers('correlationId'). - Action:
override(to ensure it uses your value). - Run policy on:
Request.
- Header Name:
- In the Flow: When calling the action, you would use the
guid()expression in Power Automate to generate a new GUID and pass it to thecorrelationIdparameter.
4. Advanced ALM: Using Environment Variables in Policies
While passing the host URL as an action parameter works, it forces the app or flow creator to be aware of the URL. A more robust ALM pattern is to store this configuration data in Environment Variables. Policies can directly reference these variables.
This approach decouples the configuration from the implementation.
Power Platform | Working with Custom Connector Policies
Let's revisit the blog post, which also covers this more advanced technique.
Read the section 'Use environment variables in a policy template (preview)'. Note the syntax @environmentVariables("variable_schema_name") and the critical prerequisite: the custom connector must be part of a Dataverse solution to use this feature.
By using an environment variable in your Set host URL policy (e.g., https://@{environmentVariables("cr49f_MyApiUrl")}), you achieve a superior ALM strategy:
- The connector definition is the same in all environments.
- The flow/app that uses the connector is the same.
- The only thing that changes is the value of the
cr49f_MyApiUrlenvironment variable in each target environment. The deployment process involves updating this value, not the connector or the flow itself.
This is a key practice for professional Power Platform developers and highly relevant for the PL-400 exam.
Conclusion
You've now seen how to move beyond static definitions and dynamically modify connector behavior at runtime. Policy templates are a critical tool for building flexible, maintainable, and enterprise-ready custom connectors.
Key Takeaways:
- Policies are no-code middleware: They intercept and modify requests and responses as they pass through your custom connector.
- The
Set host URLtemplate is essential for ALM: It allows a single connector definition to target different API endpoints across Dev, UAT, and Prod. - The
Set HTTP Headertemplate provides control over headers: Use it to add static or dynamic headers for authentication, tracing, or other metadata. - Policies can be driven by action parameters or environment variables: Using environment variables provides a more robust and decoupled solution for managing configuration in an ALM context.
- Prerequisite for Environment Variables: Remember that the custom connector must be included in a Dataverse solution to reference environment variables within its policies.
What's Next
So far in this module, we have focused entirely on API-based automation using cloud flows and custom connectors to communicate with backend services. In the next lesson, we will shift our focus to the front end. You will learn how to create a desktop flow using Power Automate for Desktop to automate a UI-based task. This will introduce you to the world of Robotic Process Automation (RPA) within the Power Platform, a powerful tool for automating legacy systems that don't have APIs.