Search code examples
jsonapioauth-2.0zapierxero-api

How to build custom Zapier integration for Xero


I am attempting to build my own integration in zapier that will allow me to create quotes in Xero (a feature not currently supported natively). I've been using this this post and this reference to help me.

I've gotten to the point where I'm creating the action and testing it with test data. Unfortunately, the response I get is "Got 400 calling POST https://identity.xero.com/connect/token, expected 2xx." Perhaps I'm sending the json data incorrectly. I've tried using the 'pretty' and 'raw' ways of sending data:

Could a zapier "expert" help me with this? Perhaps by creating their own xero integration?

EDIT

enter image description here enter image description here enter image description here enter image description here

Not sure if necessary, but blocked out the IDs. Although I now see that I didn't do that for the contactID in the first post lol... enter image description here


Solution

  • Here is how to get it done, but remember that you will need to have a search action to find information for the required ID's. Given your error I think the problem is that you did not have the tenantId that should be defined in your header like so: 'xero-tenant-id': 'YOURNUMBERHERE'. See step 8 below to compare it to yours.

    In case you can't find it, these are the steps I took:

    XERO

    1. Create Account
    2. Create Xero App and add the Zapier OAuth Redirect URL to the Xero redirect section(from your 'Zapier Dev' app on 'step 2').

    ZAPIER

    1. In your dev app, add the CLient ID & Secret from xero to the appropriate sections in 'Zapier Dev step 3'
    2. Add the POST endpoint (requested in 'Zapier Dev step 4') https://login.xero.com/identity/connect/authorize with the HTTP headers:

      response_type: code
      client_id: {{process.env.CLIENT_ID}}
      redirect_uri: {{bundle.inputData.redirect_uri}}
      state: {{bundle.inputData.state}}
      
    3. Add the scope: openid profile email accounting.transactions

    4. Refresh token ('Zapier Dev step 4: Access Token') can be obtained using this:

      REFRESH TOKEN: POST https://identity.xero.com/connect/token
      TEST CALL: GET https://api.xero.com/connections
      

      -Keep the returned tenantId for later use

      -Test the authentication. Does it work? If yes, move on to step 7.

      -If test fails: review for typos, check for correct url's and make sure your Headers match what xero requires (see link at bottom).

    5. Add Action called createQuote

      -Add input contactID

      -Add input lineitem with label of description

      -Add input tenantId

    6. Add POST to API Config at url https://api.xero.com/api.xro/2.0/Quotes

      Example POST:

      const options = {
        url: 'https://api.xero.com/api.xro/2.0/Quotes/',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Authorization': `Bearer ${bundle.authData.access_token}`,
          'xero-tenant-id': bundle.inputData.tenantID
        },
        params: {
        },
        body: {
          "Contact": { 
          "ContactID": bundle.inputData.ContactID
          },
          "Date": "2019-11-29",
          "LineItems": [
          {
            "Description": bundle.inputData.LineItems
          }
          ]
        }
      }
      
      return z.request(options)
        .then((response) => {
          response.throwForStatus();
          const results = z.JSON.parse(response.content);
          return results;
        });
      
    7. Plug in the test contactID, tenantID, lineitems and test it out

    After completing this you will need to create a search action to grab the contactID and tenantID if you want it all automated. If you have problems I found the start-up doc to be useful.