'How to use Stripe's secret key and publishable key?

I would like to connect with the Stripe API using a https call using the https library.

var https = require('https');

I have gotten the secret key and publishable key and put it inside a object:

var stripe = {
  secret_key: 'secret_key_given_in_the_dashboard',
  publishable_key: 'publishable_key_given_in_the_dashboard'
}

I am now in the part of creating the requestDetail object:

var requestDetails = {
    'protocol'  : 'https:',
    'hostname'  : 'api.stripe.com',
    'method'    : 'POST', //WHICH OF POST GET PUT DELETE SHOULD I USE?
    'path'      : '???????????????????????',// WHICH ENDPOINT SHOULD I USE?
    'auth'      : '???????????????????????',// SHOULD I USE THE SECRET AND PUBLISHABLE KEY HERE?
    'headers'   : {
      'Content-Type'  : 'application/x-www-form-urlencoded',
      'Content-Length' : Buffer.byteLength(stringPayload)
    }
  };

I plan to make use of the requestDetails object in the call using https:

var req = https.request(requestDetails, function(res){
      // Grab the status of the sent request
      var status = res.statusCode;
      //Callback successfully if the request went through
      if(status == 200 || status == 201) {
        callback(false);
      } else {
        callback('Status code returned was ' + status);
      }
    });

Where and how should I use the secret key and publishable key in order to make a call to the stripe API? Which endpoint? Which method (POST, GET, PUT,or DELETE)?

I would like to eventually create a order and pay through the STRIPE api. But for now just any authenticated call through the stripe api will do as I need a sample format that works.... I'm not too sure where to add the secret key and publishable key....



Solution 1:[1]

You should install official stripe package (source: https://github.com/stripe/stripe-node), require the package and authenticate it using your secret key ( example from the github docs):

const stripe = require('stripe')('your_stripe_secret_key');

stripe.customers.create({
  email: '[email protected]',
})
.then(customer => console.log(customer.id))
.catch(error => console.error(error));

The package is an abstraction to make the API requests for you.

More docs: https://stripe.com/docs/api?lang=node

However, if you want to use the https directly for Stripe API requests, which is not recommended, you can check the docs and examples for using the cURL, since it shows the endpoints for each example.

https://stripe.com/docs/api/authentication?lang=curl

Solution 2:[2]

try using fetch, 'Authorization': 'Bearer ' + sk.

My working example of retrieving a customer based on the customer_id:

const url = `https://api.stripe.com/v1/customers/${stripe_customer_id}`;
return await fetch(url, {
  method: "get",
  headers: {
    "Content-Type": "application/json",
    'Authorization': 'Bearer ' + sk,
  }
})
  .then(function(response) {
    return response.json();
  })
  .then(function(response) {
    // console.log(response);
    return response;
  });
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikola Kirincic
Solution 2 Tyler2P