Stripe Node.js Library instance initialization and encapsulation in fastify framework.
Install the package with:
npm i stripe fastify-stripe --save
The package needs to be added to your project with register
and you must at least configure your account's secret key wich is available in your Stripe Dashboard then call the Stripe API and you are done.
const fastify = require('fastify')({ logger: true })
fastify.register(require('fastify-stripe'), {
apiKey: 'sk_test_...'
})
fastify.get('/customers/add', async (request, reply) => {
const { stripe } = fastify
const email = 'customer@exemple.com'
try {
// We create a new customer using Stripe API
const customers = await stripe.customers.create({ email })
reply.code(201)
return {
status: 'ok',
message: `customer ${email} succesfully added`,
customers
}
} catch (err) {
reply.code(500)
return err
}
})
fastify.listen(3000, (err) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
and it works using Promises
too:
const fastify = require('fastify')({ logger: true })
fastify.register(require('fastify-stripe'), {
apiKey: 'sk_test_...'
})
fastify.get('/customers/add', function (request, reply) {
const { stripe } = fastify
const email = 'customer@exemple.com'
// We create a new customer using Stripe API
stripe.customers.create({ email })
.then((customers) => {
reply
.code(201)
.send({
status: 'ok',
message: `customer ${email} succesfully added`,
customers
})
})
.catch((err) => {
reply
.code(500)
.send(err)
})
})
fastify.listen(3000, function (err) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
apiKey
[ required ]: Your account's secret key wich is available in your Stripe Dashboard
name
[ optional ]: Through this option fastify-stripe
lets you define multiple Stripe singular instances (with different options parameters if you wish) that you can later use in your application.
const fastify = require('fastify')({ logger: true })
fastify .register(require('fastify-stripe'), { apiKey: 'sktest...', name: 'test', timeout: 24000 // in ms (this is 24 seconds) }) .register(require('fastify-stripe'), { apiKey: 'skprod...', name: 'prod' })
fastify.get('/customers/test/add', async (request, reply) => { const { stripe } = fastify const email = 'customer@exemple.com'
try { // We create a new customer using the singular Stripe test instance // This instance has a request timeout of 4 minutes // and uses a Stripe test API key const customers = await stripe['test'].customers.create({ email })
reply.code(201)
return {
status: 'ok',
message: `customer ${email} succesfully added`,
customers
}
} catch (err) { reply.code(500) return err }
fastify.get('/customers/prod/add', async (request, reply) => { const { stripe } = fastify const email = 'customer@exemple.com'
try { // We create a new customer using the singular Stripe prod instance // This instance has the default request timeout of 2 minutes // and uses a Stripe production API key const customers = await stripe.prod.customers.create({ email })
reply.code(201)
return {
status: 'ok',
message: `customer ${email} succesfully added`,
customers
}
} catch (err) { reply.code(500) return err } })
fastify.listen(3000, (err) => { if (err) { fastify.log.error(err) process.exit(1) } })
* `maxNetworkRetries` **[ optional ]**: Automatic network retries can be enabled with setMaxNetworkRetries. This will retry requests `n` times with exponential backoff if they fail due to an intermittent network problem. Idempotency keys are added where appropriate to prevent duplication. You can later change this on a per-request basis with the new Stripe API configuration object property [`maxNetworkRetries`](https://github.com/stripe/stripe-node#network-retries).
```js
// Retry this request once before giving up
fastify.stripe.customers.create(
{
email: 'customer@example.com'
},
{
maxNetworkRetries: 1
}
)
timeout
[ optional ]: The request timeout is configurable and must be expressed in ms
. By default Stripe Node.js Library uses Node's default of 120 seconds ({ timeout: 120000 }
). You can later change this on a per-request basis with the new Stripe API configuration object property timeout
.
fastify.stripe.customers.create(
{
email: 'customer@example.com'
},
{
timeout: 20000 // in ms (this is 20 seconds)
}
)
apiVersion
[ optional ]: It is important for you to check what version of the Stripe REST API you're currently consuming (via the dashboard). You can explicitly set the version you wish to use like so: { apiVersion: '2020-08-27' }
. You can later change this on a per-request basis with the new Stripe API configuration object property apiVersion
.
fastify.stripe.customers.list({ apiVersion: '2020-08-27' })
Note: You don't need to set a version explicitly. If you don't set it the Stripe REST API will use your account's default, which you can view under your dashboard (Account Settings » API Keys). Setting a version explicitly does not affect your account's default version. It'll only affect the API you consume through that singular stripe instance.
Note for TypeScript users: If you are a TypeScript user, follow Stripe Node.js Library maintainers recommendations: "we recommend upgrading your account's API Version to the latest version.
If you wish to remain on your account's default API version, you may pass null
or another version instead of the latest version, and add a @ts-ignore
comment here and anywhere the types differ between API versions."
You can see the other options in Node Stripe config object initialization documentation.
See the Node Stripe API docs.
This project is kindly sponsored by coopflow.
Licensed under MIT