![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)
codat.js
1. Installation
2. Usage
2.1 Building an API client
2.2 Interacting with companies
2.3 Using company data query objects
2.4 Using the company data client
1. Installation
Node module for accessing the Codat Accounting Data API from your node applications.
You can install this package using the following command:
npm install --save codat
For more information on Codat see www.codat.io
2. Usage
The codat library is written in idiomatic ES6 this is to support the use of modern features and tooling to make your life as a developer easier.
If you are running your application on a platfrom that does not support this version of Javascript, you will need to use additional tools to compile
the source code.
We use a tool called Babel at Codat and can highly recommend it.
Please refer to the tests in the project, they document the usage of the components found in the library.
2.1 API client
The API client is a handy object that exposes useful functionality from the Codat public API, as well as helping you make queries against data for linked companies.
import { api as codat } from 'codat';
var apiKey = '<YOUR API KEY HERE>';
var codatApiUat = codat.uat(apiKey);
var codatApi = codat.apiClient(codat.constants.UAT)(apiKey);
codatApiUat === codatApi;
var codatApiUat = codat.production(apiKey);
var codatApi = codat.apiClient(codat.constants.PRODUCTION)(apiKey);
var datasets = codat.constants.datasets;
2.2 Interacting with companies
When you want to intereact directly with your linked companies you can use the helper method exposed by the API client.
This set of features allows you to:
addCompany
- Add companies.removeCompany
- Remove companies.updateCompany
- Update companies.getCompany
- Get information about a specific company.getCompanies
- Query for all currently linked companies.
codatApi
.addCompany(new AddCompany('My Company', 'xero'))
.then(newCompany => console.log(newCompany.id));
codatApi
.getCompanies()
.then(response => response.companies.forEach(r => console.log(r.name)));
2.3 Using company data query objects
When you want to get hold of data for a specific company you can use one of the given query objects.
These query objects make building reuseable queries much easier as they specify the specific parameters for filters that you might want to use.
import { BalanceSheetQuery } from 'codat-queries';
var companyId = 'ff36ff03-17de-47be-883a-5ceecbbc65ed';
var balanceSheetQuery = new BalanceSheetQuery(companyId, 1, 3, new Date());
balanceSheetQuery
.run(codatApi)
.then(response => {
response.reports.forEach(r => console.log(`${r.date} - Net Assets: ${response.currency} ${r.netAssets}`));
});
2.4 Using the company data client
You can use the company data api client to build your own queries without the query objects and specifiy the arguments yourself.
In fact this is what the query objects use under the hood!
var companyId = 'ff36ff03-17de-47be-883a-5ceecbbc65ed';
var companyClient = codatApi.companyDataClient(companyId);
companyClient.get(datasets.BALANCE_SHEET, {
periodLength: 1,
periodsToCompare: 3
})
.then(response => {
response.reports.forEach(r => console.log(`${r.date} - Net Assets: ${response.currency} ${r.netAssets}`));
});