Oauth2 Synthetics API Test

Hi all,

I need to write a API test with Synthetics, but my App has to go through OAuthe2, with the grant type of authorization code. I’m pretty new to nodejs. Do we have any documentation for this?
I read the node modules link https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/import-nodejs-modules
, and found that passport and authentication modules are not included?

What’s the simplest way of doing this?

HI @tracywan_newbee - I have a sample API script which you can use which includes the authentication step :grin:

Thanks @stefan_garnham

I saw your post but that won’t work for my case. The oauth2 login part I need is authentication-code with client id and secret id. But the library packages included by New Relic is limited, so lots of cookie parser and response parser cannot be used.

More specifically, it’s like I have a response and I need to extract the code on the right of =

I’m exploring the regex haven’t got it working yet

new RegExp("[?;]code=([^\"]*)")).exec(response);

If you are after a method to manipulate cookies, this post on API cookies may help.

1 Like

@tracywan_newbee Let us know where you are at with this and if @stefan_garnham 's suggestions helped you get sorted! :blush:

I was able to figure it out but I would prefer there’s more modules that could be added to the whitelist.
Such as

Ahh definitely—good to know, @tracywan_newbee! Thanks for following up.

I’ll be sure to pass that input along as well. :thumbsup:

Thought I’d add another example, this is just a simple template script for hitting an OAuth 2.0 secured API in a New Relic Synthetic API test: https://gist.github.com/lostphilosopher/899a5ccce78f692906d7ee3e330aa098

It was a bit tedious to figure out how to do this in https://github.com/request/request and the example already in this thread was too complex for what I was looking for.

Hope this helps someone.

var assert = require('assert');

function new_relic_callback(err, response, body) {
  assert.equal(response.statusCode, 200, 'Expected a 200 OK response');

function api_request_callback(err, response, body) {
  var parsed_body = JSON.parse(body); 
  var api_request = {
    url: 'https://example.com',
    headers: {
      'Authorization': 'Bearer ' + parsed_body["access_token"]

  $http.get(api_request, new_relic_callback);

var token_request = {
  url: 'https://example.com/oauth/token',
  form: {
    client_id: "YOUR_CLIENT_ID",
    client_secret: "YOUR_CLIENT_SECRET",
    grant_type: "client_credentials"

$http.post(token_request, api_request_callback);

I just want to say thank you so much for this. I was completely lost and this was exactly what I needed. Just plug and play!


That is what we love to hear, @michael.brinkman! Thanks for letting us know you got what you needed. :blush:

1 Like


Thanks for the code snippet, just had to replace form with headers on the token request

Best regards

1 Like

Glad that worked for you @roshin.kulakkunnath :smiley:

@roshin.kulakkunnath I am working on an API monitor now using this example, but it’s failing for me. Can you clarify how you replaced the form with headers on the token request?