Node.js and Google Pagespeed API Node.js and Google Pagespeed API curl curl

Node.js and Google Pagespeed API


Drop the https:// prefix in host, and you should be good to go. See the docs here.

Here's a working example, just substitute your own URL and API key:

var https = require('https'),    key = 'KEY',    url = 'URL',    strategy = 'desktop';https.get({    host: 'www.googleapis.com',     path: '/pagespeedonline/v1/runPagespeed?url=' + encodeURIComponent(url) +           '&key='+key+'&strategy='+strategy    }, function(res) {      console.log("statusCode: ", res.statusCode);      console.log("headers: ", res.headers);      res.on('data', function(d) {        process.stdout.write(d);      });}).on('error', function(e) {  console.error(e);});


You can use Google's node client library for its APIs.

var googleapis = require('googleapis');googleapis.load('pagespeedonline', 'v1', function(err, client) {  // set your api key  client = client.withApiKey('...');  var params = { url: '...', strategy: '...' };  var request = client.pagespeedonline.pagespeedapi.runpagespeed(params);  request.execute(function (err, result) {    console.log(err, result);  });});

The client library also supports batch requests that may be useful in your case. Further documentation is https://github.com/google/google-api-nodejs-client


google-api-nodejs-client is Google's officially supported node.js client library for accessing Google APIs.

npm install googleapis

For PageSpeed Insights API, it's now somehting like this :

require('googleapis')    .discover('pagespeedonline', 'v1')    .execute(function (err, psclient) {        var params = { url: URLHERE }; // others params https://developers.google.com/speed/docs/insights/v1/getting_started        var request = psclient.pagespeedonline.pagespeedapi.runpagespeed(params).withApiKey(YOUR_API_KEY);;        request.execute(function (err, result) {            //do something        });    });