Skip to navigation

Calling Node.js SDK functions and handling errors

View as Markdown

Calling Node.js SDK functions and handling errors

Calling functions

Making API calls through the Node.js SDK requires calling defined functions in the API modules. Each of these functions corresponds to a group of endpoints that are used for specific feature areas related to the Docusign eSignature REST API.

Each specific REST endpoint typically has two or more Node.js SDK functions that can be used to call it. As an example, here are two functions that call the endpoint '/v2.1/accounts/{accountId}/brands/{brandId}':

this.getBrand = function(accountId, brandId, optsOrCallback, callback)
this.updateBrand = function(accountId, brandId, optsOrCallback, callback)

The difference between getBrand and updateBrand is the HTTP request method. The getBrand function makes a GET request to retrieve information for a specific brandId, while updateBrand makes a POST request to update the specific brand.

Handling errors

Error handling is an important part of developing your software. Your code should handle both expected and unexpected errors.

The Node.js SDK provides an DocusignAPIError function that is used for common scenarios where exceptions are thrown. It’s a best practice to catch this exception in your code and inspect the error code and message properties, which provide useful information to understand the cause of the error.

Common scenarios where the SDK throws a DocusignAPIError are:

  • Invalid or expired access token.
  • HTTP response status code 400 and above. These represent various error messages that are identified by the HTTP layer. Such errors include not authorized (403) errors, which occur due to issues with authentication.
  • Issues in deserialization of the response JSON. If the JSON sent back by the API cannot be properly deserialized into the Node.js objects that the SDK is expecting, a DocusignAPIError is thrown, and the “bad” JSON is returned in the message property of the exception.
  • JWT issues with either the private RSA key pair or userId. Since the requestJWTUserToken function for obtaining an OAuth token using JWT is included in the SDK, a DocusignAPIError is thrown if the RSA key or userId is missing or malformed.

Troubleshooting for common errors has detailed information about causes and resolutions for specific errors.

Obtaining the trace token for solving problems via Support

Every API response includes a GUID that can be used to trace it on the back end by Docusign Support. This GUID is called the trace token. The trace token enables Support staff to find the internal logs in the system related to the API request/response and to get additional information about it. This information may not be directly available to developers with or without the SDK.

To obtain the trace token, first create a callback function that will retain the trace token from the header of the HTTP response.

function cb(error, data, response) {
if (!error) {
console.log(`Brand updated`);
console.log(
`Trace token: ${response.headers["x-docusign-tracetoken"]}.`);
} else {
// error...
console.error (`\n\n${JSON.stringify(error,null,4)}\n\n`);
process.exit(1);
}
}

Pass this callback function as an argument when making your API call. The trace token will be logged to the console when the callback function is executed.

accountsApi.updateBrand(accountId, brandId, cb)

Note: Some API calls do not return an error, but instead provide error information inside the body of the HTTP response. In this case, you will need to have your code check the data returned from the API to make sure the call was successful.