An example walkthrough with step-by-step instructions for using the Square Subscriptions API.
Subscriptions API

Walkthrough: Create a Subscription Plan and Manage Subscriptions

In this cURL-based walkthrough, you set up a subscription program for a gym membership, create customer subscriptions, and verify customer billing. You also test both scenarios: sending a subscription invoice to a customer's email address and charging a customer's card on file. You test the walkthrough in the Square Sandbox.

To charge a customer for the subscription, you must add a card on file. To add a card on file, you use a fake payment token ("cnon:card-nonce-ok") that Square provides for Sandbox testing. The resulting card on file is a fake card that is never charged. For more information, see Sandbox Test Values.

The cURL commands in this exercise specify the connect.squareupsandbox.com domain in the endpoint URLs for Sandbox testing. In production, the domain is connect.squareup.com.

Follow these steps to gather account information for your Sandbox environment:

  1. Sign in to the Developer Dashboard.

  2. Open one of your applications, which provides you with the credentials you use. If you do not have an application, you first create an application.

  3. Get Sandbox credentials:

    1. On the Credentials page, you can toggle between the Sandbox mode and Production mode. Choose Sandbox.

    2. Copy the access token from Sandbox Access Token.

  4. Get a location ID. When creating subscriptions, you provide a seller location ID where the subscription was created.

    1. In the left pane, choose Locations.

    2. On the Locations page, choose a location ID.

You can create subscriptions only for customers that have a profile in the seller's Customer Directory. In this step, create two customers and add a card on file for one of these customers.

You must provide a valid email address when creating these customers. Square emails the subscription invoices and receipts to this email address.

  1. Create a customer:

    1. Call CreateCustomer to create a customer.

      Create Customer
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      curl https://connect.squareupsandbox.com/v2/customers \
        -X POST \
        -H 'Square-Version: 2023-05-17' \
        -H 'Authorization: Bearer {ACCESS_TOKEN}' \
        -H 'Content-Type: application/json' \
        -d '{
          "family_name": "Doe",
          "idempotency_key": "{UNIQUE_KEY}",
          "email_address": "[email protected]",
          "given_name": "John"
        }'
    2. In the response, note the customer ID. You provide this ID when creating a subscription.

  2. Create another customer. This time add a card on file for this customer. Make sure to provide a valid email address to receive emails.

    1. Call CreateCustomer to create a customer.

      Create Customer
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      curl https://connect.squareupsandbox.com/v2/customers \
        -X POST \
        -H 'Square-Version: 2023-05-17' \
        -H 'Authorization: Bearer {ACCESS_TOKEN}' \
        -H 'Content-Type: application/json' \
        -d '{
          "family_name": "Doe",
          "idempotency_key": "{UNIQUE_KEY}",
          "email_address": "[email protected]",
          "given_name": "Jane"
        }'
    2. In the response, note the customer ID. You need this ID when adding a card on file and creating a subscription.

    3. Call CreateCard to add a card on file using the payment token ("cnon:card-nonce-ok") that Square provides for Sandbox testing.

      Create Card
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      curl https://connect.squareupsandbox.com/v2/cards \
        -X POST \
        -H 'Square-Version: 2023-05-17' \
        -H 'Authorization: Bearer {ACCESS_TOKEN}' \
        -H 'Content-Type: application/json' \
        -d '{
          "idempotency_key": "{UNIQUE_KEY}",
          "source_id": "cnon:card-nonce-ok",
          "card": {
            "billing_address": {
              "address_line_1": "500 Electric Ave",
              "address_line_2": "Suite 600",
              "locality": "New York",
              "administrative_district_level_1": "NY",
              "postal_code": "94103",
              "country": "US"
            },
            "cardholder_name": "Jane Doe",
            "customer_id": "{CUSTOMER_ID}",
            "reference_id": "alternate-id-1"
          }
        }'
    4. In the response, note the card ID. When adding a subscription for this customer, you provide this card ID so that Square can charge the subscription fee to this card.

Call the UpsertCatalogObject to create a subscription plan. In the request:

  • name specifies the gym membership.

  • phases includes two subscription phases.

    • Phase 1 offers a significant discount. It specifies a monthly cadence with only $1 (price_money). Also, the phase period value is set to 1, indicating a month-long phase. For more information about cadence, see Set up a subscription plan.

      Note

      You can set the price to 0 to offer a free initial phase. In this case, Square does not send an invoice until the initial free period is over. Therefore, the price is set to $1 so you can immediately verify the invoice.

    • Phase 2 identifies the weekly cadence with price_money set to $15. That is, customers pay a $15 weekly subscription fee. The phase does not specify the period, which indicates a continuous subscription.

      Upsert Catalog Object
      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      curl https://connect.squareupsandbox.com/v2/catalog/object \
        -X POST \
        -H 'Square-Version: 2023-05-17' \
        -H 'Authorization: Bearer {ACCESS_TOKEN}' \
        -H 'Content-Type: application/json' \
        -d '{
          "idempotency_key": "{UNIQUE_KEY}",
          "object": {
            "type": "SUBSCRIPTION_PLAN",
            "id": "#plan",
            "subscription_plan_data": {
              "name": "Multiphase Gym Membership",
              "phases": [
                {
                  "cadence": "MONTHLY",
                  "periods": 1,
                  "recurring_price_money": {
                    "amount": 100,
                    "currency": "USD"
                  }
                },
                {
                  "cadence": "WEEKLY",
                  "recurring_price_money": {
                    "amount": 1500,
                    "currency": "USD"
                  }
                }
              ]
            }
          }
        }'

      The following is an example response:

      • 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
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      {
        "catalog_object": {
          "type": "SUBSCRIPTION_PLAN",
          "id": "NS6YKIV4CWFGHQCA51111111",
          "updated_at": "2020-07-14T15:41:59.777Z",
          "version": 1594741319777,
          "is_deleted": false,
          "present_at_all_locations": true,
          "subscription_plan_data": {
            "name": "Multiphase Gym Membership",
            "phases": [
             {
                "uid": "2Z3OKONRGHLUEJL2FSYBH4V3",
                "cadence": "MONTHLY",
                "periods": 1,
                "recurring_price_money": {
                  "amount": 100,
                  "currency": "USD"
                },
                "ordinal": 0
              },
              {
                "uid": "7QEXRL7ID2RNCELANGPSO7JC",
                "cadence": "WEEKLY",
                "recurring_price_money": {
                  "amount": 1500,
                  "currency": "USD"
                },
        "ordinal": 1
              }
            ]
          }
        },
        "id_mappings": [
          {
            "client_object_id": "#plan",
            "object_id": "NS6YKIV4CWFGHQCA5D6NBIE3"
          }
        ]
      }
      

Create subscriptions for the two customers you created. One of the customers has a card on file so you include that card ID in the CreateSubscription request.

  1. Make sure you have the following information:

    • Subscription plan ID

    • Customer ID

    • Card ID for the card on file

    • Location ID

  2. Call CreateSubscription to create a subscription for the customer without a card on file.

    Create Subscription
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    curl https://connect.squareupsandbox.com/v2/subscriptions \
      -X POST \
      -H 'Square-Version: 2023-05-17' \
      -H 'Authorization: Bearer {ACCESS_TOKEN}' \
      -H 'Content-Type: application/json' \
      -d '{
        "idempotency_key": "{UNIQUE_KEY}",
        "location_id": "{LOCATION_ID}",
        "plan_id": "{SUBSCRIPTION_PLAN_ID}",
        "customer_id": "{CUSTOMER_ID}"
      }'

    The following is an example response:

    In production, after the subscription is created, Square sends an invoice to the customer's email address (the Square Sandbox does not send emails). An example is shown as follows:

    An example invoice that Square emails to customers, which includes a "Pay Invoice" button that opens to the invoice payment page.

    The customer can choose Pay Invoice to open the Square-hosted invoice page to pay the invoice. The seller also receives a similar email indicating an invoice is sent.

  3. Call CreateSubscription to create a subscription for the customer with a card on file. In the request, card_id is the card ID.

    Create Subscription
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    curl https://connect.squareupsandbox.com/v2/subscriptions \
      -X POST \
      -H 'Square-Version: 2023-05-17' \
      -H 'Authorization: Bearer {ACCESS_TOKEN}' \
      -H 'Content-Type: application/json' \
      -d '{
        "idempotency_key": "{UNIQUE_KEY}",
        "location_id": "{LOCATION_ID}",
        "plan_id": "{SUBSCRIPTION_PLAN_ID}",
        "customer_id": "{CUSTOMER_ID}",
        "card_id": "{CARD_ID}"
      }'

    The following is an example response:

    If you test this in production, after charging the customer's card on file, Square sends a receipt to the customer's email address.

  4. Verify the invoices in the Seller Dashboard.

    1. Sign in to the Developer Dashboard.

    2. In the Sandbox Test Accounts section, choose open for the Default Test Account.

    3. In the left pane, choose Invoices, and then verify that the two invoices (paid and unpaid) are present.

We've made improvements to our docs.
Prefer the old format?