Back to home

Create Stripe Subscription Products From The CLI main image

Create Stripe Subscription Products From The CLI

This post displays an overview of some code that can be used to create Stripe subscription products from the command line.

Prerequisites

  1. Basic familiarity with the Stripe API.
  2. Basic familiarity with TypeScript.

Getting Started

You will need to set up an npm project that is setup to run TypeScript files (I will be omitting this).

Afterwards, we will need to install some packages:

$ yarn add -D prompts @types/prompts stripe

Writing our script

The script that we are writing is a TypeScript script that can be used to create new Stripe subscription products.

It will enable us to interactively add a product name, price and currency (in this case, USD or AUD).

/** * Playground to test Stripe helpers. * @ see https://dashboard.stripe.com/test/customers/cus_JRZnw3LaBfwvI1 * */ import prompts from "prompts"; import path from "path"; import Stripe from "stripe"; // Locking the API version at the one I used. const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { apiVersion: "2020-08-27", }); // Stripe abstractions for creating the product and price. async function createProduct(options: Stripe.ProductCreateParams) { return stripe.products.create(options); } async function createPrice(options: Stripe.PriceCreateParams) { return stripe.prices.create(options); } /** * What you need to do: * * 1. Create a product * 2. Create a price with a product * 3. Create a subscription with the price and customer id */ export async function createNewSubscriptionProduct() { const { newProductName, price, currency, interval } = await prompts([ { type: "text", name: "newProductName", message: "What is the new product name?", }, { type: "number", name: "price", message: "What is the product price?", }, { type: "select", name: "currency", message: "What currency?", choices: [ { title: "aud", value: "aud", }, { title: "usd", value: "usd" }, ], }, { type: "select", name: "interval", message: "What interval would you like to set?", choices: [ { title: "Monthly", value: "month", }, { title: "Yearly", value: "year" }, ], }, ]); const productRes = await stripeHelpers.createProduct({ name: newProductName, }); const priceRes = await stripeHelpers.createPrice({ currency: currency, unit_amount: price, recurring: { interval, }, product: productRes.id, }); } async function main() { await createNewSubscriptionProduct(); } main();

You will need to set the STRIPE_SECRET_KEY environment variable to your Stripe secret key for runtime of the script.

If we run the script, we will be asked to provide some information and the successful process will create a new SaaS product that you can view on your Stripe dashboard.

Summary

Today's post demonstrated how to create a new Stripe subscription product from the command line.

The same code can be reused within your own server-side applications if you wish to do a similar thing from your own applications.

Resources and further reading

  1. Stripe API docs

Photo credit: badashphotos

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.