Migrating to forge-apis npm package

March 15, 2017

The new package (forge-apis) is actually a bit simpler to use and the migration is quite straight forward as well.

With the old one I needed to do something like this to set the token to be used:

forgeDM.ApiClient.instance.authentications ['oauth2_access_code'].accessToken = tokenSession.getTokenInternal();
// or
forgeDM.ApiClient.instance.authentications ['oauth2_application'].accessToken = tokenSession.getTokenInternal();

With the new one, this is not needed.

Also now, instead of just storing the access token, it's easier to store the authentication and credentials objects used in the below code:

var forgeSDK = require('forge-apis');
var authentication = new forgeSDK.AuthClientThreeLegged(client_id, client_secret, callbackURL, scope);
authentication.getToken(code)
  .then(function (credentials) {
    storeCredentials(credentials); 
    storeAuthentication(authentication); 
  });
// scope needs to be an array of scope values you need, e.g.:
// scope = ['data:read', 'data:write'];

Then you can just keep passing in those two objects to all the other API calls, e.g.:

var projects = new forgeSDK.ProjectsApi();
projects.getHubProjects(resourceId/*hub_id*/, {}, authentication, credentials)
  .then(function (projects) {
    // do something
  });

When migrating your function calls, most of the time you just have to add three extra input parameters:
options, authentication, credentials

// old API call
var folders = new forgeDM.FoldersApi();
folders.getFolderContents(projectId, folderId)

// new API call
var folders = new forgeSDK.FoldersApi();
folders.getFolderContents(projectId, folderId, {}, authentication, credentials)

Not all of the new functions have an options input parameter though, so better check the function declaration to see if it contains an "opts" parameter or not.

As you can see it's not that difficult to migrate your code, so nothing holds you back from using the latest npm package :)

Posts by author

Adam Nagy joined Autodesk back in 2005 and has been providing programming support, consulting, training and evangelism to external developers.