<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jimioniay</title>
    <description>The latest articles on DEV Community by jimioniay (@jimioniay).</description>
    <link>https://dev.to/jimioniay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F225980%2Ff5c2d10b-3de1-47e0-bc3b-fecf42fb95b5.png</url>
      <title>DEV Community: jimioniay</title>
      <link>https://dev.to/jimioniay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jimioniay"/>
    <language>en</language>
    <item>
      <title>Getting Started with FreshBooks NodeJS SDK - Expenses &amp; Invoices</title>
      <dc:creator>jimioniay</dc:creator>
      <pubDate>Thu, 21 Oct 2021 14:08:02 +0000</pubDate>
      <link>https://dev.to/freshbooks/getting-started-with-freshbooks-nodejs-sdk-expenses-invoices-a6</link>
      <guid>https://dev.to/freshbooks/getting-started-with-freshbooks-nodejs-sdk-expenses-invoices-a6</guid>
      <description>&lt;p&gt;Getting Started with FreshBooks NodeJS SDK - Expenses &amp;amp; Invoices&lt;br&gt;
In this tutorial, we’ll be looking into the FreshBooks NodeJs SDK and how simple and easy it is to create, update and fetch Invoices, Expenses, Clients, Items, Payments, Projects, Time Entries etc. We have done all the heavy-lifting making it super convenient for you!&lt;/p&gt;

&lt;p&gt;We have handled http calls, http retries, Idempotency, consistent request and response structures and many more.This way you get to focus on your business logic rather than figuring out how the FreshBooks API works. &lt;/p&gt;

&lt;p&gt;Prerequisites &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A FreshBooks Developer account. If you don't have one, you can create one here.&lt;/li&gt;
&lt;li&gt;Authenticate yourself on the FreshBooks API using Oauth2.0. No idea how to do that? No problem, we have an excellent tutorial here.&lt;/li&gt;
&lt;li&gt;Basic knowledge of Async, Await and Node.js.&lt;/li&gt;
&lt;li&gt;A code editor (e.g. VS Code, Sublime, Atom etc.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get started!&lt;br&gt;
Install the FreshBooks Nodejs SDK&lt;/p&gt;

&lt;p&gt;In your Node project directory, install the FreshBooks NodeJs Client via npm or yarn&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @freshbooks/api 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn install @freshbooks/api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get your FreshBooks Client ID&lt;/p&gt;

&lt;p&gt;Login to the FreshBooks Dashboard, click on the Settings/Gear Icon, then click on Developer Portal. Select your Oauth App and then note the Client ID (you’ll need it). &lt;br&gt;
(By the way, this tutorial assumes you have created an existing Oauth App in the past and understand the dynamics of FreshBooks Authentication. If you haven’t, then this tutorial on how to create one.)&lt;/p&gt;

&lt;p&gt;Instantiate the FreshBooks Client &lt;br&gt;
Using the block of code, we can instantiate the FreshBooks Client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Client } from '@freshbooks/api';
import winston from 'winston'; // This is optional

//This logger is also optional
const logger = winston.createLogger({
   level: 'error',
   transports: [
       new winston.transports.File({ filename: 'error.log', level: 'error' }),
       new winston.transports.File({ filename: 'combined.log' }),
   ],
});


// Get CLIENT ID from STEP 2 ABOVE
const clientId = '&amp;lt;CLIENT ID&amp;gt;';

// Get token from authentication or helper function or configuration
const token = '&amp;lt;BEARER TOKEN&amp;gt;';

// Instantiate new FreshBooks API client
const freshBooksClient = new Client(token, {
   clientId
}, logger);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set a value for your Client ID and your Bearer Token. This tutorial assumes you have a helper function that helps generate the bearer tokens and refresh tokens from the /auth/oauth/token endpoints. If you don’t, you can check out authentication tutorial&lt;/p&gt;

&lt;p&gt;Confirm Instantiation &lt;br&gt;
Using the function below, we can confirm the instantiations works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const confirmClientInstantiation = async () =&amp;gt; {
   try {
       const { data: { firstName, roles } } = await    freshBooksClient.users.me()
       accountId = roles[0].accountId;
       logger.info(`Hello ${firstName}`)
       return {
           firstName,
           accountId
       }
   } catch ({ code, message }) {
       // Handle error if API call failed
       logger.error(`Error fetching user: ${code} - ${message}`)
       return {
           error: {
               code, message
           }
       }
   }
}
console.log(await confirmClientInstantiation());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything works as expected you should see a response similar to the below when you invoke the function. It also returns some useful information (especially the accountid. Store in a variable as you’ll need it in other method calls).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ firstName: 'John', accountId: 'Zz2EMMR' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there is something wrong, you will receive a response that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  error: {
    code: 'unauthenticated',
    message: 'This action requires authentication to continue.'
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create A Client&lt;br&gt;
If everything works as expected, you should be able to create a Client, an Invoice, etc. &lt;br&gt;
For simplicity, we'll create a Client. Once we do that, this same Client will be created immediately on the FreshBooks dashboard&lt;br&gt;
 we will create a Client and the same client created immediately on the FreshBooks Dashboard&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const createAClient = async () =&amp;gt; {
   let client =
   {
       fName: "John",
       lName: "Doe",
       email: 'no-reply@example.com',
   }
   console.log(accountId)
   try {
       const { ok, data } = await freshBooksClient.clients.create(client, accountId)
       return ok &amp;amp;&amp;amp; { data };
   } catch ({ code, message }) {
       return {
           error: { code, message }
       }
   }
}

console.log(await createAClient())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;List Expenses&lt;br&gt;
We should also be able to list Expenses using the sample block below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Fetch Expenses
const fetchExpenses = async () =&amp;gt; {
   try {
       const { ok, data } = await freshBooksClient.expenses.list(accountId);
       return ok &amp;amp;&amp;amp; data
   } catch ({ code, message }) {
       console.error(`Error fetching expenses for accountid:  ${accountId}. The response message got was ${code} - ${message}`)
   }
}

console.log(await fetchExpenses());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything checks out, you should get a list of expenses These expenses are  also listed on the FreshBooks Dashboard&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  expenses: [
    {
       …
      id: '7538415',
      taxAmount2: null,
      taxAmount1: null,
      visState: 0,
      status: 0,
      vendor: 'FreshBooks Payments',
      notes: 'CC Payment Transaction Fee Invoice: #2021-09',
      updated: 2021-04-17T06:45:36.000Z,
      ...
    }
  ] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion&lt;br&gt;
This implementation simply scratched the surface of the possibilities of the Node.js SDK as there are several use cases that can be achieved with it.&lt;/p&gt;

</description>
      <category>freshbooks</category>
      <category>node</category>
      <category>accounting</category>
      <category>freshbooksapi</category>
    </item>
  </channel>
</rss>
