Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

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
, 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.


@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:

It was a bit tedious to figure out how to do this in 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: '',
    headers: {
      'Authorization': 'Bearer ' + parsed_body["access_token"]

  $http.get(api_request, new_relic_callback);

var token_request = {
  url: '',
  form: {
    client_id: "YOUR_CLIENT_ID",
    client_secret: "YOUR_CLIENT_SECRET",
    grant_type: "client_credentials"

$, api_request_callback);

Multiple API calls in single synthetics

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: