Skip to content

rantalainen/fortnox-api-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fortnox-api-client

FortnoxApiClient is a third party Fortnox API client for NodeJS. It is a wrapper around an API client that has been automatically generated using the OpenAPI schema provided by Fortnox.

Installation

Add to project's package.json:

npm install @rantalainen/fortnox-api-client

Import

import { FortnoxApiClient } from '@rantalainen/fortnox-api-client';

Authentication/Authorization

One must authorize integration to use company data and to acquire accessToken and refreshToken for Fortnox API. This API client provides static helper functions to achieve the tokens.

In order to obtain your client id, client secret and redirect uri please see Fortnox documentation.

Generate Authorization URI

Please see Fortnox Authorization Code docs for more information on the topic.

const authorizationUri = FortnoxApiClient.createAuthorizationUri(
  'your client id',
  'your integration redirect uri',

  // required scope
  ['companyinformation'],

  // state that should match while getting tokens in the next step
  'state',

  // type of authorization
  'service'
);

Fetch access token and refresh token

// Authorization Code acquired from previous step
const code = 'xxxx';

// State acquired from previous step
const state = 'xxxx';

// Validate state
if (state !== expectedState) {
  // Something wrong with authorization
}

const tokens = await FortnoxApiClient.getTokensByAuthorizationCode(
  'your client id',
  'your client secret',
  'your integration redirect uri'
  code
);

// tokens.accessToken = 1hour token to use Fortnox API
// tokens.refreshToken = 45day long lived token to refresh access token when necessary

Setup client with options

const fortnox = new FortnoxApiClient({
  clientId: 'your client id',
  clientSecret: 'your client secret',

  // To use endpoints you need to provide either accessToken or refreshToken to the client
  // If refreshToken is provided, the accessToken will be acquired on first request and refreshToken will also refresh
  // To acquire these tokens see above information about authentication/authorization
  accessToken: 'token for Fortnox API',
  refreshToken: 'token for Fortnox API'
});

Example usage after initialization:

const company = await fortnox.api.companyinformation.getCompanyInformationResource();

console.log(company);

Available methods can be found in the API documentation.

Resources