How can I do api calls in the custom node created with execute type

The custom executes type node already comes with the taking input from the node and returns it. I want to do an API call in between and return the response of api call. My exact question is how can I do API calls in the node type execute? What should I use from the n8n library to do that?

You can simply use this.helpers.request(options);

All the code of all the nodes is on Github. One simple example which shows how you can achieve that is this one:

Thanks. I have tried it but there is no response. IN the work flow after execution " Results: 0 No data"

   const options = {
  method: "GET",
  headers: {
    "x-lf-source": "n8n"
  },
  uri: `https://dev58150.service-now.com/api/x_58872_needit/needit`,
  json: true
};

const response = await this.helpers.request(options);

return this.prepareOutputData(response);

I tested the API call in postman it is working there.

Simply check with a console.log . I am sure if postman returns a response you will also see one there.

What you have to make sure, however, is that you return the data to n8n I the way it expects it. You find more information about that in the documentation. Like for example here the data structure gets described:
https://docs.n8n.io/#/data-structure

So what you have to make sure of in this case is to return an array of objects that contains the data in the “json” property.

Also for that it is best to simply look in existing nodes like the Pipedrive-Node.

Thanks. The format solution worked. I have added

const finalResponse = [
      {
        json: {
          response
        }
      }
    ];

and returned finalResponse

return this.prepareOutputData(finalResponse);

Console.log didnt work though.

1 Like

Ah great to hear that you got it working! And thanks for posting the code to help other people. It is very appreciated!

About console.log, I meant that you should simply do a:

console.log(response);

To see if you get a response or not.

I could see the console log in docker running terminal. I was expecting browser console. My mistake never mind. Thanks.

Ah great! Then have fun!