In this short series, we are going to look at how to create a charge to Stripe in a number of their officially supported languages!

Today, we are going to look at how to do so with Ruby on Rails.

The expectations are that you have both Rails installed and have your Stripe API keys setup and ready to go.

The following comes in part from my documentation website.

Getting Started

Assuming you have Rails installed, run the following:

Add the following to the top of your for us to read local file values and bundle Stripe.

On the console, run .

Setting up your environment variables

For your Stripe account, add in your PK and SK test values.

Scaffolding the Charges Route

From the console run:

This will scaffold our controller.

Inside that, let’s update the code:

This code will make a charge to Stripe using the JSON body params and .

If the charge is successful, it will return the charge information as JSON.

Updating config/routes.rb

Ensure routes has the following for POST:

This ensures that we can send a POST request to when we run the server.

Running the code

Run to get our server up and running (defaulting to 3000).

In this example using HTTPie, call and we will get back our charge results sent as JSON. Hooray!

I chose to use HTTPie because I feel it is a fun tool that more should know about! Alternative, you could do the above using as well (or anything that can make a POST request for a matter of fact).

If you now go and check your Stripe dashboard, you will be able to see a charge.

Stripe Dashboard

Resources and further reading

  1. Using Rails for API only
  2. Stripe Ruby Github
  3. Dotenv Ruby Github
  4. Scaffolding routes
  5. HTTP Requests in Rails Apps
  6. Action Controlller Overview
  7. HTTPie

Image credit: Alexandru Acea

Originally posted on my blog. Follow me on Twitter for more hidden gems @dennisokeeffe92.

Senior Engineer @ UsabilityHub. Formerly Culture Amp.