How to call a REST API from a web hook?

Hi,

I’m trying to call a POST REST API from the post user registration web hook and I keep getting a 500 error. I know that my REST API works so I’m not sure why it’s getting a 500 error.

“code”: 500,
“error”: “Script generated an unhandled asynchronous exception.”,
“details”: “ReferenceError: request is not defined”,
“name”: “ReferenceError”,
“message”: “request is not defined”,
“stack”: "ReferenceError: request is not defined\n at module.export

CODE:

request({
method: ‘POST’,
url: ‘HTTPS://to_URL;’,
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’,
},
body: { "username": "${user.username}", "firstname": "${user.firstname}" }
}, function(error, response, body) {
console.log(user)
console.log(context)
cb(error, response)
});

1 Like

You will need to include the request module in your script, e.g:

var request = require('request@2.56.0'); // for specific version of request
//var request = require('request'); // for latest version of request: https://tehsis.github.io/webtaskio-canirequire/

request({ 
  method: 'POST', 
  url: 'HTTPS://to_URL;', 
  headers: { 
    'Content-Type': 'application/x-www-form-urlencoded'
  }, 
  body: { 
    "username": "${user.username}", 
    "firstname": "${user.firstname}" } 
  }, 
  function(error, response, body) { 
    console.log(user);
    console.log(context);
    cb(error, response) 
});

Request documentation: GitHub - request/request: 🏊🏾 Simplified HTTP request client.

2 Likes

I’m still having trouble with this. Do you have an example of a POST? I set up the authorization API and everything seems to work when I make a Request from Advanced Rest Client but it doesn’t seem to work at all from the webhook. I’ve tried finding information on Google but haven’t found anything that explains that library.

The request library is the following:

Note, if you are more familiar with another library, you can see whether it is supported on Redirecting to https://auth0-extensions.github.io/canirequire/.

1 Like

I finally got this working! =) Thank you for giving me the URL to the library!

^^ How did you get this working @sandy ? Do you have a post example?

@prashant I’m now trying to figure out how to send extra HTTP headers with the request. I’m not having any luck at that. Do you know how to add an HTTP header with the POST? I tried doing the options but the console returned request.options isn’t a function.

Based on your code, you should be able to do something like this:

request.post({
  url:'your_url_goes_here', 
  headers: {
    'User-Agent': 'request' //example
  },
  form:formData
});
1 Like

^^ How did you get this working @sandy ? Do you have a post example?

var request = require(‘request@2.56.0’);

var formData = {
// Pass a simple key-value pair
firstname: user.user_metadata.first_name,
username: user.id
};

request.post({url:‘your_url_goes_here’, form:formData});

Make sure you register your API in the APIs section on the dashboard. =)

1 Like

Thank you. Got it working!

Do you know if you can tell where the request is coming from in a hook? I have a dev environment and a production one - I want to change the API thats being called based on the environment …

@harry this depends on how you have separated your dev/prod environments. If you are using different clients, the context object in the Hook has the clientID property which you can check, e.g:

var url;
if(context.clientID === 'MY_DEV_CLIENT_ID'){
  url = 'https://dev.myapp.com';
} else if (context.clientID === 'MY_PROD_CLIENT_ID'){
  url = 'https://myapp.com';
}