How to call an external API

Hi Botpress team.
greetings to all.
I am really thankful for your product. It is helping a lot lately.
But now I want to develop the bot to a higher level.
I am not unable to call an external API with the botpress cloud with execute code. I have a backend API in python flask for my own website, Now with the help of chatbot, I want to input the query and based on that query the API will send JSON response. the backend is hosted already. Just give me a syntax how I can create the function. I have not much idea how to use headers, then authorization and all.
thank you

Hello, i’m not big on code but, you can use this :

Step 1 : The Url for the request :

const Url_api = 'https://theAPI_url_change_this';

Step 2 : The data you’re sending :

const Mydata = {
  data1: workflow.thevariable1,
  data2: workflow.thevariable2,
  data3: workflow.thevariable3,
  data4: workflow.thevariable4,
};

const Myconfig = {
  headers: {
    Authorization: 'Bearer <your_access_token_here>'
  }
};

Step 3 : Now we send the data to the Url :

const SendtoURL = await axios.post(Url_api, Mydata, Myconfig);

Step 4 : We store the Api Response :

const Api_Results = SendtoURL.data;

Step 5 : We extract everything and put them in our workflow variables :

workflow.response1 = Api_Results.thedata_to_extract1;
workflow.response2 = Api_Results.thedata_to_extract2;
workflow.response3 = Api_Results.thedata_to_extract3;

That should work…i hope :slight_smile:

1 Like

sure, I will try this way. But right now the concern is about the data storing and deploying. I want to deploy it on AWS, will it be possible? because currently in the botpress studio, we are creating the bot and publishing in the botpress cloud. So regarding data safety for example the API response and all, will it be stored on your server or platform?

Thanks for your help @Coucou_des_Bois !

I also have a Flask app. (I did not implement authorization/header b/c there’s nothing sensitive being passed). This worked for me:

const answer = await axios({
  method: 'post',
  url: 'https://myserver.pythonanywhere.com/endpoint',
  data: {
    /* saved Capture info -> variable "user_input" */
     'param1': workflow.user_input,
     'param2': 'value2',
     'param3': 'value3'
  }
});

workflow.response = answer.data.return_value;
1 Like

I think the guys here were sufficient, let me know if you steel need more ideas :slight_smile:

There is also two more examples from here:
https://botpress.com/docs/cloud/getting-started/tips-and-tricks/