# xero-node (QA Beta)

## Release of SDK with oAuth 2 support
Version 4.x of Xero NodeJS SDK only supports oAuth2 authentication and the following API sets.
* accounting
* bank feeds 

Coming soon
* fixed asset 
* files 
* payroll
* projects
* xero hq


## Usage
Installation
```sh
npm install xero-node-sdk
```

## Getting Started

### Create a Xero App
Follow these steps to create your Xero app

* Create a [free Xero user account](https://www.xero.com/us/signup/api/) (if you don't have one)
* Login to [Xero developer center](https://developer.xero.com/myapps)
* Click "Try oAuth2" link
* Enter your App name, company url, privacy policy url.
* Enter the redirect URI (this is your callback url - localhost, etc)
* Agree to terms and condition and click "Create App".
* Click "Generate a secret" button.
* Copy your client id and client secret and save for use later.
* Click the "Save" button. You secret is now hidden.

Example
```js
import { XeroClient } from "xero-node-sdk";

const client_id = 'YOUR-CLIENT_ID'
const client_secret = 'YOUR-CLIENT_SECRET'
const redirectUri = 'http://www.yourdomain.com/callback'
const scopes = 'openid profile email accounting.transactions offline_access'

const xero = new XeroClient({
        clientId: client_id,
        clientSecret: client_secret,
        redirectUris: [redirectUri],
        scopes: scopes.split(" ")
      });

// Get authorization for user
let consentUrl = await xeroClient.buildConsentUrl();

// Redirect user's browser to the consentUrl
// User will login to Xero and grant your app access
// User will be returned to the redirectUri (aka Callback URL)

//On callback - get token from Xero
//Get the query string (i.e. with express req.query)
await  xero.setAccessTokenFromRedirectUri(req.query);

// Optional: read user info from the id token
let tokenClaims = await xeroClient.readIdTokenClaims();
  
//Call the Xero API
let apiResponse = await xero.accountingApi.getInvoices(xero.tenantIds[0]);
console.log("Invoices[1]: ", apiResponse.body.invoices[0].invoiceID);

// Refresh the token when it expires
await xeroClient.refreshToken();
```


#### Project Structure
```
src/
  |-  gen/        autogenerated TypeScript
  `-  *.ts        handwritten TypeScript
dist/             compiled JavaScript
package.json
```
