Testing

Testing Square APIs with Command Line Tools

Learn how to use cURL or PowerShell to test Square APIs.

Before you start

The API call example in this article creates a new customer. To make the calls, you must have an access token. You can use a sandbox access token from the Application Dashboard or your personal access token to make the example API call.

API test tools

You can send requests to Connect API endpoints using REST-enabled command line tools:

  • cURL: A command line tool and library for getting and posting data with URL syntax that supports HTTP and HTTPS. If you use cURL, you will need to write and edit cURL call parameters. We recommend that you use a text editor. cURL commands can be scripted.

  • Windows PowerShell: An automation and configuration tool that is optimized for dealing with structured data such as JSON. PowerShell incorporates a command line shell, scripting language, and a framework to support cmdlets. PowerShell provides the Invoke-RestMethod cmdlet to make API calls using a syntax that is very similar to cURL. Invoke-RestMethod commands can be scripted.

Testing with Windows PowerShell

Step 1: Start PowerShell

You can start the Windows PowerShell command prompt by choosing from the following options:

  • Click Windows PowerShell from the Windows Start Menu
  • Press Windows + R keys together to open the Run dialog. Type powershell and press Enter

The PowerShell window is opened and the command prompt starts in your user directory.

Step 2: Declare call parameters

Open a text editor and copy the example code below to set the auth header, body declaration, and the Invoke-RestMethod call. Make sure you replace ACCESS_TOKEN with your access token.

## Create a new customer
$authHeader = @{ Authorization = 'Bearer  {0}' -f "ACCESS_TOKEN" }
$body = '{
      "given_name": "Amelia",
      "family_name": "Earhart",
      "email_address": "Amelia.Earhart@example.com",
      "address": {
        "address_line_1": "500 Electric Ave",
        "address_line_2": "Suite 600",
        "locality": "New York",
        "administrative_district_level_1": "NY",
        "postal_code": "10003",
        "country": "US"
      },
      "phone_number": "1-212-555-4240",
      "reference_id": "YOUR_REFERENCE_ID",
      "note": "#1 best customer"
    }'

Invoke-RestMethod -Uri https://connect.squareup.com/v2/customers |
   -Method Post |
   -ContentType "application/json" |
   -Headers $authHeader |
   -Body $body |
   -OutFile customer.json

Step 3: Make a call to the Customers endpoint

Copy the updated declarations and Invoke-RestMethod call to the PowerShell prompt and press enter. The $response object contains the new customer with server-assigned ID.

Testing with cURL

The following steps are completed at a macOS terminal command prompt.

Step 1: Start a terminal window

  • Press command + space keys together to open the Spotlight Search dialog. Type terminal and press Enter

A Terminal window is opened and the command prompt starts in your user directory.

Step 2: Declare request parameters

Open a text editor and copy the example code below to set the auth header, and the body declarations. Make sure you replace ACCESS_TOKEN with your access token.

## Create a new customer
curl https://connect.squareup.com/v2/customers \
 -H 'Content-Type: application/json'                       \
 -H 'Authorization: Bearer ACCESS_TOKEN'     \
 -d '{
  "given_name": "Amelia",
  "family_name": "Earhart",
  "email_address": "Amelia.Earhart@example.com",
  "address": {
    "address_line_1": "500 Electric Ave",
    "address_line_2": "Suite 600",
    "locality": "New York",
    "administrative_district_level_1": "NY",
    "postal_code": "10003",
    "country": "US"
  },
  "phone_number": "1-212-555-4240",
  "reference_id": "YOUR_REFERENCE_ID",
  "note": "#1 best customer"
}'

Step 3: Make a call to the Customers endpoint

Copy the updated cURL call to the terminal prompt and press enter.

The response contains the new customer with server-assigned ID.

{
    "customer": {
        "id": "SERVER_ASSIGNED_ID",
        "created_at": "2019-03-20T18:47:54.454Z",
        "updated_at": "2019-03-20T18:47:54Z",
        "given_name": "Amelia",
        "family_name": "Earhart",
        "email_address": "Amelia.Earhart@example.com",
        "address": {
            "address_line_1": "500 Electric Ave",
            "address_line_2": "Suite 600",
            "locality": "New York",
            "administrative_district_level_1": "NY",
            "postal_code": "10003",
            "country": "US"
        },
        "phone_number": "1-212-555-4240",
        "reference_id": "YOUR_REFERENCE_ID",
        "note": "#1 best customer",
        "preferences": {
            "email_unsubscribed": false
        },
        "groups": [
            {
                "id": "6VEKB6X1E99V8.REACHABLE",
                "name": "Reachable"
            }
        ],
        "creation_source": "THIRD_PARTY"
    }
}
Prev
< Testing Square APIs with Postman
Next
Running Local Web Servers >

Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow