create a customer in stripe node.js
To create a customer in Stripe using Node.js, you can follow these steps:
- Install the Stripe package: Start by installing the Stripe package in your Node.js project. Open your terminal or command prompt and run the following command:
npm install stripe
- Import the Stripe module: In your JavaScript file, import the Stripe module by requiring it at the top of your code:
const stripe = require('stripe')('YOUR_SECRET_API_KEY');
Replace 'YOUR_SECRET_API_KEY'
with your actual Stripe secret API key. You can find this key in your Stripe dashboard under "Developers" > "API Keys".
- Create a customer object: Use the
stripe.customers.create
method to create a new customer in Stripe. Pass an object with the required parameters, such asemail
andname
, to create the customer. Here's an example:
const customer = await stripe.customers.create({
email: '[email protected]',
name: 'John Doe',
});
You can also include additional optional parameters like description
or metadata
to provide more information about the customer if needed.
- Handle the response: The
create
method returns a Promise, so you can useasync/await
or.then()
to handle the response. Here's an example usingasync/await
:
try {
const customer = await stripe.customers.create({
email: '[email protected]',
name: 'John Doe',
});
console.log('Customer created:', customer);
} catch (error) {
console.error('Error creating customer:', error);
}
In this example, the created customer object will be logged to the console if the creation is successful. If there is an error, it will be caught and logged as well.
That's it! You have successfully created a customer in Stripe using Node.js. Remember to handle any errors appropriately and to replace 'YOUR_SECRET_API_KEY'
with your actual Stripe secret API key.