npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-stripe

v2.4.1

Published

Stripe Node.js Library instance initialization and encapsulation in fastify framework

Downloads

171

Readme

fastify-stripe

NPM version GitHub CI Coverage Status js-standard-style

Stripe Node.js Library instance initialization and encapsulation in fastify framework.

Install

Install the package with:

npm i stripe fastify-stripe --save

Usage

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 = '[email protected]'

  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 = '[email protected]'

  // 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)
  }
})

Options

  • 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: 'sk_test_...',
    name: 'test',
    timeout: 24000 // in ms (this is 24 seconds)
  })
  .register(require('fastify-stripe'), {
    apiKey: 'sk_prod_...',
    name: 'prod'
  })

fastify.get('/customers/test/add', async (request, reply) => {
  const { stripe } = fastify
  const email = '[email protected]'

  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 = '[email protected]'

  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.
// Retry this request once before giving up
fastify.stripe.customers.create(
  {
    email: '[email protected]'
  },
  {
    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: '[email protected]'
  },
  {
    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.

Documentation

See the Node Stripe API docs.

Acknowledgements

This project is kindly sponsored by coopflow.

License

Licensed under MIT