Make HTTP requests
In Copilot Studio, you can extend a copilot's capabilities by calling external REST APIs using the HTTP request node. This call can be helpful when you need to retrieve data from an external system or manipulate data on that system.
Prerequisites
Use the HTTP request node
Select Add node (+), open the Advanced sub menu, and then select Send HTTP request.
In the URL box, enter the URL for the API endpoint you want to call.
Select the appropriate request Method from the dropdown. GET, POST, PATCH, PUT, and DELETE methods are supported.
Under Headers and body on the node, select the Edit button, which opens the HTTP request properties pane.
You can optionally add one or more headers to the HTTP request. Select the Add button to add each header's key and value.
By default, no content is sent in the body of the HTTP request, but you can specify content in the body.
On the node, choose the Response data type from the available options.
You can provide an example JSON response, which you can usually find in the documentation for the API you're calling. It generates a Power Fx variable, allowing you to use it elsewhere in your authoring canvas, with intellisense support in the Power Fx editor. Select From Sample Data and then select Get schema from sample JSON. Paste your sample response into the editor and select Confirm.
Under Save response as choose where you want the HTTP request response to be stored, either creating a new variable or selecting an existing one.
Send content in the HTTP request body
Under Headers and body on the node, select the Edit button, which opens the HTTP request properties pane.
Under Body, select the appropriate content type. By default, Body is set to No Content, where no content should be present within the request body, which is most commonly used with the GET request. To add content to the body, select from one of the available options.
JSON Content: JSON content allows you to provide a JSON object in the request body. This object is often used when making POST or PUT requests. When this option is selected, an editor allows you to enter your JSON content.
You can also specify your JSON content using Power Fx, allowing you to include dynamic values and reference variables. Select the Edit JSON button, choose Formula, and then enter a Power Fx object (any existing JSON that was entered is converted into a Power Fx object for you), which is converted into JSON when the HTTP request is made.
Raw content: Raw content lets you insert a string of data into the request body, and this string can be of any content type you specify. Raw content is entered using a Power Fx formula, such as the string content shown in the following example.
Error handling and timeout
The HTTP request node has multiple options for handling situations where the HTTP request might fail or return an error.
By default, the feature "raises an error." This means that when an HTTP request returns an error or fails to execute, the copilot stops its operation and triggers the Error system topic, displaying an error message.
However, you can configure the error handling to not trigger the Error system topic. Instead, it populates the HTTP status code and any error response body into variables that you specify, allowing you to interrogate these variables as needed, and for the topic to continue to run. This configuration is useful in scenarios where you want the copilot to continue operation, even if a particular HTTP request fails.
To configure the error handling behavior.
Under Headers and body on the node, select the Edit button, which opens the HTTP request properties pane.
Choose your desired behavior from the Error handling dropdown.
If you choose Continue on error, configure the Status code and Error response body variables, either creating new variables or selecting existing variables from the picker.
In this code sample, if the HTTP request fails, the copilot stores the HTTP status code in the Topic.StatusCode
variable, and the response body in the Topic.ErrorResponse
variable. The copilot continues to its next steps in the topic.
The Error response variable type is Any. You can use the Parse Value node to convert this variable into a Power Fx record.
The property pane also contains a Request timeout property, which accepts a value in milliseconds. The default value is 30 seconds, but you can change this value if needed.