Keith Horwood

@keithwhor

Build a “Serverless” Stripe Store in 5 Minutes with Node.js and StdLib

Stripe is redefining e-commerce. Patrick and John Collison had a very specific insight into the future of internet businesses: primarily, they’re driven by the software developers who build them. They have, along with one of the most talented, creative teams in Silicon Valley, created a $9.2B business with powerful financial APIs as the centerpiece.

At StdLib (“Standard Library”), we share a similar vision of the future of business. Our goal is to lower the barrier to entry to creating web products based on new “serverless” technology. We make it even easier for a broader class of developer to build backend services and integrate with companies like Slack to build bots, and Twilio to build messaging hubs. Today we’re pleased to show you how in only a few minutes, you can build a production-ready Stripe-powered store that is fully-customizable using StdLib.

StdLib ❤ Stripe

We think you’ll find with StdLib that you’ve never had an easier time managing web services. You only have to think in functions, and we have a suite of powerful command line tools that allow for rapid local development and testing.

Once you’ve created your StdLib service in Minute 5, you’ll get a README.mdfile but can also follow along via GitHub in the stdlib/utils.templates stripe folder.

What You’ll Need Beforehand

  • 1x Stripe Account (We recommend a new account)
  • 1x Command Line Terminal
  • 5x Minutes (or 300x Seconds)

Minute 1: Preparing Your Stripe Account

First, make sure you’ve created a new Stripe account. We recommend creating a new account because your StdLib store will spontaneously create new products and update your Stripe product information based on configuration you’ve set. To prevent side effects on an already existing account, it’s safest to use a new account.

Once you have your Stripe account, visit the Stripe Dashboard. You don’t need an activated account yet, you’re okay to just use test data to get up and running. Click API in the bottom left of the menu to take you to the API keys page and then Reveal test key token. Keep this page open, you’ll need these API keys shortly.

Minute 2: Create a StdLib Account

You’ll now need a StdLib Account to create your “serverless” Stripe Store. We don’t need anything other than a StdLib account and Stripe account to create a store, as Stripe stores product information for us. Navigate to https://stdlib.com and click Sign Up to create your account.

Click “Sign Up” to Proceed

Minute 3: Initialize Your StdLib Workspace

To use StdLib, we recommend using the latest stable version of Node.js, ideally 8.2.x or higher installed from the Official Node.js Website. Once complete, open up your Terminal or Command Line and install the StdLib Command Line Tools with the following:

$ npm install lib.cli -g

Please make sure you are running the latest version. At least 3.1.1, you can check with lib version. You should now have access to the lib command in your terminal. You can use this to make a stdlib directory for your StdLib services.

$ mkdir stdlib
$ cd stdlib
$ lib init

Use the StdLib credentials you created in the previous step to log in. That’s it, you’re done!

Minute 4: Create Your Stripe Store

The next step is creating your Stripe Store using StdLib. You can use the stripe template to automatically generate your Stripe Store for you.

$ lib create -s @stripe/store

When you proceed, you’ll be asked for a Service Name, we suggest stripe-store, but you can name it whatever you’d like.

By specifying -s @stripe/store, you’re telling the Standard Library template service to use the Stripe boilerplate (other options include@slack/app). This will automatically populate your service with the code you need to run your store, and is fully hackable.

To visit the service you’ve just created, enter the following into your terminal:

$ cd <username>/stripe-store

To make sure your Stripe Store is working as expected, try running a HTTP gateway for your StdLib function locally:

$ lib http

You’ll see the following:

To visit your Stripe Store and make sure it works, copy and paste the URL provided, localhost:8170/<username>/stripe-store/ into your web browser:

Awesome! Your Stripe Store is almost ready to go. You can stop your HTTP server at any time by pressing ctrl+C or ctrl+D in your terminal.

Minute 5: Set Stripe Credentials and Deploy

Remember your Stripe credentials from Minute 1? You’ll want to make sure you bring up that window or open the API keys page again. Open up ./env.json in your service directory, and enter your credentials into the "local" and "dev" environments.

Enter Your Credentials

When you’re ready to deploy to production, you can put your production credentials in "release", but it’s not necessary right now.

You’re now ready to deploy your Stripe Store! Simply open your terminal and type:

$ lib up dev

You’ll notice something similar to the following output:

There’s a deploy script in ./stripe/scripts/ that will automatically read from ./stripe/products.json and update your Stripe product line accordingly, based on the JSON configuration. It runs a diff algorithm to make sure nothing is deleted, only either deactivated, updated or created.

products.json contains your Stripe Products

After deploying, you’ll be provided with a link to your new Store:

A link to your Store via HTTPS

This is the last thing you need to access your Stripe store.

That’s it! Your Stripe Store is live!

Visit your Stripe Store by copying the link provided in the last step, typically https://<username>.lib.id/stripe-store@dev/. You’ll see your default products from products.json all ready to go!

Your Stripe Store is Live!

You can test placing orders with any e-mail, name, address and the credit card number 4242 4242 4242 4242 along with any expiry date (in the future) and any CVV. You can see your products in the Stripe Dashboard at any time on the Stripe Products Dashboard Page. You can update these manually, or in your ./stripe/products.json file and do a new deploy.

Your Products on your Stripe Dashboard

To release to production, activate your Stripe account from your Stripe Dashboard, switch your environment to live and copy your new API keys to your "release" keys in ./env.json. You’ll have to use the $ lib release command instead of $ lib up dev.

Additionally, your Stripe store is fully hackable! Change any of the HTML, styles, etc. to your heart’s content. Simply modify any of the files in your service package, test locally with $ lib http, and deploy new products / etc. when you’re ready.

Thank You!

Thanks for reading, and we’re excited about the possibilities of things you can create with Stripe and StdLib! For suggestions or questions, feel free to comment here or visit the stdlib/lib repository on GitHub. If you have a neat idea, reach out to me directly by e-mail: keith [at] stdlib [dot] com, send me a tweet on Twitter, @keithwhor, or follow us on Twitter, @StdLibHQ.

We look forward to hearing from you, and happy building!

More by Keith Horwood

Topics of interest

More Related Stories