Search code examples
node.jssquare-connectunirest

How to create Square Customer with Node.js and Unirest


I am trying to create a new Square user, but am having problems. I was able to use Unirest to GET a Square customer list at the https://connect.squareup.com/v2/customers endpoint, but haven't been able to successfully POST.

const unirest = require('unirest');
const access_token = 'sq-123...';

var createCustomer = 
unirest.post('https://connect.squareup.com/v2/customers')
  .headers({
    "Accept": "application/json",
    "Authorization": "Bearer " + access_token})
  .send({
    "email_address": "[email protected]"
  })
  .end(function (createCustomer) {
    console.log(createCustomer);
});

The response returns

raw_body: 
'{"errors":[{
"category":"INVALID_REQUEST_ERROR",
"code":"BAD_REQUEST",
"detail":"invalid character \'e\' looking for beginning of value (line 1, character 1)"}]}' }

I also tried using .field instead of .send, which returned

raw_body: '{"errors":
[{"category":"INVALID_REQUEST_ERROR",
"code":"BAD_REQUEST",
"detail":"invalid character \'-\' in numeric literal (line 1, character 2)"}]}' }

Is it possible to create a customer using Unirest and Node? If so, where am I going wrong?


Solution

  • The following code will POST

    const unirest = require('unirest');
    const access_token = 'sq-123...';
    
    var createCustomer = 
    unirest.post('https://connect.squareup.com/v2/customers')
      .headers({
        "Content-Type": "application/json",
        "Authorization": "Bearer " + access_token})
      .send({
        "email_address": "[email protected]"
      })
      .end(function (createCustomer) {
        console.log(createCustomer);
    });