Create a Card on File from a Payment ID

Learn how to create a new card on file in a Square account using a payment ID as a source.

Applies to: Cards API | Customers API | Payments API

Link to section

Before you start

  • You need a valid access token - Access tokens determine the Square APIs you can call and the Square account that the call applies to. The steps in this topic call Square APIs in the Square Sandbox, so you can use your Sandbox access token in the requests. Square recommends testing with Sandbox credentials when possible. To call Square APIs in the production environment, change the base URL to https://connect.squareup.com and use production credentials.

    Applications that use OAuth access tokens require PAYMENTS_WRITE and CUSTOMERS_WRITE permissions to perform these steps.

  • You need a payment token - Payment tokens are generated by the Web Payments SDK or In-App Payments SDK using card information entered by buyers. To learn more, see Get a payment token.

Link to section

Get a payment token

Before saving a card on file, you need to get a valid single-use payment token (which represents a buyer's payment card) using the Web Payments SDK or In-App Payments SDK. You don't need to charge the buyer's card to save it. The payment token can be used to save a card on file, create a payment, or both.

Important

The postal code entered in the SDK's payment card form must match the postal code used in the CreateCard request.

You can use Sandbox test card information to generate a test payment token for this topic. Be sure to enter the postal code 10003 in the payment card form so it matches the example values used in this topic.

Link to section

Step 1: Create a payment

Call the CreatePayment endpoint to create a payment. Set source_id to the single-source payment token you generated for the card using the Web Payments SDK or In-App Payments SDK. Payment tokens are prefixed by cnon (for example, cnon:CBASEAZOKxw_kwAJQu3FI7dIkek).

Create payment

If successful, Square returns a Payment object, as shown in the following example response. Copy the payment ID so you can use it to create a card in step 3.

Link to section

Step 2: Create a customer

Call the CreateCustomer endpoint to create a customer to associate with the card.

Create customer

If successful, Square returns a Customer object, as shown in the following example response. Copy the customer ID so you can use it to create a card in step 3.

Link to section

Step 3: Create a card on file using the payment ID as the source

Call the CreateCard endpoint to save a card on file for the customer. Set source_id to the payment ID (from step 1) and card.customer_id to the customer ID (from step 2).

Create card

Important

Always ask customers for permission before saving their card information. For example, include a checkbox in your purchase flow that customers can select to specify that they want to save their card information for future purchases.

Linking cards on file without obtaining customer permission can result in your application being disabled without notice.

If successful, Square returns a Card object, as shown in the following example response:

Link to section

See also