ajyang818
ajyang818

Reputation: 505

How curl maps onto Google App Script URLFetchApp

I'm looking to implement Stripe in Google Apps Script, which comes with a URLFetch feature for communicating with third parties. However, I'm confused how the curl format that Stripe uses maps onto URLFetch.

Here's an example of a Stripe call from their documentation:

curl https://api.stripe.com/v1/charges \
-u testtoken123: \
-H "Idempotency-Key: testkey" \
-d amount=2000 \
-d currency=usd \
-d description="Charge for [email protected]" \
-d source=tok_mastercard

And the URLFetchApp documentation is here: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

Do the flags in the curl call map directly to params in URLFetch?

Upvotes: 1

Views: 1119

Answers (1)

rckrd
rckrd

Reputation: 3354

Below is an example of helper functions that can be used to call the Stripe API in Google Apps Script.

function getAuthHeader(){
  var apiKey = "STRIPE_API_KEY__CONSIDER_TO_GENERATE_A_KEY_WITH_LIMITED_SCOPE";
  var authHeader = 'Basic ' +Utilities.base64Encode(apiKey);
  return {
    headers: {Authorization: authHeader}
   }
}

function goFetch(url){
  var reponse; 
  try{
    reponse = JSON.parse(UrlFetchApp.fetch(url,getAuthHeader()).getContentText());
  }catch(err){
    Logger.log(err);
  }
  return reponse;
}

Example usage, listing charges:

function listCharges(lim){
  var url = 'https://api.stripe.com/v1/charges?'+limit=lim;
  return goFetch(url);
}
Logger.log(listCharges(10));

In your example, you are making a post request with curl. From the curl manual:

-d, --data (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

In the UrlFetchApp reference manual you find the following:

// Make a POST request with form data.
var resumeBlob = Utilities.newBlob('Hire me!', 'text/plain', 'resume.txt');
var formData = {
  'name': 'Bob Smith',
  'email': '[email protected]',
  'resume': resumeBlob
};
// Because payload is a JavaScript object, it will be interpreted as
// as form data. (No need to specify contentType; it will automatically
// default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
  'method' : 'post',
  'payload' : formData
};
UrlFetchApp.fetch('https://httpbin.org/post', options);

So a goPost function would be something like this:

function goPost(url,data){
   var options = {
   'method' : 'post',
   'payload' : data,
   'headers': getAuthHeader()['headers']
   };
   var response; 
   try{
     response = JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
   }catch(err){
    Logger.log(err);
   }
   return response;
 }

Example usage:

var data = {
  amount: 2000,
  currency: 'usd',
  source: 'tok_amex',
  description: 'Charge for [email protected]'
}
var result = goPost('https://api.stripe.com/v1/charges',data);
Logger.log(result);

Upvotes: 3

Related Questions