[New!] Box TypeScript SDK Generated

Olga Stefaniuk
Box Developer Blog
Published in
4 min readSep 5, 2023

Opting for type safety? Awesome! Let me get you acquainted with the new Box TypeScript SDK GENERATED đź’™. Our engineers decided to hop on a path of SDK unification and auto-generation.

Say goodbye to waiting for new Box APIs to be incorporated into the SDK. With our new auto-generation development approach, we can now add new Box APIs to the SDK at a much faster pace. This means you can leverage the most up-to-date features in your applications without delay. 🚀

Apart from that, you get a bunch of real-time suggestions and useful embedded documentation directly in your editor. Moreover, using TS in your project also helps to prevent more errors. TS is like a specialist who ensures we don’t make mistakes, only happy accidents that are identified as soon as possible. ⛑️🦺

A cat wearing a vest and construction helmet with TypeScript logo

In this blog post, we will explore how you can quickly start using the Box TS SDK to supercharge your Node.js application with the Box content cloud capabilities.

Prerequisites:

⚠️ Important to note: this SDK is still a beta version, and minor improvements are still being made. 🔧

Initialize a project (Node.js + TypeScript)

Let’s get into the meaty part. If you’re working on a brand new project open a terminal and:

  • create a new directory,
  • change the directory,
  • and initialise a new project.
mkdir box-ts-sdk
cd box-ts-sdk
npm init

Next, let’s install the dependencies:

// Check if you TS is installed on your machine
tsc --version
// Install if missing
npm install -g typescript

// Add types/node package
npm install --save @types/node

// Install Box TypeScript SDK
npm install box-typescript-sdk-gen

// Install dotenv package
npm install dotenv --save

Create an index.ts file in the root of your directory. Let’s also add a dist folder for the transpiled file. Additionally, we’ll need a tsconfig.json file. Check rootDir and outDir paths, so they match your folder structure.

// tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
},
"exclude":[
"./node_modules"
]
}

Next, get the developer token. Go to the Developer Console, open your Box app settings. In the Configuration tab, generate the Developer Token.

⚠️ The developer token is valid for an hour. You need to revoke it in the Developer Console once the token expires.

For securely storing data, create an .env file in the root folder and paste your developer token there.

DEVELOPER_TOKEN=INSERT_YOUR_DEVELOPER_TOKEN

Adjust Box app settings

Additionally, in the Developer Console, in your app’s configuration tab, check the Make API calls using the as-user header checkbox.

Be sure to approve your app in the Admin Console. You can find more details in this step-by-step guide.

Start using Box TS SDK

Authenticate using the developer token in the index.ts file. And finally, log the root folder items and all managed users.

require('dotenv').config();
import { Client } from "box-typescript-sdk-gen";
import { DeveloperTokenAuth } from "box-typescript-sdk-gen/lib/developerTokenAuth"

async function main(token:string | undefined) {
if (token) {
const auth = new DeveloperTokenAuth({ token });
const client = new Client({ auth });

const entries = (await client.folders.getFolderItems('0')).entries!;
entries.forEach((entry) => console.log(entry));

const users = await client.users.getUsers();
console.log(users.entries);
}
}

main(process.env.DEVELOPER_TOKEN);

Run the script by typing the following commands in the terminal:

// Transpile TS to JS
tsc
// Run the script
node dist/index.js

The script should log into the console object, which represents the content of the root folder and also a detiled list of managed users.

And that’s just the beginning; explore other possibilities such as managing, uploading, or downloading files and folders, collaborating, or automating workflows — the possibilities are endless. The Box TypeScript SDK Generated provides seamless integration between your applications and the powerful features offered by the Box Platform. Start exploring and harnessing the power of cloud content management today!

Follow the Box Developer Blog to stay up-to-date with new code samples and tutorials!

Questions, Bugs, and Feature Requests?

Check the issues tickets! Or, file a new one; our awesome SDK team will get back to you.

🦄 Want to learn from Box Platform champions?

Join our Box Developer Community for support and knowledge sharing!

Cheers!

--

--