Call REST API in Microsoft Bot Framework using Echo Bot Template

In this post, we will call REST API from the Echo Bot Template. The user sends a query, we use the query in our GET request which returns the search results.


  1. Visual Studio
  2. Bot Emulator
  3. Create Echo Bot using Bot Builder SDK in Local Environment | Microsoft Bot Framework

Links for first two requirements are available in Downloads page.

Create the Bot

I have given the project name CallREST. I have used Google Search API from Rapid API which is free to use. Please note, this google search API is not official. For official documentation from Google, visit here.

Rename the EchoBot.cs file to RESTBot.cs. Also, change all the references pointing to this file. You will get a pop-up to change the references.

Copy the code snippets to call the API from Rapid API and create a new method GetSearchAsync in RESTBot.cs. Replace the <Your Rapid API key> with your key. For testing purpose, I have entered the key in the code itself. You should always use config files (appsettings.json) or key vaults to pass these credentials. Never enter your credentials directly in the code.

This API call gets 10 results based on your query. If you need more results, give another number for num in URI. This method will return a JSON which I will be sending to the user.

For better readability for the user, you can parse this JSON and get the required information out of it. Read my post on parsing API JSON response here using modal classes. Also, this post gives an example for retrieving the data from the API JSON.

private async Task<string> GetSearchAsync(string userQuery)
            string uri = "" + userQuery + "&num=10";
            var client = new HttpClient();
            var request = new HttpRequestMessage
                Method = HttpMethod.Get,
                RequestUri = new Uri(uri),
                Headers =
                    { "x-rapidapi-key", "<Your Rapid API Key>" },
                    { "x-rapidapi-host", "" },
            using (var response = await client.SendAsync(request))
                var body = await response.Content.ReadAsStringAsync();
                return body;

Modify the OnMessageActivityAsync method as below.

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
            var userQuery = $"{turnContext.Activity.Text}";
            string responseQuery = await GetSearchAsync(userQuery.Replace(" ","+"));
            await turnContext.SendActivityAsync(MessageFactory.Text(responseQuery,responseQuery), cancellationToken);

This completes our small project which you can make use of for calling APIs using echo bot template. User gives the query, you pass the query to API and return the result back to the user. This template is ready to use for any API call. Just pass in your API header, request type, and URI, and you are good to go.

Get the complete code from GitHub.

Thank you All!!! Hope you find this useful.

Up ↑

%d bloggers like this: