Python SDK Returning INVALID_REQUEST_ERROR When I Create a Customer

I’m using the Python SDK to create a customer profile from information retrieved from a Google Form. Using gspread, I’m retrieving info with this code segment:
firstName = worksheet.acell(‘B2’).value
lastName = worksheet.acell(‘C2’).value
email = worksheet.acell(‘D2’).value
phoneNum = worksheet.acell(‘E2’).value
address = worksheet.acell(‘F2’).value

I’m then turning those into a customer body using:
customerBody = {
“given_name”: firstName,
“family_name”: lastName,
“email_address”: email,
“phone_number”: phoneNum,
‘address’: address
}

Then I use the command:
custCreate = customers_api.create_customer(customerBody)
And I get this error line:
[{‘code’: ‘BAD_REQUEST’, ‘detail’: ‘Expected “{” (line 1, character 126)’, ‘category’: ‘INVALID_REQUEST_ERROR’}]

Any help is appreciated.

The error you’re encountering is likely due to the format of the customerBody dictionary you’re sending to the create_customer method of the Square Python SDK. The address field in the Square API expects a more structured format rather than a simple string.

The address should be a dictionary containing specific keys such as address_line_1, locality, administrative_district_level_1, postal_code, and country. Here’s how you can structure your code to correctly format the address and create the customer profile:

Step-by-Step Solution

  1. Retrieve and Parse Address Information: Ensure that the address information from your Google Form is parsed into the required components.
  2. Format the Address: Create a dictionary for the address with the required keys.
  3. Create the Customer Body: Ensure that the customerBody dictionary matches the expected structure of the Square API.

Updated Code Example

Assuming the address field from the Google Form contains a full address, you will need to parse it into its components. For simplicity, let’s assume the address is already separated into its components in your Google Form:

import gspread
from square.client import Client

# Initialize the Square client
client = Client(access_token='YOUR_ACCESS_TOKEN')
customers_api = client.customers

# Retrieve information from Google Sheets
worksheet = gspread.service_account().open('Your Google Sheet Name').sheet1
firstName = worksheet.acell('B2').value
lastName = worksheet.acell('C2').value
email = worksheet.acell('D2').value
phoneNum = worksheet.acell('E2').value

# Assuming the address is split into components in your Google Form
address_line_1 = worksheet.acell('F2').value
locality = worksheet.acell('G2').value
administrative_district_level_1 = worksheet.acell('H2').value
postal_code = worksheet.acell('I2').value
country = worksheet.acell('J2').value

# Create the address dictionary
address = {
    'address_line_1': address_line_1,
    'locality': locality,
    'administrative_district_level_1': administrative_district_level_1,
    'postal_code': postal_code,
    'country': country
}

# Create the customer body
customerBody = {
    'given_name': firstName,
    'family_name': lastName,
    'email_address': email,
    'phone_number': phoneNum,
    'address': address
}

try:
    # Create the customer
    custCreate = customers_api.create_customer(customerBody)
    print(custCreate.body)
except Exception as e:
    print(f"An error occurred: {e}")

Explanation

  1. Square Client Initialization: Ensure that you initialize the Square client with your access token.
  2. Google Sheets Data Retrieval: Use gspread to retrieve the necessary data from your Google Sheets.
  3. Address Parsing: Split the address into its components (e.g., address_line_1, locality, etc.). This example assumes the address components are in separate cells.
  4. Address Dictionary: Create a dictionary for the address with the required keys.
  5. Customer Body: Create the customerBody dictionary with the structured address.
  6. Create Customer: Use the create_customer method to create the customer profile and handle any exceptions.

Notes

  • Address Components: Ensure that your Google Form collects address components separately or parse the full address string into its components.
  • Error Handling: The try-except block will help catch and print any errors that occur during the API request. :slightly_smiling_face:

I had completely forgotten that the address had to be a dictionary, not a string. That immediately solved the issue!

Glad to hear that solved the issue. :slightly_smiling_face: