Learn how to quickly set up and test the Square Ruby SDK.
SDKs and Samples

Square Ruby SDK Quickstart

Learn how to quickly set up and test the Square Ruby SDK.

Before you begin, you need a Square account and account credentials. You use the Square Sandbox for the Quickstart exercise. Do the following:

  1. Create a Square account and an application. For instructions, see Create an account and application.

  2. Get a Sandbox access token from the Developer Dashboard. For instructions, see Step 2.1: Make a test API call and review your account.

  3. Install the following:

    • Ruby. Square supports Ruby version 2.7 or later.

    • Square Ruby SDK. To install it, use the gem command:

Create a new directory for your project and then go to that directory:

  1. In your project directory, create a new file named quickstart.rb with the following content:

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    require 'square'
    
    client = Square::Client.new(
      access_token: ENV.fetch('SQUARE_ACCESS_TOKEN'),
      environment: 'sandbox',
      timeout: 1
    )
    
    result = client.locations.list_locations
    
    if result.success?
      result.data.locations.each do |location|
        printf("%s: %s, %s, %s\n",
               location[:id],
               location[:name],
               location[:address][:address_line_1],
               location[:address][:locality])
      end
    
    elsif result.error?
      result.errors.each do |error|
        warn error[:category]
        warn error[:code]
        warn error[:detail]
      end
    end
    
  2. When finished, save the quickstart.rb file.

The Ruby code in this Quickstart reads your Square Sandbox access token from the SQUARE_ACCESS_TOKEN environment variable. This helps avoid the use of hardcoded credentials in the code.

For the following commands, replace yourSandboxAccessToken with your Square Sandbox access token:

Run the following command:

Verify the result. You should see at least one location (Square creates one location when you create a Square account).

If you need more assistance, contact Developer Support or ask for help in the Developer Forums.