Skip to main content

TypeScript SDK Quickstart

Following the steps described on this page, you'll create a simple Node Js application that interacts with the Fatture in Cloud API.

If you want to download the complete working example you can find it here.

0️⃣  Prerequisites

In this guide, we assume that these prerequisites are met:

1️⃣  Step One: Install the Fatture in Cloud SDK

In this quickstart, we'll use the Fatture in Cloud SDK, published to NPM.

To install it using Npm:

npm install @fattureincloud/fattureincloud-ts-sdk

2️⃣  Step Two: Set up the OAuth access token retrieval

Create the file oauth.ts and copy in the following code:

oauth.ts
import {
OAuth2AuthorizationCodeManager,
Scope,
} from "@fattureincloud/fattureincloud-ts-sdk";
import fs from "fs";
import http from "http";

export async function getOAuthAccessToken(
req: http.IncomingMessage,
res: http.ServerResponse
) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");

let query = !!req.url && req.url.split("?")[1];
let params = new URLSearchParams(query || "");

let oauth = new OAuth2AuthorizationCodeManager(
"CLIENT_ID",
"CLIENT_SECRET",
"http://localhost:8000/oauth"
);

if (params.get("code") == null) {
res.writeHead(302, {
Location: oauth.getAuthorizationUrl(
[Scope.ENTITY_SUPPLIERS_READ],
"EXAMPLE_STATE"
),
});
res.end();
} else {
let code = params.get("code");

try {
let token = await oauth.fetchToken(code ?? "");
// saving the oAuth access token in the token.json file
fs.writeFileSync("./token.json", JSON.stringify(token, null, 4));
res.write("Token succesfully retrived and stored in token.json");
} catch (e) {
console.log(e);
}
res.end();
}
}

To make this code work properly the only thing you need to set your client id, client secret and eventually the redirect uri at line 12.

Store the Token safely!

In this QuickStart the access token and refresh token are stored in a file. This is only for educational purposes, the OAuth access token and refresh token are sensitive data and should be saved securely on your database. Also, never share your Client Secret with third-party actors, or publish it to your frontend!

4️⃣  Step Four: Set up the sample

Retrieve your Company ID!

In this example, we'll show how to retrieve your Company ID using the TypeScript SDK. If you plan to manage only one company, you can insert it directly into your code as a variable. Check the Company-scoped Methods page for more info.

Create the files index.ts and quickstart.ts in your working directory and copy in the following code:

index.ts
import * as http from "http";
import url from "url";
import { getOAuthAccessToken } from "./oauth";
import { getFirstCompanySuppliers } from "./quickstart";

const hostname = "127.0.0.1"; //set your hostname
const port = 8000; //set your port

const server = http.createServer(async (req, res) => {
var pathname = url.parse(req.url ?? "").pathname;

//url routing
switch (pathname) {
case "/oauth": //oauth endpoint
res.end(await getOAuthAccessToken(req, res));
break;
case "/quickstart": //quickstart endpoint
res.end(await getFirstCompanySuppliers());
break;
default:
res.end();
break;
}
res.end();
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Check the Redirect URL

Make sure your FattureInCloud app redirect URL points at the just edited file (e.g. http://localhost:8000/oauth).

5️⃣  Step Five: Run the sample

From the command line, run the following command:

ts-node index.ts

Now visit http://localhost:8000/oauth (or whatever your URL is), you will be redirected to the Fatture in Cloud login page where you will be asked to grant some permissions, according to what scopes you specified previously. Finally, you will see the success message and the access token will be stored in the token.json file.

You can now visit http://localhost:8000/quickstart (or whatever your URL is) to test the application.

  What now?

In this example, we used a limited set of the available API methods to show how to use our SDK.

If you want to access the full documentation of the available methods and models, you can check the following resources: